Codeforces Round 716 (Div. 2)
D
Cut and Stick
Codeforces
2000
原题
← CE →
翻译中…

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 $a_l$, $a_{l + 1}$, ..., $a_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 $a_l$, $a_{l + 1}$, ..., $a_r$ 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 $\lceil \frac{x}{2} \rceil$ 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 $a_l$, $a_{l + 1}$, ..., $a_r$ 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.

翻译中…

Input

翻译中…

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

翻译中…

The second line contains $n$ integers $a_1$, $a_2$, ..., $a_{n}$ ($1 \le a_i \le n$) — the elements of the array $a$.

翻译中…

Each of the next $q$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the range of this query.

翻译中…

Output

翻译中…

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.

翻译中…

Note

翻译中…

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]$.

翻译中…

样例

Input #1
6 2
1 3 2 3 3 2
1 6
2 5
Output #1
1
2
InputCopy OutputCopy
题目信息
题号
1514D
时间限制
3 s
内存限制
512 MB
标签
二分
数据结构
贪心
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。