Cups and Marbles
Time limit4sMemory limit256 MB
After m range-sort spells (ascending or descending) on a permutation, report the marble in the middle cup.
- Level
Hard8 of 10
- Topics
- Binary search, Sorting, Implementation, Segment tree
- Solved
- No attempts yet
Problem
Hongjun and Myungwoo like to play. Bored one day, Hongjun invented the following game.
There are cups and marbles, each numbered from 1 to . Hongjun put one marble into every cup and lined the cups up in order of their numbers. At the start, cup holds marble .
Hongjun casts a spell times. One cast takes every cup lying between two chosen cups, sorts the marbles in those cups by number in ascending or descending order, and puts them back one per cup, starting from the lowest numbered cup.
Once every spell is done, Hongjun asks Myungwoo which marble sits in cup . is always odd.
Consider , and . If the first spell sorts the marbles in cups 1 through 4 in ascending order, becomes . If the second spell sorts the marbles in cups 2 through 5 in descending order, becomes . Cup 3 is left holding marble 4.
Help Myungwoo and write a program that answers Hongjun's question.
Input
The first line contains two integers and . (, ) is odd.
The second line contains integers . () is a permutation of through .
Each of the next lines holds one spell, in the order Hongjun casts them. A line contains two integers and . () If , the marbles in cups through are sorted by number in ascending order. If , the marbles in cups through are sorted by number in descending order.
Output
Print the number of the marble in cup after all spells.