QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603905#9170. Cycle GamepeterML 0ms0kbC++143.7kb2024-10-01 20:51:582024-10-01 20:51:58

Judging History

你现在查看的是最新测评结果

  • [2024-10-01 20:51:58]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-10-01 20:51:58]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

const int maxn=6e6+5;
int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={-1,0,1,-1,1,-1,0,1};
int fa[maxn],sz[maxn],n,m,q;
vector<pii> tr[maxn<<2];
map<pii,int> mp;
int st[maxn],top=0;
pii que[maxn];

int find(int x){
	if(fa[x]==x) return x;
	return find(fa[x]);
}
void merge(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y) return;
	if(sz[x]>sz[y]) swap(x,y);
	fa[x]=y;
	sz[y]+=sz[x];
	st[++top]=x;
}
void del(int lst){
	while(top>lst){
		int x=st[top];
		top--;
		sz[fa[x]]-=sz[x];
		fa[x]=x;
	}
}

int getid(int x,int y){
	return x*(m+2)+y;
}

bool bk[maxn];

void update(int now,int l,int r,int ql,int qr,pii x){
	if(ql>qr) return;
	if(ql<=l&&qr>=r){
		tr[now].push_back(x);
		return;
	}
	int mid=(l+r)>>1;
	if(ql<=mid) update(now<<1,l,mid,ql,qr,x);
	if(qr>mid) update(now<<1|1,mid+1,r,ql,qr,x);
}

void dfs(int now,int l,int r){
	int lst=top;
	for(pii x : tr[now]) merge(x.first,x.second);
	// printf("kk%d %d %d\n",now,l,r);
	if(l==r){
		int x=que[l].first,y=que[l].second,tt=-1;
		bool flag=0;
		for(int j=0;j<8;j++){
			int nx=x+dx[j],ny=y+dy[j];
			if(!bk[getid(nx,ny)]){
				if(tt==-1) tt=find(getid(nx,ny));
				else if(tt!=find(getid(nx,ny))){
					// if(l==47) printf("\n%d %d\n",nx,ny);
					flag=1;
					break;
				}
			}else{
				int ppt=top;
				for(int t=0;t<8;t++){
					int xx=nx+dx[t],yy=ny+dy[t];
					if(xx<0||xx>n+1||yy<0||yy>m+1) continue;
					if(bk[getid(xx,yy)]||(xx==x&&yy==y)) continue;
					merge(getid(nx,ny),getid(xx,yy));
				}
				int ttt=-1;
				for(int t=0;t<8;t++){
					int xx=x+dx[t],yy=y+dy[t];
					if((!bk[getid(xx,yy)])||(xx==nx&&yy==ny)){
						if(ttt==-1) ttt=find(getid(xx,yy));
						else if(ttt!=find(getid(xx,yy))){
							// if(l==47) printf("\n%d %d %d %d\n",nx,ny,xx,yy);
							flag=1;
							break;
						}
					}
				}
				del(ppt);
				if(flag) break;
			}
		}
		if(flag){
			for(int j=0;j<8;j++){
				int nx=x+dx[j],ny=y+dy[j];
				pii tmp=make_pair(getid(x,y),getid(nx,ny));
				if(bk[getid(nx,ny)]) continue;
				if(mp[tmp]==l){
					// if(l==45) printf("\n%d %d : %d %d %d %d\n",l,q,x,y,nx,ny);
					update(1,1,q,l+1,q,tmp);
				}else{
					// if(l==45) printf("\n%d %d : %d %d\n",l,mp[tmp]-1,tmp.first,tmp.second);
					update(1,1,q,l+1,mp[tmp]-1,tmp);
				}
			}
			// printf("%d : 0\n",l);
			putchar('0');
		}else{
			bk[getid(x,y)]=1;
			// printf("%d : 1\n",l);
			putchar('1');
		}
		del(lst);
		return;
	}
	int mid=(l+r)>>1;
	dfs(now<<1,l,mid);
	for(pii x : tr[now]) merge(x.first,x.second);
	dfs(now<<1|1,mid+1,r);
	del(lst);
}

int main(){
	
	mp.clear();
	
	scanf("%d %d %d",&n,&m,&q);
	for(int i=1;i<=q;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		// if(x==9&&y==8) printf("%d : %d %d\n",i,x,y);
		for(int j=0;j<8;j++){
			int nx=x+dx[j],ny=y+dy[j];
			pii t1=make_pair(getid(x,y),getid(nx,ny)),t2=make_pair(getid(nx,ny),getid(x,y));
			if(mp.find(t1)==mp.end()) update(1,1,q,1,i-1,t1);
			mp[t1]=mp[t2]=i;
		}
		bk[getid(x,y)]=1;
		que[i]=make_pair(x,y);
	}
	
	// for(int i=0;i<=n+1;i++){
		// for(int j=0;j<=m+1;j++) printf("%d %d : %d\n",i,j,getid(i,j));
	// }
	
	for(int i=0;i<=(n+2)*(m+2);i++) fa[i]=i;
	
	for(int x=0;x<=n+1;x++){
		for(int y=0;y<=m+1;y++){
			for(int k=0;k<8;k++){
				int nx=x+dx[k],ny=y+dy[k];
				if(nx<0||nx>n+1||ny<0||ny>m+1) continue;
				if(bk[getid(x,y)]||bk[getid(nx,ny)]) continue;
				// printf("%d %d - %d %d\n",x,y,nx,ny);
				merge(getid(x,y),getid(nx,ny));
			}
		}
	}
	
	// for(int i=1;i<=49;i++) printf("%d %d\n",que[i].first,que[i].second);
	
	for(int i=1;i<=q;i++) bk[getid(que[i].first,que[i].second)]=0;
	
	dfs(1,1,q);
	
	puts("");
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

4 3 7
2 1
2 2
2 3
3 1
3 2
4 1
4 2

output:

1111111

result: