HDU-4027 Can you answer these queries? (Segment tree section prescribing)

http://acm.hdu.edu.cn/showproblem.php?pid=4027

 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

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 2 63
  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.

Sample Input

10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8

Sample Output

Case #1:
19
7
6

 

Subject to the effect:
A total number of n, m operations, the operation in two ways: 0 xy indicates the interval [x, y] of the radicand, 1 xy represents the query interval [x, y] and.

Problem-solving ideas:

Obviously segment tree

It is easy to think of lazy thinking sections updated, but the update This question is not simple addition and subtraction, multiplication and division, but prescribing. But if we can not prescribe to reduce the time delay by the complexity of the update, that idea here is not too lazy to try.

So we think square root, sqrt (1) = 1, and taking into account the data range for the 63 th power, that this number can only open up to seven times square, because of what the next before you prescribe are 1 a.

So we can start from this point, we do not update when the update delay, we need to determine what needs to be updated.

If SegTree within this range [rt] .sum = SegTree [rt] .r-SegTree [rt] .l + 1 (that is, within the range a bit are 1) it is clear that at this time no update interval, then direct return.

If this interval can be updated, it is updated directly this range. So each update are updated to the leaf node, and maintenance intervals on the line.

 

In addition, there are many questions which pits:

1: To distinguish between good data that is long long, those are int

2: Note that each sample a blank line after every output

3: Note Input x, y later to check if x <= y, if x> y, x and y to be exchanged (most pit point, did so many range query, this is the first I met also set this pit, it is interesting ...)

 

code show as below:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <set>
 10 #include <map>
 11 #include <math.h>
 12 const int INF=0x3f3f3f3f;
 13 typedef long long LL;
 14 const int mod=1e9+7;
 15 //const double PI=acos(-1);
 16 const int maxn=1e5+10;
 17 using namespace std;
 18 //ios::sync_with_stdio(false);
 19 //    cin.tie(NULL);
 20 
 21 int n,m;
 22 struct node
 23 {
 24     int l;
 25     int r;
 26     LL sum;
 27 }SegTree[maxn<<2];
 28 
 29 void PushUp(int rt)
 30 {
 31     SegTree[rt].sum=SegTree[rt<<1].sum+SegTree[rt<<1|1].sum;
 32 }
 33 
 34 void Build(int l,int r,int rt)
 35 {
 36     SegTree[rt].l=l;
 37     SegTree[rt].r=r;
 38     if(l==r)
 39     {
 40         scanf("%lld",&SegTree[rt].sum);
 41         return;
 42     }
 43     int mid=(l+r)>>1;
 44     Build(l,mid,rt<<1);
 45     Build(mid+1,r,rt<<1|1);
 46     PushUp(rt);
 47 }
 48 
 49 void Update(int L,int R,int rt)
 50 {
 51     int l=SegTree[rt].l;
 52     int r=SegTree[rt].r;
 53     if(L <= R & lt && L> = R & lt)
 54 is      {
 55          IF (SegTree [RT] == .sum SegTree [RT] .r-SegTree [RT] .L + 1 ) // Description The interval is a full, square root is not necessary 
56 is              return ;    
 57 is      }
 58      IF (R & lt == L) // this section is not. 1 
59      {
 60          SegTree [RT] = .sum (LL) sqrt (SegTree [RT] .sum * 1.0 );
 61 is          return ;
 62 is      }
 63 is      int MID = (L + R & lt) >> . 1 ;
 64      IF (L <= MID)
 65          the Update (L, R & lt, RT << . 1);
 66     if(R>mid)
 67         Update(L,R,rt<<1|1);
 68     PushUp(rt);
 69 }
 70 
 71 LL Query(int L,int R,int rt)
 72 {
 73     int l=SegTree[rt].l;
 74     int r=SegTree[rt].r;
 75     if(L<=l&&R>=r)
 76     {
 77         return SegTree[rt].sum;
 78     }
 79     LL SUM=0;
 80     int mid=(l+r)>>1;
 81     if(L<=mid)
 82         SUM+=Query(L,R,rt<<1);
 83     if(R>mid)
 84         SUM+=Query(L,R,rt<<1|1);
 85     return SUM;
 86 }
 87 
 88 int main()
 89 {
 90     //freopen("sample.txt","r",stdin);
 91     int num=1;
 92     while(~scanf("%d",&n))
 93     {
 94         printf("Case #%d:\n",num++);
 95         Build(1,n,1);
 96         scanf("%d",&m);
 97         for(int i=1;i<=m;i++)
 98         {
 99             int f,x,y;
100             scanf("%d %d %d",&f,&x,&y);
101             if(x>y)//坑点 
102                 swap(x,y);
103             if(f==0)
104             {
105                 Update(x,y,1);
106             }
107             else if(f==1)
108             {
109                 printf("%lld\n",Query(x,y,1));
110             }
111         }
112         printf("\n");//坑点 
113     }
114     return 0;
115 }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11408944.html