Token Distance
Time limit2sMemory limit512 MB
Tokens on squares move over time, and each query asks whether tokens L to R are evenly spaced when sorted by square (ties by token number).
- Level
Medium7 of 10
- Topics
- Segment tree, Sorting, Math
- Solved
- No attempts yet
Problem
You are playing a board game with squares numbered from 0 to (inclusive) and N tokens numbered from 1 to N. Each token is located in one of the squares, and multiple tokens might be located in the same square. Initially, token i is located in square . The distance of two tokens is the difference between the square number where the first token is located and the square number where the second token is located.
For a set of tokens S, we check whether the tokens are well-separated as follows.
- Let A be the list of tokens in S sorted by increasing square number. If several tokens are in the same square, sort them by increasing token number.
- If there are fewer than three tokens in A, the tokens are well-separated.
- Otherwise, the tokens are well-separated if and only if the distance between every two neighbouring tokens in A is the same. Two tokens are neighbouring if their indices in A differ by one.
You perform Q operations of one of these two types:
- Move token X to square Y.
- Report whether the set of tokens numbered from L to R (inclusive) is well-separated.
Input
The first line contains two integers N and Q (; ), the number of tokens and the number of operations. The next line contains N integers (), the initial square of each token. Each of the next Q lines is one of the following.
1 X Y(; ): Move token X to square Y.2 L R(): Report whether the tokens numbered from L to R are well-separated.
There is at least one operation of the second type.
Output
For each operation of the second type, in input order, print YES or NO on its own line, depending on whether the tokens numbered from L to R are well-separated.