D - Range Count Query Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

長さ NN の数列 A=(A1,,AN)A=(A_1,\ldots,A_N) が与えられます。

以下の形式で与えられる QQ 個のクエリに答えてください。

  • 整数 L,R,XL,R,X が与えられる。 AL,,ARA_L, \ldots,A_R のうち、値が XX に等しいものの個数を求めよ。

制約

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1AiN1 \leq A_i \leq N
  • 1Q2×1051 \leq Q \leq 2\times 10^5
  • 各クエリについて、 1LRN,1XN1\le L \leq R \leq N, 1 \leq X \leq N
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN 
A1A_1 A2A_2 \ldots ANA_N
QQ
Query1\mathrm{Query}_1
Query2\mathrm{Query}_2
\vdots
QueryQ\mathrm{Query}_Q

ただし、Queryi\mathrm{Query}_iii 個目のクエリを表す。

各クエリは以下の形式で与えられる。

LL RR XX

出力

QQ 行出力せよ。ii 行目には、ii 個目のクエリに対する答えを出力せよ。


入力例 1Copy

Copy
5
3 1 4 1 5
4
1 5 1
2 4 3
1 5 2
1 3 3

出力例 1Copy

Copy
2
0
0
1

11 個目のクエリでは、 (A1,A2,A3,A4,A5)=(3,1,4,1,5)(A_1,A_2,A_3,A_4,A_5) =(3,1,4,1,5) のうち値が 11 に等しいものの個数は 22 です。

22 個目のクエリでは、 (A2,A3,A4)=(1,4,1)(A_2,A_3,A_4) =(1,4,1) のうち値が 33 に等しいものの個数は 00 です。

Score : 400400 points

Problem Statement

You are given a sequence of length NN: A=(A1,,AN)A=(A_1,\ldots,A_N).

Answer QQ queries given in the following format.

  • You are given integers LL, RR, and XX. Find the number of elements among AL,,ARA_L, \ldots, A_R whose values are equal to XX.

Constraints

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1AiN1 \leq A_i \leq N
  • 1Q2×1051 \leq Q \leq 2\times 10^5
  • 1LRN,1XN1\le L \leq R \leq N, 1 \leq X \leq N, for each query.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN 
A1A_1 A2A_2 \ldots ANA_N
QQ
Query1\mathrm{Query}_1
Query2\mathrm{Query}_2
\vdots
QueryQ\mathrm{Query}_Q

Here, Queryi\mathrm{Query}_i represents the ii-th query.

Each query is in the following format:

LL RR XX

Output

Print QQ lines, the ii-th of which contains the answer to the ii-th query.


Sample Input 1Copy

Copy
5
3 1 4 1 5
4
1 5 1
2 4 3
1 5 2
1 3 3

Sample Output 1Copy

Copy
2
0
0
1

In the first query, two of (A1,A2,A3,A4,A5)=(3,1,4,1,5)(A_1,A_2,A_3,A_4,A_5) =(3,1,4,1,5) have values equal to 11.

In the second query, zero of (A2,A3,A4)=(1,4,1)(A_2,A_3,A_4) =(1,4,1) have values equal to 33.



2025-04-25 (Fri)
10:25:09 +00:00