HDU 4027 Can you answer these queries? (线段树)
Posted taozi1115402474
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4027 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
题意
有N艘战舰,每艘战舰的能量为E[i],然后有m次询问,当T=0时,[X,Y]之间的战舰能量开方,当T=1时,查询[X,Y]所有值的和
题解
线段树区间更新延迟标记,区间查询
这里有几个优化,当值为1的时候开方已经不影响结果了,所以得标记,下次再更新的时候直接跳过即可
代码
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 #define ll long long 8 9 const int N=1e5+5; 10 11 ll sum[N<<2],ans; 12 bool cnt[N<<2]; 13 14 void PushUp(int rt) 15 { 16 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 17 cnt[rt]=cnt[rt<<1]&&cnt[rt<<1|1]; 18 } 19 void Build(int l,int r,int rt) 20 { 21 if(l==r) 22 { 23 scanf("%lld",&sum[rt]); 24 return; 25 } 26 int mid=(l+r)>>1; 27 Build(l,mid,rt<<1); 28 Build(mid+1,r,rt<<1|1); 29 PushUp(rt); 30 } 31 void Update(int L,int R,int l,int r,int rt) 32 { 33 if(l==r) 34 { 35 sum[rt]=sqrt(sum[rt]); 36 if(sum[rt]<=1)cnt[rt]=true; 37 return; 38 } 39 int mid=(l+r)>>1; 40 if(L<=mid&&!cnt[rt<<1])Update(L,R,l,mid,rt<<1); 41 if(R>mid&&!cnt[rt<<1|1])Update(L,R,mid+1,r,rt<<1|1); 42 PushUp(rt); 43 } 44 void Query(int L,int R,int l,int r,int rt) 45 { 46 if(L<=l&&r<=R) 47 { 48 ans+=sum[rt]; 49 return; 50 } 51 int mid=(l+r)>>1; 52 if(L<=mid)Query(L,R,l,mid,rt<<1); 53 if(R>mid)Query(L,R,mid+1,r,rt<<1|1); 54 PushUp(rt); 55 } 56 int main() 57 { 58 int n,q,op,x,y,o=1; 59 while(scanf("%d",&n)!=EOF) 60 { 61 memset(cnt,0,sizeof cnt); 62 printf("Case #%d: ",o++); 63 Build(1,n,1); 64 scanf("%d",&q); 65 for(int i=0;i<q;i++) 66 { 67 scanf("%d%d%d",&op,&x,&y); 68 if(x>y)swap(x,y); 69 if(op==0) 70 Update(x,y,1,n,1); 71 else 72 ans=0,Query(x,y,1,n,1),printf("%lld ",ans); 73 } 74 printf(" "); 75 } 76 return 0; 77 }
以上是关于HDU 4027 Can you answer these queries? (线段树)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 4027 Can you answer these queries? (线段树)
HDU-4027 Can you answer these queries?(线段树区间开方)
HDU 4027 Can you answer these queries?
Can you answer these queries? HDU - 4027