QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 256 MB 總分: 100 可 Hack ✓

#9663. 反转对染色

统计

我们都知道逆序对的定义:对于一个长度为 $n$ 的排列 $a_1, a_2, \dots, a_n$,如果存在 $i < j$ 且 $a_i > a_j$,则称 $(a_i, a_j)$ 为一个逆序对。长度为 $n$ 的排列包含 $1$ 到 $n$ 的每个数字恰好一次。

寻找逆序对的数量对你来说应该不是难事。现在,给定一个长度为 $n$ 的排列 $a_1, a_2, \dots, a_n$。对于每一对满足 $i < j$ 且 $a_i > a_j$ 的逆序对,将 $n \times n$ 网格中第 $i$ 行第 $a_j$ 列的单元格涂色。请确定最终网格中有多少个涂色单元格构成的连通分量。连通的定义是:如果两个涂色单元格共享一条边,则认为这两个单元格是连通的。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 3 \times 10^5$),表示排列的长度。 第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$,表示一个长度为 $n$ 的排列。

输出格式

输出一个整数,表示最终网格中四连通分量的数量。

样例

样例输入 1

9
5 9 1 8 2 6 4 7 3

样例输出 1

5

样例输入 2

2
1 2

样例输出 2

0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.