1. 程式人生 > >Can you answer these queries?(線段樹,區間更新,更新到點)

Can you answer these queries?(線段樹,區間更新,更新到點)

題目 連結:傳送門

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

這道題本來很好寫的,但是一直在用區間更新的方法在做,結果一直超時,太過於在意套路(模板)的思路,結果沒有自己做出來,太失望了(深刻的教訓)。

思路:這回區間更新要更新到每個點,當tree[num]==r-l+1(及所有的數都是1)的時候,就不用繼續更新了。
注意:樣例 1(或0) x y x可能大於y,要交換一下。

#include<stdio.h>
#include<math.h>
#include<string.h>
#define N 100009
#define  LL long long

LL tree[N*3+10],a[N];

void build(int
num,int l,int r) { if(l==r) { tree[num]=a[l]; return ; } int mid=(l+r)>>1; build(num<<1,l,mid); build(num<<1|1,mid+1,r); tree[num]=tree[num<<1]+tree[num<<1|1]; } void update(int num,int l,int r,int x,int y) { int mid=(l+r)>>1; if(x<=l&&r<=y) { if(tree[num]==r-l+1) return ; if(l==r) { tree[num]=sqrt(1.0*tree[num]); return ; } update(num<<1,l,mid,x,y); update(num<<1|1,mid+1,r,x,y); tree[num]=tree[num<<1]+tree[num<<1|1]; return ; } if(mid>=x) update(num<<1,l,mid,x,y); if(mid<y) update(num<<1|1,mid+1,r,x,y); tree[num]=tree[num<<1]+tree[num<<1|1]; } LL query(int num,int l,int r,int x,int y) { if(x<=l&&r<=y) return tree[num]; int mid=(l+r)>>1; LL sum=0; if(mid>=x) sum+=query(num<<1,l,mid,x,y); if(mid<y) sum+=query(num<<1|1,mid+1,r,x,y); return sum; } int main() { int n,m; int tt=1; while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) scanf("%lld",&a[i]); build(1,1,n); scanf("%d",&m); int x,y,z; printf("Case #%d:\n",tt++); while(m--) { scanf("%d%d%d",&x,&y,&z); if(y>z) { int p=y; y=z; z=p; } if(x) printf("%lld\n",query(1,1,n,y,z)); else update(1,1,n,y,z); } printf("\n"); } return 0; }