CF1514D.Cut and Stick

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:512MB

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.

Baby Ehab 有一张“剪切与粘贴”(Cut and Stick)卡片,上面写有一个长度为 nn 的数组 aa。他打算拿起一把剪刀,对这张卡片执行以下操作:

  • 选定一个区间 (l,r)(l, r),并剪下该区间内所有元素 ala_l, al+1a_{l + 1}, ..., ara_r;
  • 将其中一部分元素按其在原数组中的相同顺序粘贴在一起;
  • 最终得到若干个片段,每个片段包含若干元素,且每个元素恰好属于某一个片段。

更形式化地说,他将序列 ala_l, al+1a_{l + 1}, ..., ara_r 划分为若干个子序列。他称一个划分是优美的(beautiful),当且仅当:对每个片段(即每个子序列),若其长度为 xx,则其中任意数值的出现次数均不超过 ⌈x2⌉\lceil \frac{x}{2} \rceil 次。

他尚未选定具体区间,因此他想知道:对于给定的 qq 个查询区间 (l,r)(l, r),将元素 ala_l, al+1a_{l + 1}, ..., ara_r 划分为优美划分时,所需的最少片段数是多少?

注意:序列 bb 是数组 aa 的一个子序列,当且仅当 bb 可通过从 aa 中删除若干(可能为零)个元素得到。注意子序列不必连续。

输入格式

The first line contains two integers nn and qq (1≤n,q≤3⋅1051 \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} (1≤ai≤n1 \le a_i \le n) — the elements of the array aa.

Each of the next qq lines contains two integers ll and rr (1≤l≤r≤n1 \le l \le r \le n) — the range of this query.

第一行包含两个整数 nn 和 qq(1≤n,q≤3⋅1051 \le n,q \le 3 \cdot 10^5)—— 分别表示数组 aa 的长度和查询次数。

第二行包含 nn 个整数 a1a_1, a2a_2, ..., ana_{n}(1≤ai≤n1 \le a_i \le n)—— 表示数组 aa 的元素。

接下来的 qq 行,每行包含两个整数 ll 和 rr(1≤l≤r≤n1 \le l \le r \le n)—— 表示该次查询的区间范围。

输出格式

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

在第一个查询中,你可以将整个数组放入一个子序列中,因为其长度为 66,且其中没有任何数值出现次数超过 33 次。

在第二个查询中,查询区间内的元素为 [3,2,3,3][3,2,3,3]。你不能将它们全部放入一个子序列中,因为该子序列长度为 44,而数值 33 的出现次数超过了 22 次。然而,你可以将其划分为两个子序列:[3][3] 和 [2,3,3][2,3,3]。

输入解题思路,AI测评打分。不知道怎么写?

首页