hdu2642 Stars 二维树状数组

Problem:
给了一个矩阵,每次可以更新一个点的值,多次查询一个子矩阵的和。
Solution:
二维树状数组。

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s,0,sizeof(s))

const double PI = 3.141592653589;
const int INF = 0x3fffffff;

bool b[1010][1010];

long long BI_tree[1010][1010];
int nx, ny;

inline int lowbit(int x) {
    return x & -x;
}

void update(int x, int y, int v) {
    for(int i = x; i <= nx; i += lowbit(i))
        for(int j = y; j <= ny; j += lowbit(j))
            BI_tree[i][j] += v;
}

long long query(int x, int y) {
    long long ans = 0;
    for(int i = x; i > 0; i -= lowbit(i))
        for(int j = y; j > 0; j -= lowbit(j))
            ans += BI_tree[i][j];

    return ans;
}

int main() {
//        freopen("/Users/really/Documents/code/input","r",stdin);
    //    freopen("/Users/really/Documents/code/output","w",stdout);
    ios::sync_with_stdio(false);

    int m;
    cin >> m;
    nx = ny = 1001;
    while(m--) {
        char c;
        int x, y, x1, y1;

        cin >> c;
        if(c == 'B') {
            cin >> x >> y;
            x++;  y++;
            if(b[x][y] == false) {
                update(x, y, 1);
                b[x][y] = true;
            }
        }
        else if(c == 'D') {
            cin >> x >> y;
            x++;  y++;
            if(b[x][y] == true) {
                update(x, y, -1);
                b[x][y] = false;
            }
        }
        else if(c == 'Q') {
            cin >> x >> x1 >> y >> y1;
            x++;  y++;  x1++;  y1++;
            if(x1 < x)
                swap(x, x1);
            if(y1 < y)
                swap(y, y1);
            cout << query(x1, y1) - query(x-1, y1) - query(x1, y-1) + query(x-1, y-1) << endl;
        }

    }





    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值