【BZOJ 1452】 [JSOI2009]Count
【链接】 我是链接,点我呀:)
【题意】
【题解】
维护一百个二维树状数组。 二维区间求和。【代码】
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int C = 100;
const int N = 300;
int n,m,a[C+10][N+10][N+10],q,b[N+10][N+10];
int lowbit(int x){
return x&(-x);
}
void _upd(int num,int x,int y,int delta){
while (x<=n){
int temp = y;
while (temp<=m){
a[num][x][temp]+=delta;
temp+=lowbit(temp);
}
x+=lowbit(x);
}
}
int _sum(int num,int x,int y){
int temp = 0;
while (x>0){
int tempy = y;
while (tempy>0){
temp+=a[num][x][tempy];
tempy-=lowbit(tempy);
}
x-=lowbit(x);
}
return temp;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
scanf("%d%d",&n,&m);
rep1(i,1,n)
rep1(j,1,m){
scanf("%d",&b[i][j]);
_upd(b[i][j],i,j,1);
}
scanf("%d",&q);
while (q--){
int ope;
scanf("%d",&ope);
if (ope==1){
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
_upd(b[x][y],x,y,-1);
_upd(c,x,y,1);
b[x][y] = c;
}else{
int x1,y1,x2,y2,c;
scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&c);
printf("%d\n",_sum(c,x2,y2)-_sum(c,x2,y1-1)-_sum(c,x1-1,y2)+_sum(c,x1-1,y1-1));
}
}
return 0;
}