Daunting device
Time limit1sMemory limit1024 MB
Apply N range-recolor operations whose endpoints depend on the current count of a query color, then report the highest cell frequency.
- Level
Hard8 of 10
- Topics
- Segment tree, Implementation, Sorting, Math
- Solved
- No attempts yet
Problem
At an excavation site on the Caribbean island of Saint Basil you found an old device together with a set of instructions that reads like a puzzle. Your local guide Vibenas says that solving the puzzle makes the device point to the place where the pirate Lyerpes hid a treasure.
The device holds a tape of cells indexed from to . Each cell carries one color, and a command sent to the device changes that color. Colors are written as integers, and every cell starts with the same color.
The instructions list steps to perform before the device shows the way. Each step is given by four integers , , and . To perform a step, first count the cells whose current color is . Call that count , then compute the two values
Finally, change every cell in the closed interval to color .
After all steps, pick a color that appears the greatest number of times on the tape and report how many cells carry it. That number is the same even when several colors tie for the greatest count.
Input
The first line contains three integers , and (), which are the number of cells on the tape, the number of available colors, and the number of steps. Colors are the distinct integers from to , and every cell starts with color .
Each of the next lines describes one step with four integers , , and (, ). is the color whose cell count decides the range of the step, is the color the cells in the range must have after the step is performed, and and are used to compute the bounds of the range as described above. The steps are performed in the given order.
Output
Print one line with the number of cells that have a color appearing the greatest number of times on the tape after all steps have been performed.