CF1514D.Cut and Stick

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Baby Ehab has a piece of Cut and Stick with an array aa of length nn written on it. He plans to grab a pair of scissors and do the following to it:

  • pick a range (l,r)(l, r) and cut out every element ala_l , al+1a_{l + 1} , ..., ara_r 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 ala_l , al+1a_{l + 1} , ..., ara_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length xx , then no value occurs strictly more than x2\lceil \frac{x}{2} \rceil times in it.

He didn't pick a range yet, so he's wondering: for qq ranges (l,r)(l, r) , what is the minimum number of pieces he needs to partition the elements ala_l , al+1a_{l + 1} , ..., ara_r into so that the partitioning is beautiful.

A sequence bb is a subsequence of an array aa if bb can be obtained from aa by deleting some (possibly zero) elements. Note that it does not have to be contiguous.

输入格式

The first line contains two integers nn and qq ( 1n,q31051 \le n,q \le 3 \cdot 10^5 ) — the length of the array aa and the number of queries.

The second line contains nn integers a1a_1 , a2a_2 , ..., ana_{n} ( 1ain1 \le a_i \le n ) — the elements of the array aa .

Each of the next qq lines contains two integers ll and rr ( 1lrn1 \le l \le r \le 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 66 , and no value occurs more than 33 times in it.

In the second query, the elements of the query range are [3,2,3,3][3,2,3,3] . You can't put them all in one subsequence, since its length is 44 , and 33 occurs more than 22 times. However, you can partition it into two subsequences: [3][3] and [2,3,3][2,3,3] .

首页