当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 洛谷P4054 [JSOI2009]计数问题(二维树状数组)

洛谷P4054 [JSOI2009]计数问题(二维树状数组)

2019年02月06日  | 移动技术网IT编程  | 我要评论

58免费小说网,吞噬星空最新章节列表,星际飚车王主题曲

题意

题目链接

sol

很傻x的题。。

c才100, n, m才300,直接开100个二维树状数组就做完了。。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 301;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m, col[maxn][maxn];
struct bit {
#define lb(x) (x & (-x))
    int t[maxn][maxn];
    void add(int x, int y, int v) {
        for(int i = x; i <= n; i += lb(i))
            for(int j = y; j <= m; j += lb(j))
                t[i][j] += v;
    }
    int sum(int x, int y) {
        int ans = 0;
        for(int i = x; i; i -= lb(i))
            for(int j = y; j; j -= lb(j))
                ans += t[i][j];
        return ans;
    }
    int query(int x1, int x2, int y1, int y2) {
        return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1);
    }
}t[101];
int main() {
    n = read(); m = read();
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= m; j++) 
            t[col[i][j] = read()].add(i, j, 1);
    int q = read();
    while(q--) {
        int opt = read(), a = read(), b = read(), c = read(), d, e;
        if(opt == 1) t[col[a][b]].add(a, b, -1), t[col[a][b] = c].add(a, b, 1);
        if(opt == 2) d = read(), e = read(), printf("%d\n", t[e].query(a, b, c, d));
    }
    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网