Min Max Pair

AtCoder
IDabc262_c
Time2000ms
Memory256MB
Difficulty
You are given a sequence $a = (a_1, \dots, a_N)$ of length $N$ consisting of integers between $1$ and $N$. Find the number of pairs of integers $i, j$ that satisfy all of the following conditions: * $1 \leq i \lt j \leq N$ * $\min(a_i, a_j) = i$ * $\max(a_i, a_j) = j$ ## Constraints * $2 \leq N \leq 5 \times 10^5$ * $1 \leq a_i \leq N \, (1 \leq i \leq N)$ * All values in input are integers. ## Input Input is given from Standard Input in the following format: $N$ $a_1$ $\ldots$ $a_N$ [samples]
Samples
Input #1
4
1 3 2 4
Output #1
2

$(i, j) = (1, 4), (2, 3)$ satisfy the conditions.
Input #2
10
5 8 2 2 1 6 7 2 9 10
Output #2
8
API Response (JSON)
{
  "problem": {
    "name": "Min Max Pair",
    "description": {
      "content": "You are given a sequence $a = (a_1, \\dots, a_N)$ of length $N$ consisting of integers between $1$ and $N$. Find the number of pairs of integers $i, j$ that satisfy all of the following conditions: * ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc262_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a sequence $a = (a_1, \\dots, a_N)$ of length $N$ consisting of integers between $1$ and $N$.\nFind the number of pairs of integers $i, j$ that satisfy all of the following conditions:\n\n* ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments