This page is still under construction.

Parts of this page are still being built. What you see may change.

Jousting Tournament

Time limit1sMemory limit256 MB

Summary
Given the starting order of N-1 knights and C fixed round intervals, find the smallest insertion position for a late knight with skill R that maximizes the number of rounds it wins.
Level

Hard8 of 10

Topics
Array, Simulation, Binary search, Segment tree
Solved
No attempts yet

Problem

A jousting tournament is held with NN knights. The knights first stand in a single line and, from the front, are numbered 00 to N−1N-1 in the order they stand.

A round begins by announcing two positions SS and EE with 0≤S<E≤(current line length)−10 \le S < E \le (\text{current line length}) - 1. Every knight currently standing at positions SS through EE (inclusive) competes, and exactly one of them wins. The winner goes back into the line while the losers drop out; the remaining knights then move toward position 00, keeping their relative order, to close the gaps, so the winner ends up at position SS. The line is thus renumbered 00 to (previous length) −(E−S)−1-(E-S)-1. The next round works the same way, and rounds continue until a single knight is left.

Every knight has a distinct skill, given as an integer from 00 to N−1N-1 (a larger value means a stronger knight). The position ranges of all CC rounds are known in advance, and in every round the competitor with the highest skill always wins.

Of the NN knights, N−1N-1 have already arrived and are standing in line; only the most popular knight has not arrived yet. The late knight has skill RR. To make the festival as exciting as possible, we want to insert this knight at the position that maximizes the number of rounds it wins. Rounds in which the late knight does not take part are irrelevant — only the rounds it enters and wins are counted.

For example, if the current line has skills [1,3,0,2,4][1, 3, 0, 2, 4] and a round announces (S,E)=(0,2)(S, E) = (0, 2), then the knights at positions 0,1,20, 1, 2 (skills 1,3,01, 3, 0) compete, the skill-33 knight wins, and the line becomes [3,2,4][3, 2, 4].

You are given:

  • NN: the total number of knights (1≤N≤100,0001 \le N \le 100{,}000).
  • CC: the number of rounds (1≤C≤N−11 \le C \le N-1).
  • RR: the skill of the late knight. All skills, including RR, are distinct integers from 00 to N−1N-1.
  • KK: an array of N−1N-1 integers, the skills of the already-present knights in line order.
  • SS and EE: arrays of length CC. For each ii with 0≤i≤C−10 \le i \le C-1, round i+1i+1 involves the knights currently at positions S[i]S[i] through E[i]E[i]. It is guaranteed that S[i]<E[i]S[i] < E[i], that E[i]E[i] is smaller than the number of knights in line when that round starts, and that exactly one knight remains after all CC rounds.

Find the best position PP (0≤P≤N−10 \le P \le N-1) at which to insert the late knight so that the number of rounds it wins is as large as possible. If several positions are optimal, choose the smallest. Here PP is the late knight's position after insertion, i.e. the number of knights standing in front of it: P=0P = 0 places it at the very front, and P=N−1P = N-1 places it at the very back.

Input

The first line contains NN, CC, and RR. Each of the next N−1N-1 lines contains one value K[i]K[i]. Each of the following CC lines contains S[i]S[i] and E[i]E[i].

Output

Print PP, the smallest optimal insertion position for the late knight, on a single line.

Examples5

  1. Example 1

    Input
    5 3 3
    1
    0
    2
    4
    1 3
    0 1
    0 1
    
    Expected output
    1
    
  2. Example 2

    Input
    6 4 5
    4
    0
    3
    1
    2
    4 5
    1 2
    0 1
    0 2
    
    Expected output
    1
    
  3. Example 3

    Input
    6 4 0
    1
    3
    4
    5
    2
    4 5
    1 2
    0 1
    0 2
    
    Expected output
    0
    
  4. Example 4

    Input
    2 1 0
    1
    0 1
    
    Expected output
    0
    
  5. Example 5

    Input
    7 1 3
    6
    0
    1
    2
    5
    4
    0 6
    
    Expected output
    0