码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4027 Can you answer these queries? 线段树,区间修改

时间:2015-05-05 16:05:10      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

            Can you answer these queries?




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
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的数组,支持2种操作:
1.0 a b  :把a到b之间的数都开平方
2.1 a b  :查询区间a~b的数之和
 
 
注意:这道题求出的平方根要下取整。
   然后,每个样例之后有空行。
 
有个技巧,写在了注释。
 
 
 
技术分享
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 
  6 using namespace std;
  7 
  8 #define lson l,m,rt<<1
  9 #define rson m+1,r,rt<<1|1
 10 #define LL long long
 11 const int maxn=100000+5;
 12 
 13 LL sum[maxn<<2];
 14 
 15 //因为2^63总和最多2^63
 16 //如果只有1个数,也就最多开7次根号
 17 //即开着开着就恒为1
 18 //所以,当该区间的和等于区间长度(r-l)+1的时候,
 19 //该区间的值都不会再更新
 20 
 21 //1表示这个区间不需要更新了
 22 
 23 void pushup(int rt)
 24 {
 25     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 26 
 27 }
 28 
 29 void build(int l,int r,int rt)
 30 {
 31     if(l==r)
 32     {
 33         scanf("%lld",&sum[rt]);
 34         return ;
 35     }
 36 
 37     int m=(l+r)>>1;
 38 
 39     build(lson);
 40     build(rson);
 41     pushup(rt);
 42 }
 43 
 44 void update(int L,int R,int l,int r,int rt)
 45 {
 46     if(sum[rt]==r-l+1)
 47         return ;
 48 
 49     if(l==r)
 50     {
 51         sum[rt]=(LL)(sqrt((long double)sum[rt]));
 52         //printf("%lld ",sum[rt]);
 53         return ;
 54     }
 55 
 56     int m=(l+r)>>1;
 57 
 58     if(L<=m)
 59         update(L,R,lson);
 60     if(R>m)
 61         update(L,R,rson);
 62 
 63     pushup(rt);
 64 }
 65 
 66 LL query(int L,int R,int l,int r,int rt)
 67 {
 68     if(L<=l&&R>=r)
 69     {
 70         return sum[rt];
 71     }
 72 
 73     int m=(l+r)>>1;
 74 
 75     LL ret=0;
 76 
 77     if(L<=m)
 78         ret+=query(L,R,lson);
 79     if(R>m)
 80         ret+=query(L,R,rson);
 81 
 82     return ret;
 83 }
 84 
 85 int main()
 86 {
 87     int cas=1;
 88 
 89     int n;
 90 
 91     while(scanf("%d",&n)!=EOF)
 92     {
 93         //memset(cal,0,sizeof(cal));
 94 
 95         build(1,n,1);
 96 
 97         int m;
 98         scanf("%d",&m);
 99 
100         int T,x,y;
101 
102         printf("Case #%d:\n",cas++);
103 
104         for(int i=1;i<=m;i++)
105         {
106             scanf("%d %d %d",&T,&x,&y);
107 
108             if(x>y)
109                 swap(x,y);
110 
111             if(T==0)
112             {
113                 update(x,y,1,n,1);
114             }
115             else
116             {
117                 printf("%lld\n",query(x,y,1,n,1));
118             }
119         }
120 
121         printf("\n");
122     }
123     return 0;
124 }
View Code

 

 
 
 
 
 
 

HDU 4027 Can you answer these queries? 线段树,区间修改

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4479090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!