题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
解析:
树状数组
基础知识:http://blog.csdn.net/mullerwch/article/details/38382831
/* ID:muller8 Name: 1166 敌兵布阵 Reference:树状数组 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> using namespace std; #define MAXN 50005 #define INF 1e9 int c[MAXN]; int N; int lowbit(int n){ return n&(n^(n-1)); } void ADD(int p, int k){ while(p<=N){ c[p] += k; p += lowbit(p); } } int getsum(int p){ int sum = 0; while(p>0){ sum += c[p]; p -= lowbit(p); } return sum; } int main(){ int T; int Case = 0; scanf("%d", &T); while(T--){ scanf("%d", &N); int i; memset(c, 0, sizeof(c)); for(i=1; i<=N; ++i){ int k; scanf("%d", &k); ADD(i, k); } printf("Case %d: ", ++Case); char str[10]; int m,n; while(~scanf("%s", str), str[0]!='E'){ scanf("%d%d", &m, &n); if(str[0]=='A'){ ADD(m, n); } if(str[0]=='S'){ ADD(m, -n); } if(str[0]=='Q'){ printf("%d ", getsum(n) - getsum(m-1)); } } } return 0; }