Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:给一个线段树容易出错的测试用例In Reply To:Re:给一个线段树容易出错的测试用例 Posted by:black_sky_zhang at 2016-07-28 11:53:19 #include<stdio.h> #include<string.h> #include<iostream> #define N 111111 #define INF 0x3f3f3f3 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define root 1,n,1 #define LL long long using namespace std; LL add[N<<2],sum[N<<2]; void Pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void Build(int l,int r,int rt) { add[rt]=0; if(l==r) { scanf("%lld",&sum[rt]); return ; } int m=(l+r)>>1; Build(l,m,rt<<1); Build(m+1,r,rt<<1|1); Pushup(rt); } void Pushdown(int rt,int m) { if(add[rt]) { add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt]; sum[rt<<1]+=(m-(m>>1))*add[rt]; sum[rt<<1|1]+=(m>>1)*add[rt]; add[rt]=0; } } void Update(LL L,LL R,LL c,int l,int r,int rt) { if(L<=l&&r<=R) { add[rt]+=c; sum[rt]+=(LL)(r-l+1)*(LL)c; return ; } Pushdown(rt,r-l+1); int m=(l+r)>>1; if(L<=m)Update(L,R,c,lson); if(R>m)Update(L,R,c,rson); Pushup(rt); } LL Query(LL L,LL R,int l,int r,int rt) { if(L<=l&&r<=R) return sum[rt]; Pushdown(rt,r-l+1); int m=(l+r)>>1; LL res=0; if(L<=m)res+=Query(L,R,l,m,rt<<1); if(R>m)res+=Query(L,R,m+1,r,rt<<1|1); return res; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { memset(sum,0,sizeof(sum)); int a,b,c; Build(1,n,1); char ch[3]; while(m--) { scanf("%s",ch); if(ch[0]=='Q') { LL aa,bb; scanf("%lld%lld",&aa,&bb); printf("%lld\n",Query(aa,bb,root)); } else { LL aa,bb,cc; scanf("%lld%lld%lld",&aa,&bb,&cc); Update(aa,bb,cc,root); } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator