洛谷P4054 [JSOI2009]计数问题(二维树状数组)
程序员文章站
2022-06-30 13:45:52
题意 "题目链接" Sol 很傻x的题。。 c才100, n, m才300,直接开100个二维树状数组就做完了。。 cpp include using namespace std; const int MAXN = 301; inline int read() { char c = getchar( ......
题意
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; }