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

Can you answer these queries? HDU - 4027

时间:2019-07-19 23:42:54      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:href   can   vector   size   names   线段   typedef   好的   namespace   

Can you answer these querites?

 HDU - 4027 

普通的线段树题,但是有一个问题是,区间更新时,因为必须更新每个点,才能更新区间,那么线段树更新就很慢了,无法使用lazy数组。有一个小技巧是当区间和等于区间长度时,那么说明已经到最好的情况了,不用再修改了。这一步简化后就可以过了

如果写线段树可以像喝水一样自然就好了

 1 #include <iostream>
 2 #include <cstring>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <cmath>
 6 #define lson l,m,rt<<1
 7 #define rson m+1,r,rt<<1|1
 8 #define mem(x) memset(x,0,sizeof(x))
 9 using namespace std;
10 typedef long long ll;
11 const int INF=9999999;
12 const int maxn=100000+5;
13 ll sum[maxn<<2];
14 int n;
15 
16 void pushup(ll rt){
17     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
18 }
19 
20 void build(int l,int r,int rt){
21     if(l==r){
22         scanf("%lld",&sum[rt]);
23         return;
24     }
25     int m=(l+r)>>1;
26     build(lson);
27     build(rson);
28     pushup(rt);
29 }
30 void update(int L,int R,int l,int r,int rt){
31     if(sum[rt]==r-l+1) return;
32     if(l==r) {
33         sum[rt]=(ll) sqrt(sum[rt]);
34         return;
35     }
36     int m=(l+r)>>1;
37     if(L<=m) update(L,R,lson);
38     if(R>m)update(L,R,rson);
39     pushup(rt);
40 }
41 
42 ll query(int L,int R,int l,int r,int rt){
43     if(L<=l&&R>=r){
44         return sum[rt];
45     }
46     ll res=0;
47     int m=(l+r)>>1;
48     if(L<=m) res+=query(L,R,lson);
49     if(R>m) res+=query(L,R,rson);
50     return res;
51 }
52 int main(){
53     int cnt=0;
54     while(cin>>n){    
55     printf("Case #%d:\n",++cnt);
56         build(1,n,1);
57         int m;scanf("%d",&m);
58         for(int i=1;i<=m;i++){
59             int a,b,c;scanf("%d%d%d",&a,&b,&c);
60             if(b>c) swap(b,c);
61             if(a==1){
62                 
63                 printf("%lld\n",query(b,c,1,n,1));
64             }
65             else 
66             {    update(b,c,1,n,1);
67             }
68         }
69         cout <<endl;
70     }
71     return 0;
72 }

 

Can you answer these queries? HDU - 4027

标签:href   can   vector   size   names   线段   typedef   好的   namespace   

原文地址:https://www.cnblogs.com/Msmw/p/11216055.html

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