复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61#include<stdio.h> #define mid (l+r)/2 #define lson o*2,l,mid #define rson o*2+1,mid+1,r typedef long long ll; const int N=1e5+100; ll lazy[4*N],tree[4*N];int a[N]; void build(int o,int l,int r){ if(l==r){ tree[o]=a[l]; return ; } build(lson),build(rson); tree[o]=tree[o*2]+tree[o*2+1]; } int ql,qr;ll v; void pushdown(int o,int m){ if(!lazy[o])return; ll lz=lazy[o]; lazy[o*2]+=lz,lazy[o*2+1]+=lz,lazy[o]=0; tree[o*2]+=lz*(m-m/2),tree[o*2+1]+=lz*(m/2); }//线段树左边区间更长一些 void updata(int o,int l,int r){ if(ql<=l&&r<=qr){ tree[o]+=(r-l+1)*v; lazy[o]+=v; return ; } pushdown(o,r-l+1); if(ql<=mid) updata(lson); if(qr>mid) updata(rson); tree[o]=tree[o*2]+tree[o*2+1]; } ll query(int o,int l,int r){ ll ans=0; if(ql<=l&&r<=qr) return tree[o]; pushdown(o,r-l+1); if(ql<=mid) ans+=query(lson); if(qr>mid) ans+=query(rson); return ans; } int main(){ int n,q; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); while(q--){ char ch; scanf(" %c%d%d",&ch,&ql,&qr); if(ch=='Q') printf("%lldn",query(1,1,n)); else{ scanf("%lld",&v); updata(1,1,n); } } }
最后
以上就是朴实大门最近收集整理的关于POJ3468(用lazy标记进行延迟更新)的全部内容,更多相关POJ3468(用lazy标记进行延迟更新)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复