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 |
题目中虽然说修改值c不超过1w,但是必须要用long long 否则就wa附赠一个树状数组代码,非常简陋,请见谅 #include<iostream> #include<cstdio> using namespace std; int n; const int maxn = 1e6+50; long long bit[maxn];//差分 long long bit2[maxn];// bit2[i] = i*bit[i] void add(int i,long long x) { int p = i; while(i<=n) { bit[i] +=x; bit2[i] +=p*x; i+= i&(-i); } } void modify(int l,int r,long long k){ add(l,k); add(r+1,-k); } long long sum(int i) { int p = i; long long ans=0; while(i>0) { ans += (p+1)*bit[i] -bit2[i]; i-=i&(-i); } return ans; } long long rangeAsk(int l,int r){ return sum(r) - sum(l-1); } int main() { int m; scanf("%d%d",&n,&m); int before = 0; for(int i=1; i<=n; ++i) { int t ; scanf("%d",&t); add(i,t-before); before = t; } char input[5]; while(m--){ scanf("%s",input); if(input[0]=='Q'){ int a; int b; scanf("%d%d",&a,&b); long long ans = rangeAsk(a,b); printf("%lld\n",ans); }else{ int a; int b; long long c; scanf("%d%d%lld",&a,&b,&c); modify(a,b,c); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator