Maintain a line of fans with club labels under deletions and range-count queries, where each query counts the maximal same-club run around an element.
Hard8Linked listUnion-findArrayImplementationNo attempts yetTime limit2.5sMemory limit256 MBWookje has K fan clubs. N fans came to the fan meeting he holds to celebrate becoming an adult. The fans receive registration numbers 1 through N and stand in one line in that order. The fan with registration number i belongs to fan club Ai.
With nothing else to do, Wookje decides to perform Q actions. There are two kinds of actions.
After finishing the Q actions Wookje got fed up, shouted "everyone worse at algorithms than me, get out", and ended the fan meeting. He wants to know how many gifts he gave to the fans. Help him.
The first line contains K and N. (1≤K,N≤106)
The second line contains A1,A2,…,AN. (1≤Ai≤K)
The third line contains Q. (0≤Q≤3×106)
Each of the next Q lines contains integers a and b. (1≤a≤2, 1≤b≤N)
If a is 1, action 1 is applied to the fan with registration number b. The same fan is never thrown out twice.
If a is 2, action 2 is applied with the fan with registration number b as the center. A fan who is already out is never used as the center.
Print the total number of gifts Wookje gave to the fans after the Q actions. Wookje has infinitely many gifts, so he never fails to give one because he ran out.
In the first example the fans belong to clubs 1, 1, 2, 3, 1 in order.
At the first action 2, registration numbers 1 and 2 receive gifts. (1-1-2-3-1)
At the second action 2, registration numbers 3 and 4 are already out, so registration numbers 1, 2, and 5 receive gifts. (1-1-1)
At the last action 2, registration number 2 is out as well, so registration numbers 1 and 5 receive gifts. (1-1)