天天看點

C. NEKO's Maze Game題目連結: C. NEKO’s Maze Game

題目連結: C. NEKO’s Maze Game

time limit per test:1.5 seconds memory limit per test:256 megabytes inputstandard input outputstandard output

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≤10^5^, 1≤q≤10^5^).

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).

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.

題目大意

給你一個2xN的表格,一共有q次查詢,每次查詢前會有一個表格變成岩漿不能走,或者從岩漿變回了可以總,問你能否從1 1這個表格走到2 n這個位置,可以的話就輸出“Yse”,否則就是“No”;

解題思路

表格的變換我們可以用位運算 異或 來處理,問題是我們怎麼判斷這次變化後是否可行呢?經過畫圖的多次嘗試,我們發現如果這次不行,那麼肯定至少有一列都是岩漿或者至少有一個對角線的位置是岩漿,是以我們可以運用類似字首和的思想,每次統計以變化位置為中心的連着三個對立位置,如果該位置是岩漿,就統計對面三個位置有幾個熔漿,如果是可以走,那就減去前面位置有幾塊熔漿如果結果為零那說明可以走,因為沒有一塊岩漿,如果不唯一就不行,這裡為什麼呢?你想呀,如果你這個位置查詢的時候是0,可以走的說明什麼?說明之前他是1不能走,既然之前不能走,那之前的ans肯定包含不能走時的轉态,如果上一次這三個位置都是0,ans加的是0,這次減得也是零對結果沒影響,如果上次這三個位置是1呢?說明上次ans加了三個1,這次剛好減去這裡很巧妙的運用了位運算和字首和的思想,真的太巧了,膜拜大神們呀!

代碼

#include<bits/stdc++.h>
using namespace std;
const int mx=1e5+10;
int a[2][mx];

int main()
{
    int q,ans=0,n;
    cin>>n>>q;
    int x,y;
    while(q--)
    {
        cin>>x>>y;
        x--;//x--,友善以後的 ^ 運算 
        a[x][y]^=1;//改變這個位置的狀态 
        int m=a[x][y]*2-1;//如果是 0 就是可以走,那結果就要減,1的話加 
        ans+=m*(a[x^1][y-1]+a[x^1][y]+a[x^1][y+1]);//進行ans的累加 
        if(ans==0)
        cout<<"Yes"<<'\n';
        else cout<<"No"<<'\n';
    }
    return 0;
}            

複制