CF848C Goodbye Souvenir
Description
I won't feel lonely, nor will I be sorrowful... not before everything is buried.
A string of $ n $ beads is left as the message of leaving. The beads are numbered from $ 1 $ to $ n $ from left to right, each having a shape numbered by integers between $ 1 $ and $ n $ inclusive. Some beads may have the same shapes.
The memory of a shape $ x $ in a certain subsegment of beads, is defined to be the difference between the last position and the first position that shape $ x $ appears in the segment. The memory of a subsegment is the sum of memories over all shapes that occur in it.
From time to time, shapes of beads change as well as the memories. Sometimes, the past secreted in subsegments are being recalled, and you are to find the memory for each of them.
Input Format
The first line of input contains two space-separated integers $ n $ and $ m $ ( $ 1
Output Format
For each query, print one line with an integer — the memory of the recalled subsegment.
Explanation/Hint
The initial string of beads has shapes $ (1,2,3,1,3,2,1) $ .
Consider the changes and queries in their order:
1. 2 3 7: the memory of the subsegment $ [3,7] $ is $ (7-4)+(6-6)+(5-3)=5 $ ;
2. 2 1 3: the memory of the subsegment $ [1,3] $ is $ (1-1)+(2-2)+(3-3)=0 $ ;
3. 1 7 2: the shape of the $ 7 $ -th bead changes into $ 2 $ . Beads now have shapes $ (1,2,3,1,3,2,2) $ respectively;
4. 1 3 2: the shape of the $ 3 $ -rd bead changes into $ 2 $ . Beads now have shapes $ (1,2,2,1,3,2,2) $ respectively;
5. 2 1 6: the memory of the subsegment $ [1,6] $ is $ (4-1)+(6-2)+(5-5)=7 $ ;
6. 2 5 7: the memory of the subsegment $ [5,7] $ is $ (7-6)+(5-5)=1 $ .