C. NEKO's Maze Game(Codeforces Round #614 (Div. 2))

题目

NEKO#ΦωΦ has just got a new maze game on her PC!

The game’s main puzzle is a maze, in the forms of a 2×n
rectangle grid. NEKO’s task is to lead a Nekomimi girl from cell (1,1) to the gate at (2,n)

and escape the maze. The girl can only move between cells sharing a common side.

However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.

After hours of streaming, NEKO finally figured out there are only q
such moments: the i-th moment toggles the state of cell (ri,ci)

(either from ground to lava or vice versa).

Knowing this, NEKO wonders, after each of the q
moments, whether it is still possible to move from cell (1,1) to cell (2,n)

without going through any lava cells.

Although NEKO is a great streamer and gamer, she still can’t get through quizzes and problems requiring large amount of Brain Power. Can you help her?
Input

The first line contains integers n, q (2≤n≤105, 1≤q≤105).
The i-th of q following lines contains two integers ri, ci (1≤ri≤2, 1≤ci≤n), denoting the coordinates of the cell to be flipped at the i-th moment.
It is guaranteed that cells (1,1)and (2,n)never appear in the query list.
Output
For each moment, if it is possible to travel from cell (1,1)
to cell (2,n), print “Yes”, otherwise print “No”. There should be exactly q answers, one after every update.
You can print the words in any case (either lowercase, uppercase or mixed).
Example
Input
5 5
2 3
1 4
2 4
2 3
1 4

Output

Yes
No
No
No
Yes

Note

We’ll crack down the example test here:
After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1)→(1,2)→(1,3)→(1,4)→(1,5)→(2,5).
After the second query, it’s impossible to move to the goal, since the farthest cell she could reach is (1,3).
After the fourth query, the (2,3)
is not blocked, but now all the 4
-th column is blocked, so she still can’t reach the goal.
After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.

解释

题意是一个两行的方格网。每次告诉你一个格子并转化其状态(能走或不能走),要求你输出是否能够从1,1到2,n。
问题转化为某连续两格内是否上下都存在不能走的格,这样会使得无法形成通路。考虑到时间效率,不可以每次知道一个点才去询问当前的连通状态。
可以采用每放入一个点就去询问其另一行相邻的三个,统计封断格子的个数 1~3然后ans加入或减去这个值,这就是这一个格子对结果ans的影响贡献。加入或减去取决于当前格子改变后的状态,若是封闭就是加上,若变回连通态就是减去。
由此可以快速直接得到答案,不需要每次加入一个点就从头开始查询连通态,仅仅涉及到的是当前加入的点的相邻的三个点的状态查询。大大减少了时间开销。

#include<cstdio>
int G[3][100002];
int ans = 0;
int n;
int dis[6][2] ={{-1,0},{1,0},{1,-1},{1,1},{-1,-1},{-1,1}};
int cal(int a, int b){
	G[a][b] = 1^G[a][b];
	int l, r;
	int add = 0;
	for(int i = 0; i < 6; i++){
		l = a+dis[i][0];
		if(l > 2 || l < 1) continue;
		r = b+dis[i][1];
		if(r < 1||r > n) continue;
		add += G[l][r];
	}
	return G[a][b] ? add: -add;
}
int main(){
	int q;
	int temp;
	int a, b;
	int ans = 0;
	scanf("%d %d", &n, &q);
	for(int i = 0; i < q; i++){
		scanf("%d %d", &a, &b);
		temp = cal(a,b);
		ans += temp;
		printf("%s\n", ans > 0?"No":"Yes");
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值