[kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers
2017-10-01 13:24
429 查看
https://vjudge.net/contest/66989#problem/C
区间修改
区间修改
#include <cstdio> #include <algorithm> #include <iostream> #define lid (id << 1) #define rid (id << 1 | 1) using namespace std; const int maxn = 1e5+6; int n,a[maxn],m; struct node { int l,r; long long sum,lazy; void updata(long long x) { sum =sum+(r-l+1)*x; //增加 lazy=lazy+x; } } tr[maxn*4]; void push_up(int id) { tr[id].sum=tr[lid].sum+tr[rid].sum; } void push_down(int id) { if(tr[id].lazy) { tr[lid].updata(tr[id].lazy); tr[rid].updata(tr[id].lazy); tr[id].lazy=0; } } void build(int id,int l,int r) { tr[id].l=l,tr[id].r=r; if(l==r) tr[id].sum=a[l],tr[id].lazy=0; else { int mid=(l+r)/2; build(lid,l,mid); build(rid,mid+1,r); push_up(id); } } void updata(int id,int l,int r,long long val) { if(l==tr[id].l&&tr[id].r==r) tr[id].updata(val); else { push_down(id); int mid = (tr[id].l+tr[id].r)>>1; if(r<=mid) updata(lid,l,r,val); else if (l>mid) updata(rid,l,r,val); else { updata(lid,l,mid,val); updata(rid,mid+1,r,val); } push_up(id); } } long long query_sum(int id,int l,int r) { if(l==tr[id].l&&tr[id].r==r) return tr[id].sum; else { push_down(id); int mid = (tr[id].l+tr[id].r)>>1; if(r<=mid) return query_sum(lid,l,r); else if(l>mid) return query_sum(rid,l,r); else return query_sum(lid,l,mid)+query_sum(rid,mid+1,r); push_up(id); } } int main() { while(scanf("%d%d",&n,&m)==2) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); char ch; for(int i=1;i<=m;i++) { scanf(" %c",&ch); if(ch=='Q') { int x,y; scanf("%d%d",&x,&y); printf("%lld\n",query_sum(1,x,y)); } else if(ch=='C') { int val,x,y; scanf("%d%d%d",&x,&y,&val); updata(1,x,y,val); } } } }
相关文章推荐
- 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers
- kuangbin专题七:C题 A Simple Problem with Integers
- 线段树专题 POJ3468 A Simple Problem with Integers
- 线段树专题 POJ3468 A Simple Problem with Integers
- Poj 3468 A Simple Problem with Integers(树状数组&&线段树)
- POJ 3468 A Simple Problem with Integers(线段树区间修改)
- POJ 3468 A Simple Problem with Integers (线段树)
- C - A Simple Problem with Integers (线段树lazy区间增减)
- POJ 3468 A Simple Problem with Integers(线段树成段更新)
- poj 3468 A Simple Problem with Integers(线段树区间累加)
- 线段树系列-pku-3468-A Simple Problem with Integers-区间修改区间求和
- poj 3468 A Simple Problem with Integers(线段树——区间更新)
- POJ 3468 A Simple Problem with Integers(线段树、lazy思想)
- POJ 3468 A Simple Problem with Integers(线段树成段更新)
- POJ:3468 A Simple Problem with Integers(线段树模版题)
- POJ 3468 A Simple Problem with Integers (线段树)
- ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
- poj 3468 A Simple Problem with Integers 线段树 区间更新求和
- poj 3468 A Simple Problem with Integers【线段树】
- [线段树] poj3468 A Simple Problem with Integers