hdu 4027 Can you answer these queries? 线段树

来源:互联网 发布:网络视听审核员证书 编辑:程序博客网 时间:2024/06/07 12:50

 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

题意:给你N个数,有两种操作,第一个操作是吧[l,r]区间的数开根号,另一个操作是[l,r]区间求和

思路:典型的  线段树模板题,需要注意的是对区间开根号时如果每次都一直向下更新的话会超时,这里需要做一个剪枝,因为对1开根号还是1,如果某一个区间全是1,那莫就不用处理这个区间了,怎样判断一个区间全是1呢,只要满足这个区间的和=R-L+1就可以了
注意有个很坑的地方,输入区间是时用一个swap
#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>using namespace std;const int MAXN=100100;typedef long long ll;struct node{int l,r;ll sum;}segTree[MAXN*4];void build(int num,int l,int r){segTree[num].l=l;segTree[num].r=r;if(l==r) {    scanf("%lld",&segTree[num].sum);    return;}int mid=(l+r)>>1;build(num<<1,l,mid);build(num<<1|1,mid+1,r);segTree[num].sum=segTree[num<<1].sum+segTree[num<<1|1].sum; }void update(int num,int l,int r){int mid=(segTree[num].l+segTree[num].r)>>1;if(segTree[num].l==l&&segTree[num].r==r&&segTree[num].sum==r-l+1) return;//剪枝 if(segTree[num].l==segTree[num].r){segTree[num].sum=sqrt(segTree[num].sum*1);return;}if(r<=mid) update(num<<1,l,r);else if(l>mid) update(num<<1|1,l,r);else{update(num<<1,l,mid);update(num<<1|1,mid+1,r);}segTree[num].sum=segTree[num<<1].sum+segTree[num<<1|1].sum; }ll query(int num,int l,int r){long long ans=0;int mid=(segTree[num].l+segTree[num].r)>>1;if(segTree[num].l==l&&segTree[num].r==r) return segTree[num].sum;if(r<=mid) ans+=query(num<<1,l,r);else if(l>mid) ans+=query(num<<1|1,l,r);else{ans+=query(num<<1,l,mid);ans+=query(num<<1|1,mid+1,r);}return ans;}int main(void){int Test;int a,b,c;int N,M;int Case=0;while(scanf("%d",&N)!=EOF){   build(1,1,N);   scanf("%d",&M);   printf("Case #%d:\n",++Case);   for(int i=1;i<=M;i++)   {   scanf("%d%d%d",&c,&a,&b);   if(a>b) swap(a,b);   if(c) printf("%lld\n",query(1,a,b));   else update(1,a,b);   }   printf("\n");    }return 0;}


阅读全文
0 0