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 |
必须ac就是一道树状数组 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int N=100010; int a[N]; int n,m; typedef long long LL; LL c[2][N]; LL s[N]; int lowbit(int x) { return x&-x; } LL sum(int k,int x) { LL ans=0; for(int i=x;i;i-=lowbit(i)) ans+=c[k][i]; return ans; } void add(int k,int x,int y) { for(int i=x;i<=n;i+=lowbit(i)) { c[k][i]+=y; } } int main() { cin>>n>>m; for(int i=1;i<=n;i++) { cin>>a[i]; s[i]=s[i-1]+a[i]; } while(m--) { char op[2]; int l,r,d; scanf("%s",op); scanf("%d%d",&l,&r); if(op[0]=='C') { scanf("%d",&d); add(0,l,d); add(0,r+1,-d); add(1,l,l*d); add(1,r+1,-(r+1)*d); } else { LL ans=s[r]+(r+1)*sum(0,r)-sum(1,r); ans-=s[l-1]+l*sum(0,l-1)-sum(1,l-1); cout<<ans<<endl; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator