\ ([Lcoi2018] [wx11.1] ~ Tirpitz? \)
Time Limit: 1 s memory limit: 131072kb
Input file: T. In Output file: T. Out
Background
Wang was decadent On the ninth day, which is why he was so weak.
As we all know,Tirpitz)Also known as the "Lonely queen of the North ".
However, this item has nothing to do with the question.
Description
\ (Felling \) and the evil monk \ (neyii. Very fierce, Earth people in order to commemorate this great battle. So I wrote this epic story into a game namedBlack and white blocks
The second Chinese teenager \ (LZR \) on the earth is very immersed in this history. Not only does it take this history as the background of the question, but it has produced a set of questions that are very toxic, also went to play thisFierce combat game black and white blocks
However, goose, he's too tired to play.Black and white blocksYou can only play with one column)
Then, to simplify the problem, we flip this column into a row.
Then we use 0 to represent the white block, and 1 to represent the Black Block.
HoweverThis black and white block is constantly changing, so \ (LZR \) wants you to help him maintain this black and white Sequence
You need to support the following two operations
1. convert a white block into a Black Block
2. output the first white block on the left of a Black Block.
Input/Output Format
The first row has two numbers. \ (n, m \) indicates that this black and white sequence has an element, and a total of \ (M \) operations are required.
The second row contains \ (n \) numbers, which are the black and white sequence described in the question.
Next there are \ (M \) rows, each line has two numbers
If the input format is
E x
Turns the \ (x \) block into black. If it is black. Ignore this input.
If the output format is
Z x
Please output the white block at the leftmost of the \ (x \) blocks. If the \ (x \) blocks are white blocks, output \ (x \) ^ \ (k \). If no white block exists on the left of the \ (x \) number, \ (0 \) is output \).
\ (K \) is the \ (x \) of the previous operation, start \ (k = 0 \), and '^' is an exclusive or.
Output Format
Each row has a number, which is the position of the white block in operation 2.
Sample input/output sample output
4 40 0 0 0E 1Z 1E 2Z 4
Sample output
14
For \ (40 \%\) data, \ (n, m \ le 10000 \)
For \ (100 \ % \) data, \ (n, m \ le 3 \ cdot 10 ^ 6 \)
First \ (O (n) \) pre-processes each black spot \ (x \) at the beginning, and the first white point on the Left \ (f (x )\).
void Init(){ int Now = 0 ; for(int i = 1; i <= N; i ++) if(! Data[i]) Now = Data[i] ; else F[i] = Now ;}
Then, for each modification, we know that the point has been modified, but the attribute of the point before it has not changed. So we just need to assign it to the \ (f \) of the previous vertex. Then, for each query, query one \ (find (x. In fact, it uses an idea similar to querying a set.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 500010#define Inf 0x7fffffff#define LL long longusing namespace std ;int Read(){ int X = 0 ; char ch = getchar() ; while(ch > '9' || ch < '0') ch = getchar() ; while(ch >= '0' && ch <= '9') X = (X << 1) + (X << 3) + (ch ^ 48), ch = getchar() ; return X ;}int N, M, F[MAXN], Y ;bool Data[MAXN] ;int Find(int Now){ if(F[Now] == Now) return F[Now] ; else return F[Now] = Find(F[Now]) ;}void Init(){ int Now = 0 ; for(int i = 1; i <= N; i ++){ if(! Data[i]) Now = i ; F[i] = Now ; } }int main(){ N = Read(), M = Read() ; for(int i = 1; i <= N; i ++) Data[i] = Read() ; Init() ; for(int i = 1; i <= M; i ++){ char Opt = getchar() ; if(Opt == 'E'){ int X = Read() ; if(Data[X]) continue ; Data[X] = 1 ; F[X] = Find(X - 1) ; } else{ int X = Read() ; X = Find(X) ; printf("%d\n", X ^ Y) ; Y = X ^ Y ; } } return 0 ;}
[Lcoi2018] [wx] Tirpitz