Under construction

Still being built. Things may change

Hacker Cups and Balls

Time limit3sMemory limit512 MB

Summary
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 nn cups and nn balls, both numbered 1,2,…,n1, 2, \ldots, n. At every moment, each cup holds exactly one ball. Initially, the aia_i-th ball is placed in the ii-th cup. Admenoor performs mm magic operations on these balls and cups. The ii-th operation sorts all the balls in the cups numbered between lil_i and rir_i, inclusive. The sorting can be done in ascending order or descending order. After these mm operations, you must answer which ball is placed in the center cup. We guarantee that nn is an odd integer, so the center cup means the n+12\frac{n + 1}{2}-th cup.

For example, consider n=5n = 5, m=2m = 2 and a=[5,1,4,2,3]a = [5, 1, 4, 2, 3]. If the first operation is to sort the balls in the cups numbered between 11 and 44 in ascending order, then aa becomes [1,2,4,5,3][1, 2, 4, 5, 3]. If the second operation is to sort the balls in the cups numbered between 22 and 55 in descending order, then aa becomes [1,5,4,3,2][1, 5, 4, 3, 2]. In this example, the number of the ball in the center cup after all operations is 44.

Input

The first line of input contains two integers nn and mm. The following line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n. Each of the following mm lines contains two integers lil_i and rir_i. If li<ril_i < r_i, 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

  • 1≤n≤99 9991 \le n \le 99\,999
  • nn is an odd integer
  • 0≤m≤1050 \le m \le 10^5
  • 1≤ai≤n1 \le a_i \le n
  • ⟨ai⟩\langle a_i \rangle is a permutation of 1,2,…,n1, 2, \ldots, n
  • 1≤li,ri≤n1 \le l_i, r_i \le n

Examples2

  1. Example 1

    Input
    3 2
    1 3 2
    1 3
    3 1
    
    Expected output
    2
    
  2. Example 2

    Input
    5 2
    5 1 4 2 3
    1 4
    5 2
    
    Expected output
    4