102426: [AtCoder]ABC242 G - Range Pairing Query

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

$N$ people numbered $1,2,\dots,N$ are standing in a row. Person $i$ wears Color $A_i$.

Answer $Q$ queries of the format below.

  • You are given integers $l$ and $r$. Considering only Person $l,l+1,\dots,r$, how many pairs of people wearing the same color can be formed at most?

Constraints

  • All values in input are integers.
  • $1 \le N \le 10^5$
  • $1 \le Q \le 10^6$
  • $1 \le A_i \le N$
  • $1 \le l \le r \le N$ in each query.

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\dots$ $A_N$
$Q$
$\mathrm{Query}_1$
$\mathrm{Query}_2$
$\vdots$
$\mathrm{Query}_Q$

Here, $\mathrm{Query}_i$ represents the $i$-th query.

Each query is in the following format:

$l$ $r$

Output

Print $Q$ lines. The $i$-th line should contain the answer for the $i$-th query as an integer. The use of fast input and output methods is recommended because of potentially large input and output.


Sample Input 1

10
1 2 3 2 3 1 3 1 2 3
6
6 10
5 8
3 6
4 4
1 6
1 10

Sample Output 1

2
2
1
0
3
4

We have $A=(1,2,3,2,3,1,3,1,2,3)$. This input contains six queries.

The first query is $(l, r) = (6, 10)$. By pairing Person $6, 8$ and paring Person $7, 10$, we can form two pairs of people wearing the same color.

The second query is $(l, r) = (5, 8)$. By pairing Person $5, 7$ and paring Person $6, 8$, we can form two pairs of people wearing the same color.

There can be a query where $l=r$.

Input

题意翻译

给出长度为 $N$ 的数列 $A_i$,表示第 $i$ 个人衣服颜色为 $A_i$。有 $M$ 次询问 $[l,r]$ 区间最多能组成多少对衣服颜色相同的人。

Output

得分:600分

问题描述

$N$个编号为$1,2,\dots,N$的人站成一排。第$i$个人穿着颜色$A_i$。

回答$Q$个查询,格式如下。

  • 给你整数$l$和$r$。只考虑第$l,l+1,\dots,r$个人,最多可以形成多少对穿相同颜色衣服的人?

约束

  • 输入中的所有值都是整数。
  • $1 \le N \le 10^5$
  • $1 \le Q \le 10^6$
  • $1 \le A_i \le N$
  • 每个查询中,$1 \le l \le r \le N$。

输入

输入从标准输入中给出,格式如下:

$N$
$A_1$ $A_2$ $\dots$ $A_N$
$Q$
$\mathrm{Query}_1$
$\mathrm{Query}_2$
$\vdots$
$\mathrm{Query}_Q$

其中,$\mathrm{Query}_i$表示第$i$个查询。

每个查询的格式如下:

$l$ $r$

输出

输出$Q$行。 第$i$行应包含第$i$个查询的答案,即一个整数。 由于输入和输出可能很大,因此建议使用快速输入和输出方法。


样例输入1

10
1 2 3 2 3 1 3 1 2 3
6
6 10
5 8
3 6
4 4
1 6
1 10

样例输出1

2
2
1
0
3
4

我们有$A=(1,2,3,2,3,1,3,1,2,3)$。这个输入包含了六个查询。

第一个查询是$(l, r) = (6, 10)$。通过将第6人和第8人配对,将第7人和第10人配对,我们可以形成两对穿相同颜色衣服的人。

第二个查询是$(l, r) = (5, 8)$。通过将第5人和第7人配对,将第6人和第8人配对,我们可以形成两对穿相同颜色衣服的人。

可能出现$l=r$的查询。

加入题单

算法标签: