线段树hdu-4027

发布时间 2023-07-19 23:22:01作者: smiling&weeping

Smiling & Weeping

                 ---- 姑娘,倘若,我双手合十的愿望里有你呢

Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
 
Input
The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
 
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
思路:这道题目是一道中规中矩的线段树,但是有点小坑,需要牢记,以后长一下记性:
if(L > R) swap(L , R);   (•́へ•́╬)不要问我怎么知道的(•́へ•́╬)
对了,对于无结束符可以使用 sacnf("%d",&n) != EOF 来判断
那么现在上代码:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn = 100100;
 8 ll a[maxn] , tree[maxn<<2];
 9 int t , n;
10 #define ls(p) p<<1
11 #define rs(p) p<<1|1
12 void push_up(int p)
13 {
14     tree[p] = tree[ls(p)] + tree[rs(p)];
15 }
16 void build(int p , int pl , int pr)
17 {
18     if(pl == pr) {tree[p] = a[pl]; return ; }
19     int mid = pl+pr >> 1;
20     build(ls(p) , pl , mid);
21     build(rs(p) , mid+1 , pr);
22     push_up(p);
23 }
24 void update(int L ,int R , int p , int pl , int pr)
25 {
26     if(pl == pr) {tree[p] = sqrt(tree[p]); return ;}
27     if(tree[p] <= pr-pl+1) return ;
28     int mid = pl+pr >> 1;
29     if(L <= mid)  update(L , R , ls(p) , pl , mid);
30     if(R >  mid)  update(L , R , rs(p) , mid+1 , pr);
31     push_up(p);
32 }
33 ll query(int L , int R , int p , int pl, int pr)
34 {
35     if(L <= pl && pr <= R)
36         return tree[p];
37     int mid = pr+pl >> 1;
38     ll res = 0;
39     if(L <= mid)  res += query(L , R , ls(p) , pl , mid);
40     if(R >  mid)  res += query(L , R , rs(p) , mid+1 , pr);
41     return res;
42 }
43 int main()
44 {
45     int ind = 0;
46     while(scanf("%d",&n) != EOF)
47     {
48         int k;
49         printf("Case #%d:\n",++ind);
50         for(int i = 1; i <= n; i++)
51             scanf("%lld",&a[i]);
52         scanf("%d",&k);
53         build(1,1,n);
54         for(int i = 1; i <= k; i++)
55         {
56             int opt , L , R;
57             scanf("%d%d%d",&opt,&L,&R);
58             if(L > R)   swap(L , R);
59             if(opt == 0)
60             {
61                 update(L , R , 1 , 1 , n);
62             }
63             else
64             {
65                 printf("%lld\n",query(L , R , 1 , 1 , n));
66             }
67         }
68         printf("\n");
69     }
70 }

青山不改,绿水长流,我们下次再见ヾ( ̄▽ ̄)Bye~Bye~