【BZOJ】【P1452】【JSOI2009】【Count】【二维树状数组】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1452

BZOJ上没给颜色的范围……,蒟蒻就离线做了……读入所有询问,并把初始给出的格点也视为询问,每次处理相同颜色的……树状数组维护……

A了之后看别人的题解……我靠,都是TM直接暴力,开100个二维bit…… = = 、不过蒟蒻rank3还是挺爽的……

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int getint(){
	int ok=0,res=0;char ch;
	while(1){
		ch=getchar();
		if(ch<='9'&&ch>='0'){
			ok=1;res*=10;res+=ch-'0';
		}else if(ok)break;
	}return res;
}
int tot;
int mp[301][301];
struct Qes{
	int x1,x2,y1,y2,c,ty,i;
	bool operator<(const Qes &q)const{
		return c<q.c;
	}
};
int anss[200001];
int q=0;
Qes Q[409001];
int d[301][301];
inline int lowbit(int x){
	return x&(-x);
}
inline void updata(int x0,int y0,int x){
	for(int i=x0;i<=n;i+=lowbit(i))
		for(int j=y0;j<=m;j+=lowbit(j))
			d[i][j]+=x;
}
inline int get(int x0,int y0){
	int ans=0;
	for(int i=x0;i>=1;i-=lowbit(i))
		for(int j=y0;j>=1;j-=lowbit(j))
			ans+=d[i][j];
	return ans;
}
int main(){
	n=getint();m=getint();
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++){
		int x=getint();
		Q[q].x1=i;Q[q].y1=j;Q[q].c=x;Q[q].ty=1;
		q++;mp[i][j]=x;
	}
	int qe=getint();
	while(qe--){
		Q[q].ty=getint();
		if(Q[q].ty==1){
			Q[q].x1=getint();Q[q].y1=getint();Q[q].c=getint();
			q++;
			Q[q].ty=3;
			Q[q].x1=Q[q-1].x1;Q[q].y1=Q[q-1].y1;Q[q].c=mp[Q[q].x1][Q[q].y1];
			mp[Q[q].x1][Q[q].y1]=Q[q-1].c;
		}else{
			Q[q].x1=getint();Q[q].x2=getint();Q[q].y1=getint();Q[q].y2=getint();Q[q].c=getint();
			Q[q].i=tot++;		
		}		
		q++;
	}
	stable_sort(Q,Q+q);
	for(int i=0;i<q;i++){		
		if(Q[i].ty==1){
			updata(Q[i].x1,Q[i].y1,1);
		}else
		if(Q[i].ty==2){
			int ans=get(Q[i].x2,Q[i].y2);
			ans-=get(Q[i].x1-1,Q[i].y2);
			ans-=get(Q[i].x2,Q[i].y1-1);
			ans+=get(Q[i].x1-1,Q[i].y1-1);
			anss[Q[i].i]=ans;
		}else{
			updata(Q[i].x1,Q[i].y1,-1);
		}
		if(i!=q-1&&Q[i].c!=Q[i+1].c)
			memset(d,0,sizeof(d));
	}
	for(int i=0;i<tot;i++)
		printf("%d\n",anss[i]);
	return 0;
}



你可能感兴趣的:(bzoj,省选)