問題一覧 > 通常問題

No.404 Kadomatsu Subsequences

レベル : / AI推定難易度 : 1750 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 44
作問者 : りあん / テスター : anta
お気に入りにしたユーザー ProblemId : 1072 / 自分の提出
問題文最終更新日: 2026-07-01 08:27:09
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 143 (順位表) の他の問題:

Problem Statement

A Kadomatsu sequence is a sequence $A_1, A_2, A_3$ of $3$ elements that satisfies both of the following 2 conditions.

  • $A_1, A_2, A_3$ are all distinct
  • Among the $3$ elements, $A_2$ is the largest, or $A_2$ is the smallest

Among the subsequences of 3 elements of the given sequence (they need not be contiguous), we call those that are Kadomatsu sequences Kadomatsu subsequences.

For each query, find the number of Kadomatsu subsequences whose middle element (the $A_2$ in the description above) has a value between $L$ and $H$, inclusive.

Note that the elements of a subsequence must not be reordered.

Also, even if the elements of the chosen subsequences are the same, they are counted separately if the positions they are chosen from differ.

Input

$N$
$a_1 \ a_2 \ ... \ a_N$
$Q$
$L_1 \ H_1$
:
$L_Q \ H_Q$

The first line contains the number of elements of the sequence, $N$.

The second line contains the values of the elements of the sequence, $a_i \ (1 \leq i \leq N)$, separated by spaces.

The third line contains the number of queries, $Q$.

Each of the $Q$ lines starting from the fourth line contains the values $L_i$, $H_i$, which determine the range of values of the middle element, separated by a space.

The input satisfies the following constraints.

  • $1 \ \leq \ N \ \leq \ 2 \times 10^5$
  • $1 \ \leq \ a_i \ \leq \ 10^9$
  • $1 \ \leq \ Q \ \leq \ 2 \times 10^5$
  • $1 \ \leq \ L_i \ \leq \ H_i \ \leq \ 10^9$

There are no subtasks or the like, but in 1_small*.in, $N = Q = 200, \ a_i \leq 10000$.

Output

The output consists of $Q$ lines.

On the $i \ (1 \leq i \leq Q)$-th line, print the number of Kadomatsu subsequences whose middle element has a value between $L_i$ and $H_i$, inclusive.

Print a newline at the end.

Sample

Sample 1
Input
4
1 4 2 3
4
3 4
5 8
2 4
1 1
Output
2
0
3
0

There are 4 subsequences of 3 elements: {1, 4, 2}, {1, 4, 3}, {1, 2, 3}, {4, 2, 3},

and all of them except {1, 2, 3} are Kadomatsu sequences.

Therefore, there are 3 Kadomatsu subsequences: {1, 4, 2}, {1, 4, 3}, {4, 2, 3}.

Sample 2
Input
5
1 1 1 4 2
4
1 3
3 6
4 4
4 8
Output
0
3
3
3

The subsequence {1, 4, 2} can be formed in 3 ways.

Sample 3
Input
6
1 1 4 5 1 4
3
1 4
2 3
1 14514
Output
1
0
4

Sample 4
Input
1
1
1
1 1
Output
0

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。