Haybale Stacking
Time limit1sMemory limit128 MB
Add one bale to every stack in each given range, then report the median height among all N stacks.
- Level
Medium4 of 10
- Topics
- Prefix sum, Array, Sorting, Implementation
- Solved
- No attempts yet
Problem
Feeling sorry for all the mischief she has caused around the farm recently, Bessie has agreed to help Farmer John stack up an incoming shipment of hay bales.
She starts with empty stacks, numbered through (, and is odd). Farmer John then gives her a sequence of instructions (), each of the form " ", meaning that Bessie should add one new hay bale to the top of every stack in the range . For example, if Bessie is told " ", she adds one hay bale to each of the stacks , , , and .
After Bessie finishes following all of the instructions, Farmer John would like to know the median height of his stacks — that is, the height of the middle stack when the stacks are arranged in sorted order. Since is odd, this stack is unique. Please help Bessie determine the answer.
Input
- Line 1: Two space-separated integers, and .
- Lines : Each line contains one instruction as two space-separated integers ().
Output
- A single line containing the median stack height after Bessie completes all instructions.
Hint
Consider the case with stacks and instructions. After all instructions are processed, the stack heights are . Sorting them gives , and the middle (fourth) value is , so the median is .