Elections
Time limit2sMemory limit1024 MB
Given n candidate votes and m range queries, report a candidate with the highest vote count inside each range [l, r].
- Level
Medium7 of 10
- Topics
- Sliding window, Sorting, Array
- Solved
- No attempts yet
Problem
Recently the country held an election that used modern technology. For each ballot, it is known when it was put into the box and which candidate the voter chose. After the votes were counted, everyone wanted to analyze how the election went. Why else would the whole system have been introduced?
Many people wanted to know the following: for a given time interval, which candidate received the most votes? If a candidate got many votes in a short interval and fewer outside it, fraud may have happened during that interval. A site was built to answer such questions, but the number of visitors was unexpectedly large, and the site could not keep up with the load.
The management decided to rewrite the site so that it serves users at the same time. Your task is to write a program that answers the question users ask: which candidate received the most votes in a given interval?
Input
The first line contains the number of processed ballots ().
The second line contains integers separated by spaces (). Here is the number of the candidate chosen by the voter who put ballot number into the box.
The third line contains the number of queries ().
Each of the next lines contains a query ().
Output
For each query, print one number on a separate line: the number of the most popular candidate among the ballots numbered from to , inclusive.
A candidate is the most popular if no other candidate received more votes among those ballots. Ties are allowed.
Hint
For the last query of the sample, the answer 1 is also accepted. Ololo, partial solutions.