zoukankan      html  css  js  c++  java
  • 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 imes 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 imes 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 imes 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_ileq b_j,ileq 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;
    }
    
  • 相关阅读:
    软件开发沉思录读书笔记
    卓有成效的程序员读书笔记
    结对编程收获
    《提高c++性能的编程技术》读书笔记
    第六次读书笔记
    第五周读书笔记
    美团与它的超级大脑
    第四次读书笔记
    团队-团队编程项目爬取豆瓣电影top250-模块测试过程
    团队-爬取豆瓣电影TOP250-模块开发过程
  • 原文地址:https://www.cnblogs.com/autoint/p/13344770.html
Copyright © 2011-2022 走看看