A Simple Problem with Integers POJ - 3468
2017-08-09 16:28
225 查看
点击打开链接
最近刚接触线段树 感觉很神奇
这道题是区间修改区间查询 pre[n*4]数组是关键(此题可以和懒惰标记合并)
只要pre[cur]不为零 就说明有上一次未完成的更新 这里的pre[cur]应该是“+=”而不是“=”
因为可能存在这种情况 上一次你在该点打了懒惰标记 但是还未及更新其子节点 这一次的更新又来到此点 此时用“=”会将上一次的覆盖 WA了好几次。。
最近刚接触线段树 感觉很神奇
这道题是区间修改区间查询 pre[n*4]数组是关键(此题可以和懒惰标记合并)
只要pre[cur]不为零 就说明有上一次未完成的更新 这里的pre[cur]应该是“+=”而不是“=”
因为可能存在这种情况 上一次你在该点打了懒惰标记 但是还未及更新其子节点 这一次的更新又来到此点 此时用“=”会将上一次的覆盖 WA了好几次。。
#include <stdio.h> long long sum[400001],pre[400001]; int laz[400001]; int n; void pushup(int cur); void pushdown(int len,int cur); void build(int l,int r,int cur); void update(int ll,int rr,long long val,int l,int r,int cur); long long query(int ll,int rr,int l,int r,int cur); int main() { long long c; int q,i,a,b; char ch[2]; while(scanf("%d%d",&n,&q)!=EOF) { for(i=1;i<=n*4;i++) { sum[i]=0; pre[i]=0; laz[i]=0; } build(1,n,1); while(q--) { scanf("%s",ch); if(ch[0]=='C') { scanf("%d%d%lld",&a,&b,&c); update(a,b,c,1,n,1); } else { scanf("%d%d",&a,&b); printf("%lld\n",query(a,b,1,n,1)); } } } return 0; } void pushup(int cur) { sum[cur]=sum[cur*2]+sum[cur*2+1]; return; } void pushdown(int len,int cur) { if(laz[cur]==1) { sum[cur*2]+=pre[cur]*(len-len/2); pre[cur*2]+=pre[cur]; laz[cur*2]=1; sum[cur*2+1]+=pre[cur]*(len/2); pre[cur*2+1]+=pre[cur]; laz[cur*2+1]=1; pre[cur]=0; laz[cur]=0; } return; } void build(int l,int r,int cur) { int m; if(l==r) { scanf("%lld",&sum[cur]); return; } m=(l+r)/2; build(l,m,cur*2); build(m+1,r,cur*2+1); pushup(cur); return; } void update(int ll,int rr,long long val,int l,int r,int cur) { int m; if(ll<=l&&r<=rr) { sum[cur]+=(r-l+1)*val; pre[cur]+=val; laz[cur]=1; return; } pushdown(r-l+1,cur); m=(l+r)/2; if(ll<=m) update(ll,rr,val,l,m,cur*2); if(rr>m) update(ll,rr,val,m+1,r,cur*2+1); pushup(cur); return; } long long query(int ll,int rr,int l,int r,int cur) { long long ans; int m; if(ll<=l&&r<=rr) { return sum[cur]; } pushdown(r-l+1,cur); ans=0,m=(l+r)/2; if(ll<=m) ans+=query(ll,rr,l,m,cur*2); if(rr>m) ans+=query(ll,rr,m+1,r,cur*2+1); return ans; }
相关文章推荐
- 线段树(成段更新) 之 poj 3468 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(线段树——区间更新)
- POJ 3468 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(树状数组区间更新) 续
- POJ 3468 A simple problem with integers Splay 区间维护模板题
- POJ 3468 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(线段树,延迟标记)
- 【POJ】3468 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
- POJ-3468-A Simple Problem with Integers
- POJ - 3468 B - A Simple Problem with Integers 线段树区间更新模板
- poj 3468 A Simple Problem with Integers(线段树)