CF1379F2 Chess Strikes Back (hard version)

Chess Strikes Back (hard version)

Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard \(2n \times 2m\): it has \(2n\) rows, \(2m\) columns, and the cell in row \(i\) and column \(j\) is colored white if \(i+j\) is even, and is colored black otherwise.

The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place \(n \times m\) kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner.

Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has \(q\) queries. In each query he either marks a cell as unavailable, or marks the previously unavailable cell as available. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him!

\(1 \leq n, m, q \leq 200\,000\)

题解

Let's divide the grid into \(nm\) squares of size \(2 \times 2\). Each square contains exactly two white cells. So, we should put exactly one king into one square.

Let's mark a square L, if its left upper cell is banned and R if its right down cell is banned. Square can be L and R at the same time.

  • If there exists some L-square \((x_1, y_1)\) and R-square \((x_2, y_2)\), such that: \(x_1 \leq x_2,y_1 \leq y_2\), the answer is NO.

    It's easy to prove because if such pair of cells exists we can consider path from \((x_1, y_1)\) to \((x_2, y_2)\). In this path, there will be two neighboring cells.

  • If no such pairs of cells exist the answer is YES.

So, after each query, we should check this condition.

Let's maintain the values:

  • \(a_x =\) minimal \(y\), such that \((x, y)\) is L-square

  • \(b_x =\) maximal \(y\), such that \((x, y)\) is R-square
    These values can be maintained using \(O(n)\) sets in \(O(\log{q})\) time.

然后找一下有没有\(a_i\leq b_j,i\leq j\),用线段树即可实现。

Total complexity is \(O((n+q)(\log{n}+\log{q}))\).

CO int N=2e5+10;
set<pair<int,int> > C;
set<int> L[N],R[N];

int minL[4*N],maxR[4*N],val[4*N];

#define lc (x<<1)
#define rc (x<<1|1)
#define mid ((l+r)>>1)
IN void push_up(int x){
	minL[x]=min(minL[lc],minL[rc]);
	maxR[x]=max(maxR[lc],maxR[rc]);
	val[x]=minL[lc]<=maxR[rc] or val[lc] or val[rc];
}
void modifyL(int x,int l,int r,int p,int v){
	if(l==r){
		minL[x]=v;
		val[x]=minL[x]<=maxR[x];
		return;
	}
	if(p<=mid) modifyL(lc,l,mid,p,v);
	else modifyL(rc,mid+1,r,p,v);
	push_up(x);
}
void modifyR(int x,int l,int r,int p,int v){
	if(l==r){
		maxR[x]=v;
		val[x]=minL[x]<=maxR[x];
		return;
	}
	if(p<=mid) modifyR(lc,l,mid,p,v);
	else modifyR(rc,mid+1,r,p,v);
	push_up(x);
}

#undef lc
#undef rc
#undef mid

int main(){
	int n=read<int>(),m=read<int>();
	for(int i=1;i<=n;++i){
		L[i].insert(m+1);
		modifyL(1,1,n,i,m+1);
		R[i].insert(0);
		modifyR(1,1,n,i,0);
	}
	for(int q=read<int>();q--;){
		int x=read<int>(),y=read<int>();
		if(C.count({x,y})){
			C.erase({x,y});
			if(x%2==1){
				L[(x+1)/2].erase((y+1)/2);
				modifyL(1,1,n,(x+1)/2,*L[(x+1)/2].begin());
			}
			else{
				R[x/2].erase(y/2);
				modifyR(1,1,n,x/2,*R[x/2].rbegin());
			}
		}
		else{
			C.insert({x,y});
			if(x%2==1){
				L[(x+1)/2].insert((y+1)/2);
				modifyL(1,1,n,(x+1)/2,*L[(x+1)/2].begin());
			}
			else{
				R[x/2].insert(y/2);
				modifyR(1,1,n,x/2,*R[x/2].rbegin());
			}
		}
		puts(val[1]?"NO":"YES");
	}
	return 0;
}

posted on 2020-07-20 14:09  autoint  阅读(313)  评论(0编辑  收藏  举报

导航