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.
InputThe 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.
OutputFor 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
题意:就是它的每次操作是将一段区间的所有数都开一次平方,这个好像分块的时候也做过一次。
题解:因为一个数经过几次平方根就会GG变为1,然后就只需要记录这段区间是否为r-l+1即可。
是的话就不需要继续开根好了,否则就暴力下去,各个数开平方,就没了,每个数平均5次暴力,
然后就是普通线段树了,复杂度近似为O(n log n)
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 const int MAXN=100017; 6 typedef long long LL; 7 long long a[MAXN],tree[MAXN*4]; 8 int x,y,z,q,n,t; 9 using namespace std; 10 11 void build(int l,int r,int p) 12 { 13 if (l==r) tree[p]=a[l]; 14 else 15 { 16 int mid=(l+r)>>1; 17 build(l,mid,p*2); 18 build(mid+1,r,p*2+1); 19 tree[p]=tree[p*2]+tree[p*2+1]; 20 } 21 } 22 LL query(int l,int r,int p,int x,int y) 23 { 24 if (l==x&&r==y) return tree[p]; 25 else 26 { 27 int mid=(l+r)>>1; 28 LL res; 29 if (y<=mid) res=query(l,mid,p*2,x,y); 30 else if(x>=mid+1) res=query(mid+1,r,p*2+1,x,y); 31 else 32 { 33 res=query(l,mid,p*2,x,mid); 34 res=res+query(mid+1,r,p*2+1,mid+1,y); 35 } 36 return res; 37 } 38 } 39 void change(int l,int r,int p,int x,int y) 40 { 41 if (l==r) tree[p]=LL(sqrt(tree[p])); 42 else 43 { 44 if (tree[p]!=(r-l+1)) 45 { 46 int mid=(l+r)>>1; 47 if (y<=mid) change(l,mid,p*2,x,y); 48 else if (x>=mid+1) change(mid+1,r,p*2+1,x,y); 49 else 50 { 51 change(l,mid,p*2,x,mid); 52 change(mid+1,r,p*2+1,mid+1,y); 53 } 54 tree[p]=tree[p*2]+tree[p*2+1]; 55 } 56 } 57 } 58 int main() 59 { 60 t=0; 61 while (~scanf("%d",&n)) 62 { 63 memset(tree,0,sizeof(tree)); 64 memset(a,0,sizeof(a)); 65 printf("Case #%d: ",++t); 66 for (int i=1;i<=n;i++) 67 scanf("%lld",&a[i]); 68 build(1,n,1); 69 scanf("%d",&q); 70 for (int i=1;i<=q;i++) 71 { 72 scanf("%d%d%d",&x,&y,&z); 73 if (y>z) swap(y,z); 74 if (x) printf("%lld ",query(1,n,1,y,z)); 75 else change(1,n,1,y,z); 76 } 77 printf(" "); 78 } 79 }