Exhibition 3

시간 제한3초메모리 제한2048 MB

요약
주어진 구간들의 구간 최댓값 수열이 사전순으로 최대가 되도록 배열을 재배치하고, 그때의 각 구간 최댓값을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 정렬, 구간, 구현
정답자
아직 제출이 없습니다

문제

The JOI Art Museum is planning to hold an exhibition of paintings soon. The museum owns NN paintings numbered from 11 to NN, and the beauty of painting ii (1≤i≤N1 ≤ i ≤ N) is given as A_iA\_i. For the exhibition, these paintings will be arranged in a single row from left to right, but the order in which they are placed has not yet been determined.

There will be MM magazines covering the exhibition. These magazines are numbered from 11 to MM in descending order of their influence. Each magazine will publish photographs of a certain contiguous segment of paintings in the arranged row. Specifically, magazine jj (1≤j≤M1 ≤ j ≤ M) will publish photographs of the L_j,L_j+1,…,R_jL\_j , L\_j + 1, \dots , R\_j-th paintings from the left in the row. The appeal of the article by magazine jj (1≤j≤M1 ≤ j ≤ M) is defined as the maximum beauty among the paintings it covers.

JOI-kun, the director of the JOI Art Museum, aims to arrange the paintings in a way that allows these magazines to write articles with greater appeal, thereby attracting more people to the exhibition. Since magazines with greater influence reach a larger audience, he wants to prioritize increasing the appeal of articles in more influential magazines. More precisely, let b_jb\_j be the appeal of the article published by magazine jj (1≤j≤M1 ≤ j ≤ M), then JOI-kun wants to arrange the paintings so that the sequence b=(b_1,b_2,…,b_M)b = (b\_1, b\_2, \dots , b\_M) is lexicographically maximized. Here, for two distinct sequences b=(b_1,b_2,…,b_M)b = (b\_1, b\_2, \dots , b\_M) and b′=(b′_1,b′_2,…,b′_M)b' = (b'\_1, b'\_2, \dots , b'\_M), bb is said to be lexicographically larger than b′b' when, for the smallest index kk (1≤k≤M1 ≤ k ≤ M) such that b_k≠b′_kb\_k \ne b'\_k, b_k>b′_kb\_k > b'\_k holds.

Write a program which, given the information of the paintings to be exhibited and the magazines covering the event, calculates the appeal of each magazine’s article when the paintings are arranged to maximize the lexicographical order of sequence b=(b_1,b_2,…,b_M)b = (b\_1, b\_2, \dots , b\_M).

입력

Read the following data from the standard input.

NN MM

A_1A\_1 A_2A\_2 ⋯\cdots A_NA\_N

L_1L\_1 R_1R\_1

L_2L\_2 R_2R\_2

⋮\vdots

L_ML\_M R_MR\_M

출력

Write MM lines to the standard output. The jj-th line (1≤j≤M1 ≤ j ≤ M) of the output should contain b_jb\_j, the appeal of the article published by magazine jj. Here, the sequence b=(b_1,b_2,…,b_M)b = (b\_1, b\_2, \dots , b\_M) must be lexicographically maximized.

제한

  • 1≤N≤100,0001 ≤ N ≤ 100\\, 000.
  • 1≤M≤100,0001 ≤ M ≤ 100\\, 000.
  • 1≤A_i≤N1 ≤ A\_i ≤ N (1≤i≤N1 ≤ i ≤ N).
  • 1≤L_j≤R_j≤N1 ≤ L\_j ≤ R\_j ≤ N (1≤j≤M1 ≤ j ≤ M).
  • Given values are all integers.

예제3

  1. 예제 1

    입력
    4 4
    1 2 1 2
    1 1
    2 3
    4 4
    3 4
    
    예상 출력
    2
    2
    1
    2
    
  2. 예제 2

    입력
    4 8
    1 2 3 4
    1 2
    2 3
    4 4
    1 1
    2 4
    3 3
    3 3
    4 4
    
    예상 출력
    4
    4
    3
    2
    4
    1
    1
    3
    
  3. 예제 3

    입력
    12 10
    6 2 2 5 2 5 2 3 3 3 2 2
    3 5
    10 12
    12 12
    2 4
    8 9
    10 11
    1 3
    7 9
    9 10
    10 11
    
    예상 출력
    6
    5
    5
    6
    5
    3
    6
    5
    5
    3