Couriers
Time limit3sMemory limit512 MB
Given the courier numbers in shipment order, report the courier that appears more than half the time in each query interval, or 0 if none does.
- Level
Medium7 of 10
- Topics
- Segment tree, Binary search
- Solved
- No attempts yet
Problem
Byteasar works for BAJ, a company that sells computer games. BAJ has contracts with several courier companies, and they deliver the games BAJ sells to its customers. Byteasar is checking how those contracts are being kept. He has a log of the packages BAJ shipped, in chronological order, with the number of the courier company that delivered each package.
If one courier company delivered more than half of the packages shipped during some period, we say that it dominated that period. Byteasar wants to know, for each period he is interested in, whether a courier company dominated it, and which one.
Write a program that finds the dominating courier company for each period, or reports that there is none.
Input
The first line contains two integers and , separated by a single space (): the number of packages BAJ shipped and the number of periods to examine. The courier companies are numbered from to at most .
The second line contains integers , separated by single spaces (). The value is the number of the courier company that delivered the -th package in shipment order.
Each of the next lines describes one period with two integers and , separated by a single space (). The period runs from the -th shipped package to the -th shipped package, both included.
Output
Print one line per period, lines in total. Each line holds the number of the courier company that dominated the corresponding period, or if no company dominated it.