BZOJ1452 Count
BZOJ1452 Count
题解
看到这题\(c\)的数据范围之后才发现这题是个水题,开100个二维树状数组记录每个颜色的个数,之后就能做到\(log^2n\)的询问和修改了。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
#define lowbit(x) x&(-x)
const int maxn=305;
int n,m,q;
int a[maxn][maxn];
/*==================Define Area================*/
struct TreeArray {
int t[maxn][maxn];
void Modify(int x,int y,int d) {
for(int i=x;i<=n;i+=lowbit(i)) {
for(int j=y;j<=m;j+=lowbit(j)) {
t[i][j]+=d;
}
}
}
int Sum(int x,int y) {
int ans=0;
for(int i=x;i;i-=lowbit(i)) {
for(int j=y;j;j-=lowbit(j)) {
ans+=t[i][j];
}
}
return ans;
}
}T[105];
int main() {
read(n);read(m);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
read(a[i][j]);
T[a[i][j]].Modify(i,j,1);
}
}
read(q);
for(int i=1;i<=q;i++) {
int opt;
read(opt);
if(opt==1) {
int x,y,c;
read(x);read(y);read(c);
T[a[x][y]].Modify(x,y,-1);
T[c].Modify(x,y,1);
a[x][y]=c;
}
else {
int lx,ly,rx,ry,c;
read(lx);read(rx);read(ly);read(ry);read(c);
int ans=T[c].Sum(rx,ry)+T[c].Sum(lx-1,ly-1);
ans-=(T[c].Sum(lx-1,ry)+T[c].Sum(rx,ly-1));
printf("%d\n",ans);
}
}
return 0;
}
/*
3 3
1 2 3
3 2 1
2 1 3
3
2 1 2 1 2 1
1 2 3 2
2 2 3 2 3 2
*/
「我不敢下苦功琢磨自己,怕终于知道自己并非珠玉;然而心中既存着一丝希冀,便又不肯甘心与瓦砾为伍。」