CF1514D.Cut and Stick
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
- pick a range (l,r) and cut out every element al , al+1 , ..., ar in this range;
- stick some of the elements together in the same order they were in the array;
- end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence al , al+1 , ..., ar into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x , then no value occurs strictly more than ⌈2x⌉ times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l,r) , what is the minimum number of pieces he needs to partition the elements al , al+1 , ..., ar into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
输入格式
The first line contains two integers n and q ( 1≤n,q≤3⋅105 ) — the length of the array a and the number of queries.
The second line contains n integers a1 , a2 , ..., an ( 1≤ai≤n ) — the elements of the array a .
Each of the next q lines contains two integers l and r ( 1≤l≤r≤n ) — the range of this query.
输出格式
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
输入输出样例
输入#1
6 2 1 3 2 3 3 2 1 6 2 5
输出#1
1 2
说明/提示
In the first query, you can just put the whole array in one subsequence, since its length is 6 , and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3] . You can't put them all in one subsequence, since its length is 4 , and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3] .