题目传送门
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 n, q n,q ( 2 ≤ n ≤ 10 (2≤n≤10 (2≤n≤105, 1 ≤ q ≤ 10 1≤q≤10 1≤q≤105 ) . ). ).
The i-th of q following lines contains two integers r i , c i ( 1 ≤ r i ≤ 2 , 1 ≤ c i ≤ n ) , ri, ci (1≤ri≤2, 1≤ci≤n), 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 ) (1,1) (1,1) and ( 2 , n ) (2,n) (2,n) never appear in the query list.
Output
For each moment, if it is possible to travel from cell ( 1 , 1 ) (1,1) (1,1) to cell ( 2 , n ) , (2,n), (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).
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 ) . (1,1)→(1,2)→(1,3)→(1,4)→(1,5)→(2,5). (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 ) (1,3) (1,3).
After the fourth query, the ( 2 , 3 ) (2,3) (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.
题意
- 有一个 2 ? n 2*n 2?n的地图,小女孩从 ( 1 , 1 ) (1,1) (1,1)想移动到 ( 2 , n ) (2,n) (2,n),有 k k k次询问,每次询问更改一个格子状态(是否可以通过),只能上下左右移动而不能斜着移动,问每次操作后,是否可以移动到 ( 2 , n ) (2,n) (2,n)
题解
- 由于题目范围1e5,询问1e5,显然不能暴力直接搜
- 不能通过只有三种情况,按照墙处理。每次操作之后 c h e c k check check一下,加减墙数,最后判断是否有墙即可
AC-Code
#include <bits/stdc++.h>
using namespace std;const int maxn = 1e5;
bool ma[3][maxn + 7];
int num;
void check(int x, int y) {
int o;if (x == 1) o = 2;else o = 1;if (ma[x][y] == false) {
// 加墙if (ma[o][y] == true)++num;if (ma[o][y - 1] == true)++num;if (ma[o][y + 1] == true)++num;}else if (ma[x][y] == true) {
// 减墙if (ma[o][y] == true)--num;if (ma[o][y - 1] == true)--num;if (ma[o][y + 1] == true)--num;}ma[x][y] = !ma[x][y];
}
int main() {
int n, m;while (cin >> n >> m) {
num = 0;memset(ma, false, sizeof ma);while (m--) {
int x, y;cin >> x >> y;check(x, y);if (!num)cout << "YES" << endl;elsecout << "NO" << endl;}}return 0;
}
官方题解
#pragma GCC optimize("Ofast")#include <bits/stdc++.h>
using namespace std;#define endl '\n'int n, q;
vector<vector<int>> lava;void Input() {
cin >> n >> q;lava.resize(2, vector<int>(n, 0));
}void Solve() {
int blockedPair = 0;while (q--) {
int x, y; cin >> x >> y; x--; y--;int delta = (lava[x][y] == 0) ? +1 : -1;lava[x][y] = 1 - lava[x][y];for (int dy=-1; dy<=1; dy++) {
if (y+dy < 0 || y+dy >= n) continue;if (lava[1-x][y+dy] == 1) blockedPair += delta;}cout << ((blockedPair == 0) ? "Yes\n" : "No\n");}
}int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0); cin.tie(NULL);Input(); Solve(); return 0;
}