【JSOI 2009】 Count
【题目链接】
【算法】
二维树状数组
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 300 #define MAXC 100 int N,M,Q,i,j,opt,x,y,xa,ya,xb,yb,c; int a[MAXN+10][MAXN+10]; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x/10); putchar(x%10+'0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } struct BinaryIndexedTree { int bit[MAXC+10][MAXN+10][MAXN+10]; int lowbit(int x) { return x & (-x); } inline void modify(int c,int x,int y,int val) { int i,j; for (i = x; i <= N; i += lowbit(i)) { for (j = y; j <= M; j += lowbit(j)) { bit[c][i][j] += val; } } } inline int query(int c,int x,int y) { int i,j,ret=0; for (i = x; i >= 1; i -= lowbit(i)) { for (j = y; j >= 1; j -= lowbit(j)) { ret += bit[c][i][j]; } } return ret; } inline int query(int c,int xa,int xb,int ya,int yb) { return query(c,xb,yb) - query(c,xb,ya-1) - query(c,xa-1,yb) + query(c,xa-1,ya-1); } } BIT; int main() { read(N); read(M); for (i = 1; i <= N; i++) { for (j = 1; j <= M; j++) { read(a[i][j]); BIT.modify(a[i][j],i,j,1); } } read(Q); while (Q--) { read(opt); if (opt == 1) { read(x); read(y); read(c); BIT.modify(a[x][y],x,y,-1); BIT.modify(c,x,y,1); a[x][y] = c; } else { read(xa); read(xb); read(ya); read(yb); read(c); writeln(BIT.query(c,xa,xb,ya,yb)); } } return 0; }