308410: CF1514D. Cut and Stick

Memory Limit:512 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Cut and Stick

题意翻译

#### 题意 给定一个长度为 $n$ 的序列 $(n\le3\times10^5)$,可以对其进行三种操作: 1. 把一段区间片段中所有的数从原来的区间里剪下来 2. 把这些数按照在原来序列里的排列顺序重新拼接成序列 3. 最后形成一个或者多个片段的序列,使最开始的序列中每一个数都属于某一个片段。 要求:给定 $q$ 个询问 $(q\le3\times10^5)$ ,每次给出一个区间的左右端点,将这个区间分成若干个片段,使得每个片段内任意元素出现的次数不严格大于 $\lceil\frac{x}{2}\rceil$ ($x$ 为该片段长度)。求可以分成的最少片段数目。 #### 输入 第一行输入两个整数 $n$ 和 $p$。 第二行输入 $n$ 个整数,对应在原序列中的值。 第 $3\sim q+2$ 行,每行输入两个整数,分别表示询问的区间左端点和右端点。 #### 输出 对于每个询问,输出一个整数,表示询问区间分成的最少片段。

题目描述

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.

输入输出格式

输入格式


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.

输出格式


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

Input

题意翻译

#### 题意 给定一个长度为 $n$ 的序列 $(n\le3\times10^5)$,可以对其进行三种操作: 1. 把一段区间片段中所有的数从原来的区间里剪下来 2. 把这些数按照在原来序列里的排列顺序重新拼接成序列 3. 最后形成一个或者多个片段的序列,使最开始的序列中每一个数都属于某一个片段。 要求:给定 $q$ 个询问 $(q\le3\times10^5)$ ,每次给出一个区间的左右端点,将这个区间分成若干个片段,使得每个片段内任意元素出现的次数不严格大于 $\lceil\frac{x}{2}\rceil$ ($x$ 为该片段长度)。求可以分成的最少片段数目。 #### 输入 第一行输入两个整数 $n$ 和 $p$。 第二行输入 $n$ 个整数,对应在原序列中的值。 第 $3\sim q+2$ 行,每行输入两个整数,分别表示询问的区间左端点和右端点。 #### 输出 对于每个询问,输出一个整数,表示询问区间分成的最少片段。

加入题单

算法标签: