poj 1195 Mobile phones(二维树状数组)

题目链接:poj 1195 Mobile phones

题目大意:四种操作

  • 0 s:清空(1,1)~(s,s)
  • 1 x y a:在(x,y)点加上a
  • 2 x1 y1 x2 y2:询问矩形(x1,y1)~(x2,y2)的和。
  • 3:结束

解题思路:纯二维树状数组。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1030;

#define lowbit(x) ((x)&(-x))
int fenw[maxn+5][maxn+5];

void add (int x, int y, int a) {
    for (int i = x; i <= maxn; i += lowbit(i)) {
        for (int j = y; j <= maxn; j += lowbit(j))
            fenw[i][j] += a;
    }
}

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

int main () {
    int type;
    int s, x, y, a, l, r;
    while (scanf("%d", &type) == 1 && type != 3) {
        if (type == 0) {
            scanf("%d", &s);
            for (int i = 1; i <= s; i++)
                for (int j = 1; j <= s; j++)
                    fenw[i][j] = 0;
        } else if (type == 1) {
            scanf("%d%d%d", &x, &y, &a);
            x++; y++;
            add(x, y, a);
        } else {
            scanf("%d%d%d%d", &x, &y, &l, &r);
            x++; y++; l++; r++;
            printf("%d\n", sum(l, r) - sum(x-1, r) - sum(l, y-1) + sum(x-1, y-1));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值