This page is still under construction.

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

Best Student

Time limit1.2sMemory limit1024 MB

Summary
For each query interval in an array of student ids, print the id that occurs most often, breaking ties by the largest id.
Level

Hard8 of 10

Topics
Divide and conquer, Segment tree, Hash map, Binary search
Solved
No attempts yet

Problem

The SY School selects a best student every day. Given a list of the best students for nn days, the school wants to know which student was selected as the best student most often during a period [S,E][S, E] of days from SS to EE. The school plans to award that student a gift.

Given a list of the best students for nn consecutive days and qq queries {(S1,E1),…,(Sq,Eq)}\{(S_1, E_1), \dots, (S_q, E_q)\}, write a program to find the best student selected most often during the period [Si,Ei][S_i, E_i] for each query (Si,Ei)(S_i, E_i).

Input

Your program is to read from standard input. The input starts with a line containing two integers, nn and qq, representing the number of days and the number of queries, respectively, where 1≤n≤100,0001 \le n \le 100,000 and 1≤q≤100,0001 \le q \le 100,000. Students have unique id numbers between 11 and 10910^9. The next line consists of nn positive integers representing nn id numbers for best students, ordered from day 11 to day nn. Each of the following qq lines consists of two positive integers, SiS_i and EiE_i, that represent a query (Si,Ei)(S_i, E_i), where [Si,Ei][S_i, E_i] is the period of days from SiS_i to EiE_i. Note that 1≤Si≤Ei≤n1 \le S_i \le E_i \le n for i=1,…,qi = 1, \dots, q.

Output

Your program is to write to standard output. Print exactly qq lines. The ii-th line should contain the id number of the student selected most often as best student during the ii-th period [Si,Ei][S_i, E_i]. When there are more than one such students, the program should print the largest one among their id numbers.

Examples2

  1. Example 1

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

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