HDU 1166 敌兵布阵 树状数组

看的别人的博客学的树状数组:

http://blog.csdn.net/lulipeng_cpp/article/details/7816527

http://blog.csdn.net/queuelovestack/article/details/47414119

/*          树状数组                */
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cstring>
#define N 51000
using namespace std;
int a[N],c[N],n;
int lowbit(int x)/*     c[t]展开以后有多少项        */
{
    return x&(-x);
}
int SUM(int i)/*        求a数组的前n项和               */
{
    int sum=0;
    while(i>0) {
        sum+=c[i];
        i-=lowbit(i);
    }
    return sum;
}
void update(int x,int y)/*      单点更新数组c      */
{
    while(x<=n) {
        c[x]+=y;
        x+=lowbit(x);
    }
}
void creat()/*      创建树状数组c                  */
{
    int i=1;
    while(i<=n) {
        update(i,a[i]);
        i++;
    }
}
int main()
{
    int t;
    cin>>t;
    for(int m=1; m<=t; m++) {
        int i;
        scanf("%d",&n);
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        for(i=1; i<=n; i++)
            scanf("%d",a+i);
        creat();
        printf("Case %d:
",m);
        getchar();
        string s;
        while(1) {
            cin>>s;
            if(s=="End")
                break;
            int x,y;
            scanf("%d %d%*c",&x,&y);
            if(s=="Query")
                printf("%d
",SUM(y)-SUM(x-1));
            else if(s=="Add")
                update(x,y);
            else
                update(x,-y);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yu0111/p/5456462.html