二维树状数组--hdu1892

二维树状数组--hdu1892

题意

二维书架上原本有1本书

T组样例,四种操作,search,add,del,move

书架上的书小于del的值时,取完该格子的书即可

查询操作用二维的容斥推

由于格子取值可以等于0,故下标都+1

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 5;
const int MOD = 1e9 + 9;
const double pi = 3.1415926;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i)

int sum[N << 1][N << 1], p[N][N];
int lowbit(int x) {return x & (-x);}
void change(int x, int y, int num)
{
    for(int i = x;i <= N;i += lowbit(i))
        for(int j = y;j <= N;j += lowbit(j))
        sum[i][j] += num;
}

int query(int x, int y)
{
    int ret = 0;
    for(int i = x;i;i -= lowbit(i))
        for(int j = y;j;j -= lowbit(j))
        ret += sum[i][j];
    return ret;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int T;cin >> T;
    for(int cas = 1;cas <= T;++cas)
    {
        cout << "Case " << cas << ":" << endl;
        for(int i = 1;i < N;++i)
            for(int j = 1;j < N;++j)
            {sum[i][j] = lowbit(i) * lowbit(j);p[i][j] = 1;}
        int Q;cin >> Q;
        while(Q--)
        {
            string s;int a, b, c, d, e;
            cin >> s;
            if(s == "A")
            {
                cin >> a >> b >> c;
                a++;b++;p[a][b] += c;
                change(a, b, c);
            }
            else if(s == "S")
            {
                cin >> a >> b >> c >> d;
                a++;b++;c++;d++;
                if(a > c) swap(a, c);
                if(b > d) swap(b, d);
                cout << query(c, d) - query(a - 1, d) - query(c, b - 1) + query(a - 1, b - 1) << endl;
            }
            else if(s == "D")
            {
                cin >> a >> b >> c;
                a++;b++;c = min(c, p[a][b]);p[a][b] -= c;
                change(a, b, -c);
            }
            else
            {
                cin >> a >> b >> c >> d >> e;
                a++;b++;c++;d++;
                e = min(e, p[a][b]);p[a][b] -= e;p[c][d] += e;
                change(a, b, -e);change(c, d, e);
            }
        }
    }
    return 0;
}

posted @ 2019-05-13 21:22  谁知道你啊啊啊  阅读(178)  评论(0编辑  收藏  举报