Beautiful Necklaces: Input
Beautiful Necklaces: Input
Beautiful Necklaces: Input
Alice just got a new necklace for her birthday. The necklace consists of N beads numbered with IDs from
1 to N , where the bead with ID i and the bead with ID i + 1 (for each i ∈ [1, N − 1]) are connected
by a thread and we say that they are neighbors. The bead with ID N and the bead with ID 1 are also
connected, and they are neighbors as well. The color of the i-th bead is ci . Colors are represented by
positive integers.
Alice defines the beauty of a necklace as the length of the longest contiguous subsequence of beads that
have the same color. For example, if Alice’s necklace is N = 8 and c = [4, 2, 2, 1, 2, 2, 4, 4], then the beauty
of that necklace is equal to 3, since the beads with IDs 7, 8 and 1 have color 4, and form the longest
contiguous subsequence with the same color.
During the next Q days, Alice will get a new bead. The ID of the new bead gotten in the i-th day will be
N + i and its color will be di . She can perform one of the two possible actions:
1. Given two IDs ai and bi of beads that are currently connected, she cuts a thread that connects
them, inserts the new bead between the beads with ID ai and bi , and connects each of them to the
new bead, so the beads ai and bi become the neighbors of the new bead.
2. Given one ID ai , she removes the bead with ID ai from its current necklace, connect its previous
neighbors with a thread, and creates a new necklace consisting of just two beads: the bead with ID
ai and the new bead. Note that after this operation, Alice will have more than one necklace. It is
guaranteed that after this operation, all of the necklaces have length greater or equal to 2.
After the daily operation, Alice wants to know what is the beauty of the necklace where the new bead
was inserted, and the beauty of the most beautiful necklace that Alice has at the moment.
Input
The first line contains two integers N and Q (2 ≤ N ≤ 105 and 1 ≤ Q ≤ 105 ).
The second line contains N integers c1 , c2 , . . . , cN (1 ≤ ci ≤ 2 ∗ 105 ).
The following Q lines contain the description of the operations that Alice will perform. The (i + 2)-th line
begins with two integers ti and di (ti ∈ 1, 2 and 1 ≤ di ≤ 2 ∗ 105 ), then some integers follow depending on
the operation:
1. If ti = 1, then Alice performs the first operation, and two distinct integers ai and bi follow
(1 ≤ ai , bi < N + i). It is guaranteed that the beads with ID ai and bi are neighbors right before
the operation.
2. If ti = 2, then Alice performs the second operation, and one integer ai follows (1 ≤ ai < N + i).
Output
For each operation, print a line with two space-separated integers. The first integer is the beauty of the
necklace where the new bead is inserted and the second integer is the beauty of the most beautiful necklace
that Alice has at the moment.
Page 1 of 2
Examples
standard input standard output
8 3 3 3
4 2 2 1 2 2 4 4 1 4
1 3 1 2 5 5
2 4 4
1 2 9 2
7 5 1 3
1 1 2 2 3 3 1 1 2
2 4 4 1 2
2 5 2 2 2
2 6 6 3 3
1 5 2 9
1 5 2 9
Note
Note that, if a necklace has length equal to 2, the two beads are connected by two threads. For example,
if we consider the color 1 as blue and the color 5 as orange, then the necklace formed by the beads with
IDs 2 and 9 after the second operation looks like this:
Page 2 of 2