TwilioOA #1
Count Groups
EasyJudge-ready
Topics (AI-labelled)Prefix & RangesHashing
You are given the numeric label of each cable. Within each independent query range, only two cables with the same label can form a group, and each cable can be used at most once. Find the maximum number of groups that can be formed in the range. What follows is this site's standard input/output protocol.
Input
The first line contains n and q; the second line contains n labels; each of the next q lines contains l and r, a 1-based closed range. Queries are independent of each other.
Output
Output the q answers in input order, separated by spaces. Each answer is the sum, over all labels, of the label's number of occurrences in the range divided by 2 and rounded down.
Example 1
Input:
4 2
2 3 4 2
1 4
3 4
Output:
1 0
Example 2
Input:
4 1
2 2 2 2
1 4
Output:
2
Example 3
Input:
1 1
1000000000
1 1
Output:
0
Example notes
Use Mo's algorithm to move the range endpoints, maintaining the frequency of each label and the number of groups formed.
Editorial
Loading solution…