BZOJ 1452: [JSOI2009]Count(二维BIT)

为每一个权值开一个二维树状数组.

-------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep(i, n) for(int i = 0; i < n; ++i)
#define Rep(i ,n) for(int i = 1; i <= n; ++i)
 
using namespace std;
 
const int maxn = 300 + 5;
 
#define lowbit(x) ((x) & -(x))
 
int n, m;
 
struct BIT {
int b[maxn][maxn];
BIT() { memset(b, 0, sizeof b); }
void add(int x, int y, int v) {
for(int i = x; i <= n; i += lowbit(i))
   for(int j = y; j <= m; j += lowbit(j))
       b[i][j] += v;
}
int sum(int x, int y) {
int ans = 0;
for(int i = x; i > 0; i -= lowbit(i))
   for(int j = y; j > 0; j -= lowbit(j))
       ans += b[i][j];
return ans;
}
};
 
const int maxc = 100 + 5;
 
BIT g[maxc];
int M[maxn][maxn];
 
int main() {
freopen("test.in", "r", stdin);
cin >> n >> m;
Rep(i, n)
   Rep(j, m) {
    scanf("%d", &M[i][j]);
    g[M[i][j]].add(i, j, 1);
   }
int q;
cin >> q;
while(q--) {
int op;
scanf("%d", &op);
if(op == 1) {
int x, y, v;
scanf("%d%d%d", &x, &y, &v);
g[M[x][y]].add(x, y, -1);
g[M[x][y] = v].add(x, y, 1);
} else {
int x1, x2, y1, y2, v;
scanf("%d%d%d%d%d", &x1, &x2, &y1, &y2, &v);
x1--; y1--;
printf("%d\n", g[v].sum(x2, y2) + g[v].sum(x1, y1) - g[v].sum(x1, y2) - g[v].sum(x2, y1));
}
}
return 0;
}

  

------------------------------------------------------------------------- 

1452: [JSOI2009]Count

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1384  Solved: 814
[Submit][Status][Discuss]

Description

Input

Output

Sample Input



Sample Output

1
2

HINT



Source

 

posted @ 2015-05-07 22:51  JSZX11556  阅读(196)  评论(0编辑  收藏  举报