Hacker Cups and Balls
Time limit3sMemory limit512 MB
Given a permutation and range sort operations that go ascending or descending depending on whether l < r, find the value in the middle cup at the end.
- Level
Hard8 of 10
- Topics
- Binary search, Segment tree, Sorting, Implementation
- Solved
- No attempts yet
Problem
The dark side of Dreamoon, Ademnoor, wants to play an interesting game with you. Have you heard of the game "cups and balls"? Here is the hacker version of it!
There are cups and balls, both numbered . At every moment, each cup holds exactly one ball. Initially, the -th ball is placed in the -th cup. Admenoor performs magic operations on these balls and cups. The -th operation sorts all the balls in the cups numbered between and , inclusive. The sorting can be done in ascending order or descending order. After these operations, you must answer which ball is placed in the center cup. We guarantee that is an odd integer, so the center cup means the -th cup.
For example, consider , and . If the first operation is to sort the balls in the cups numbered between and in ascending order, then becomes . If the second operation is to sort the balls in the cups numbered between and in descending order, then becomes . In this example, the number of the ball in the center cup after all operations is .
Input
The first line of input contains two integers and . The following line contains integers . Each of the following lines contains two integers and . If , Admenoor sorts the balls in ascending order in this operation; otherwise, the balls are sorted in descending order in this operation.
Output
Output a single line with the number of the ball in the center cup after all operations.
Constraints
- is an odd integer
- is a permutation of