敌兵布阵(树状数组模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

中文题,直接套树状数组模板即可。

代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int  maxn = 50000 + 20;
const int  maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
const int MOD = 1000;
const double PI = acos(-1.0);
int n, m, k;
int tree[maxn];
int lowbit(int x){
    return x & (-x);
}
void add(int x, int num){
    while(x <= n){
        tree[x] += num;
        x += lowbit(x);
    }
}
int get_sum(int x){
    int sum = 0;
    while(x >= 1){
        sum += tree[x];
        x -= lowbit(x);
    }
    return sum;
}
char s[15];
int main(){
    int t, kase = 0;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        memset(tree, 0, sizeof tree);
        int x;
        for(int i = 1; i <= n; ++i){
            scanf("%d", &x);
            add(i, x);
        }
        printf("Case %d:\n", ++kase);
        int a, b;
        while(scanf("%s", s) == 1){
            if(s[0] == 'E') break;
            scanf("%d%d", &a, &b);
            if(s[0] == 'A'){
                add(a, b);
            }
            else if(s[0] == 'S'){
                add(a, -b);
            }
            else if(s[0] == 'Q'){
                printf("%d\n", get_sum(b) - get_sum(a - 1));
            }
        }
    }
    return 0;
}


你可能感兴趣的:(OJ,——,HDU,数据结构,——,树状数组)