[破碎的状态] [-4] Hdu 1166
一道树状数组的好题
感谢@JCarlson帮忙找题
代码:
#include<set> #include<map> #include<list> #include<queue> #include<stack> #include<string> #include<time.h> #include<math.h> #include<memory> #include<vector> #include<bitset> #include<fstream> #include<stdio.h> #include<utility> #include<sstream> #include<string.h> #include<iostream> #include<stdlib.h> #include<algorithm> using namespace std; int a[100005]; void change(int i,int x) { for (;i<=50000;) { a[i]+=x; i+=i&(-i); } } int sum(int i) { int ans=0; for (;i!=0;) { ans+=a[i]; i^=(i&(-i)); } return ans; } int main() { #ifdef absi2011 freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int t; scanf("%d",&t); int zu; for (zu=0;zu<t;zu++) { printf("Case %d:\n",zu+1); int i; int n; scanf("%d",&n); memset(a,0,sizeof(a)); for (i=0;i<n;i++) { int x; scanf("%d",&x); change(i+1,x); } for (;;) { static char b[105]; scanf("%s",b); if (b[0]=='Q') { int l,r; scanf("%d%d",&l,&r); l--; printf("%d\n",sum(r)-sum(l)); } else if (b[0]=='S') { int l,x; scanf("%d%d",&l,&x); change(l,-x); } else if (b[0]=='A') { int l,x; scanf("%d%d",&l,&x); change(l,x); } else { break; } } } return 0; }