HDU 1166 敌兵布阵

来源:互联网 发布:论文数据捏造 编辑:程序博客网 时间:2024/05/01 08:56

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


解析

树状数组

基础知识:http://blog.csdn.net/mullerwch/article/details/38382831

/*ID:muller8Name: 1166  敌兵布阵Reference:树状数组*/#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define MAXN 50005#define INF 1e9int 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:\n", ++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\n", getsum(n) - getsum(m-1));            }        }    }    return 0;}


0 0
原创粉丝点击