Lowland Distance (APCS 2020-10 Expert)

Points 100 1.0s 256M

Problem Description

An alien archaeology team found an ancient site with \(2n\) fortresses in a row. They further discovered that fortress heights appear in pairs: each height appears in exactly two fortresses. For every pair of fortresses with the same height, the team defines the "lowland distance" of this pair as the number of fortresses between them whose heights are lower. The team wants to compute the total lowland distance of all these fortresses. Please design a program to help calculate it.

More precisely, suppose the sequence of fortress heights from left to right is \((h_1,h_2,\ldots,h_{2n})\), where every height from \(1\) to \(n\) appears exactly twice. For \(1 \le k \le n\), the lowland distance of the fortresses with height \(k\) is

\[d(k)=\left|\{i \mid p<i<q,\ h_i<k\}\right|,\]

where \(h_p=h_q=k\).

This problem asks you to compute \(\sum_{k=1}^{n} d(k)\).

For example, when \(n = 4\) and the fortress heights are \((1,4,3,2,3,1,2,4)\), heights \(1\), \(2\), \(3\), and \(4\) each appear twice. By definition, the lowland distance of height \(1\) is \(0\), because there is no number smaller than \(1\) between the two $1$s. The lowland distance of height \(2\) is \(1\), because there is only one \(1\) between the two $2$s, and it is smaller than \(2\). Similarly, the lowland distance of height \(3\) is also \(1\), while the lowland distance of height \(4\) is \(5\). Therefore, the total lowland distance is \(0 + 1 + 1 + 5 = 7\).

Hint: this problem has multiple solutions. One solution separates the height sequence into two subsequences by height, then recursively solves it with divide and conquer. Another solution computes, for each position \(i\), the number of fortresses before \(i\) whose heights are smaller than \(h_i\).

Input

The first line contains \(n\), meaning there are \(2n\) fortresses in total, and \(n\) does not exceed \(10^5\). The second line contains \(2n\) positive integers, giving the heights of the fortresses from left to right. Each height is at most \(n\), and adjacent numbers on the same line are separated by spaces. The input guarantees that equal heights appear exactly twice; that is, every integer from \(1\) to \(n\) appears exactly twice.

Output

Output the total lowland distance of all heights. Note that the answer may exceed \(2^{31}\).

Sample Input 1

4
1 4 3 2 3 1 2 4

Sample Output 1

7

Sample Input 2

5
1 2 3 4 4 3 2 1 5 5

Sample Output 2

0

Scoring

The input contains several test cases. For each test case, Python programs have a time limit of 3 seconds, and programs in other languages have a time limit of 1 second. Scores are awarded according to the number of correctly passed test cases:

Subtask group 1: 20 points, \(n\) does not exceed \(1000\).

Subtask group 2: 40 points, \(n\) does not exceed \(40000\).

Subtask group 3: 40 points, no additional constraints.

Source

Source: Programming Implementation, October 2020.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.