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 |
TL 了 。。。求指教#include<cstdio> #include<cstring> typedef long long ll; const int maxn=262144; ll sum[maxn],add[maxn]; int node[maxn/2]; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void bulid(int l,int r,int rt) { if(l==r) { sum[rt]=(ll)node[l]; return ; } int m=(l+r)>>1; bulid(l,m,rt<<1); bulid(m+1,r,rt<<1|1); pushup(rt); } void pushdown(int rt,int ln,int rn) { if(add[rt]) { add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt]; sum[rt<<1]+=add[rt]*(ll)ln; sum[rt<<1|1]+=add[rt]*(ll)rn; add[rt]=0; } } void update(int L,int R,ll c,int l,int r,int rt) { if(l==r) { sum[rt]+=(ll)c*(r-l+1); add[rt]+=c; return ; } int m=(l+r)>>1; pushdown(rt,m-l+1,r-m); if(L<=m) update(L,R,c,l,m,rt<<1); if(R>m) update(L,R,c,m+1,r,rt<<1|1); pushup(rt); } ll query(int L,int R,int l,int r,int rt) { if(L<=l && R>=r) return sum[rt]; int m=(l+r)>>1; pushdown(rt,m-l+1,r-m); ll ans=0; if(L<=m) ans+=query(L,R,l,m,rt<<1); if(R>m) ans+=query(L,R,m+1,r,rt<<1|1); return ans; } int main() { int n,q,x,y; char ch; ll z; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&node[i]); bulid(1,n,1); for(int i=0;i<q;i++) { getchar(); scanf("%c",&ch); if(ch=='Q') { scanf("%d%d",&x,&y); printf("%lld\n",query(x,y,1,n,1)); } else if(ch=='C') { scanf("%d%d%lld",&x,&y,&z); update(x,y,z,1,n,1); } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator