Best Student
Time limit1.2sMemory limit1024 MB
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 days, the school wants to know which student was selected as the best student most often during a period of days from to . The school plans to award that student a gift.
Given a list of the best students for consecutive days and queries , write a program to find the best student selected most often during the period for each query .
Input
Your program is to read from standard input. The input starts with a line containing two integers, and , representing the number of days and the number of queries, respectively, where and . Students have unique id numbers between and . The next line consists of positive integers representing id numbers for best students, ordered from day to day . Each of the following lines consists of two positive integers, and , that represent a query , where is the period of days from to . Note that for .
Output
Your program is to write to standard output. Print exactly lines. The -th line should contain the id number of the student selected most often as best student during the -th period . When there are more than one such students, the program should print the largest one among their id numbers.