HDU-1166 敌兵布阵(树状数组做法)

传送门

线段树做法

树状数组学习博客:

https://blog.csdn.net/bestsort/article/details/80796531

https://blog.csdn.net/Small_Orange_glory/article/details/81290634

https://blog.csdn.net/u012972031/article/details/84474968

emmmm,也没什么说的,今天在重新学习树状数组,就又把这个题重新敲了一遍。

#include
#define int long long
using namespace std;
const int maxn = 5e4 + 10;
int n,c[maxn];
int lowbit(int x)
{
    return x & (-x);
}
void add(int x,int y)
{
    while (x <= n)
    {
        c[x] += y;
        x += lowbit(x);
    }
}
int sum(int x)
{
    int sum = 0;
    while(x > 0)
    {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}
signed main()
{
    //freopen("in","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T,Case = 0,d;
    cin >> T;
    for(int t = 1;t <= T; t++)
    {
        memset(c,0,sizeof(c));
        cin >> n;
        for(int i = 1;i <= n; i++)
        {
            cin >> d;
            add(i,d);
        }
        string s;
        cout << "Case " << t << ":" << endl;
        while(cin >> s && s[0] != 'E')
        {
            int x, y;
            cin >> x >> y;
            if(s[0] == 'Q')
                cout << sum(y) - sum(x - 1) << endl;
            else if(s[0] == 'A')
                add(x,y);
            else
                add(x,-y);
        }
    }
    return 0;
}

 

你可能感兴趣的:(数据结构)