hdu 4027

来源:互联网 发布:安卓投屏软件 airplay 编辑:程序博客网 时间:2024/06/06 04:35

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 19421    Accepted Submission(s): 4589


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 263.
  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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
 

Sample Output
Case #1:1976
 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
 

#include<stdio.h>  #include<iostream>  #include<string>  #include<algorithm>  #include<string.h>  #include<map>   #include<math.h>  #include<set>  #include<queue>  #include<vector>  #include<stack>  #define MAX 100005using namespace std; struct node{      int l,r;long long sum;}f[MAX*4];long long a[100005];int p=1;long long buildtree(int root,int l,int r){f[root].l=l;f[root].r=r;if(l==r){//printf("%d ",root);f[root].sum=a[p++];//printf("%d ",p);return f[root].sum;}return f[root].sum=buildtree(2*root,l,(l+r)/2)+buildtree(2*root+1,(l+r)/2+1,r);}void insert(int root,int l,int r){if((r-l+1)==f[root].sum)//当这个区间所有数都为1 就没必要更新了,即区间长度等于和 return;if(f[root].l==f[root].r){f[root].sum=sqrt(f[root].sum);return;}int mid=(f[root].l+f[root].r)/2;if(l>=mid+1){insert(2*root+1,l,r);}else if(r<=mid){insert(2*root,l,r);}else{insert(2*root,l,mid);insert(2*root+1,mid+1,r);}f[root].sum=f[2*root].sum+f[2*root+1].sum;}long long sum1;void printftree(int root,int l,int r){if(f[root].l==l&&f[root].r==r){sum1+=f[root].sum;return;}int mid=(f[root].l+f[root].r)/2;if(l>=mid+1){printftree(2*root+1,l,r);}else if(r<=mid)printftree(2*root,l,r);else{printftree(2*root,l,mid);printftree(2*root+1,mid+1,r);}}void f1(int root){printf("%d->%d:%d\n",f[root].l,f[root].r,f[root].sum);if(f[root].l==f[root].r){return;}f1(2*root);f1(2*root+1);}int main(){int i,n,x,y,k,t=0,d,b,a1,c;while(~scanf("%d",&n)){t++;p=1;printf("Case #%d:\n",t); for(i=1;i<=n;i++){cin>>a[i];}buildtree(1,1,n);//f1(1);scanf("%d",&k);for(i=1;i<=k;i++){scanf("%d%d%d",&a1,&b,&c);sum1=0;if(b>c)swap(b,c);//这地方太坑了,一直run time error if(a1==0){insert(1,b,c);//f1(1);}else{printftree(1,b,c);printf("%lld\n",sum1);}}printf("\n");}}