Most Influential Pumpkin
Time limit5sMemory limit256 MB
After each range increment on an odd-length array, report the median value of the array.
- Level
Medium7 of 10
- Topics
- Segment tree, Binary search, Sorting
- Solved
- No attempts yet
Problem
The pumpkins in Hagrid's garden have come to life. They walk, they talk, they date, and of course they hold elections to choose the Head Pumpkin. Guessing the winner is easy. Line all the pumpkins up sorted by size, and the pumpkin standing exactly in the middle becomes the Head Pumpkin.
Hagrid wants no fuss in his garden, so he wants to know who the Head Pumpkin is. When several pumpkins share the same size he cannot tell which one of them holds the title, but knowing the size of the Head Pumpkin is enough for him.
The pumpkins grow in a row, numbered from 1 to . Hagrid often waters a group of pumpkins that grow next to each other. He picks two numbers and and waters every pumpkin from the -th to the -th. Each watered pumpkin grows by exactly 1. A new election follows every watering, so the size of the Head Pumpkin changes. Report the size of the Head Pumpkin after each watering Hagrid does.
Input
The input holds several test cases.
The first line of a test case has two integers and (). is always odd.
The next line has integers (), the initial sizes of the pumpkins.
Each of the next lines has two integers and (), meaning that Hagrid watered every pumpkin numbered from to .
The sum of over all test cases is at most 60000, and the sum of over all test cases is at most 60000.
The last line of the input has two zeros. That line is not a test case and must not be processed.
Output
For each test case print lines. The -th of them holds the size of the Head Pumpkin right after the -th watering. Since is odd, that size is the -th smallest size in the row.