#P11197. [COTS 2021] 赛狗游戏 Tiket

    ID: 12423 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树状数组2021cdq 分治O2优化COCI(克罗地亚)

[COTS 2021] 赛狗游戏 Tiket

Background

Rebirth.

Translated from Izborne Pripreme 2021 (Croatian IOI/CEOI Team Selection) D2T3。1s,0.5G\texttt{1s,0.5G}

Problem Description

Three people are watching a greyhound racing game.

Each of them guesses the order in which the dogs cross the finish line. Let PiP_i be the ID of the dog that finishes in the ii-th place. We assume there are no ties.

There are NN dogs, so PiP_i forms a permutation of 1N1\sim N. Let the permutation guessed by the jj-th person be P(j)P(j).

Also, the actual finishing order forms a permutation TT.

Compute the number of pairs (a,b)(a,b) that satisfy the following conditions:

  • In TT, aa is before bb.
  • 1j3\forall 1\le j\le 3, either aa is before bb in all P(j)P(j), or bb is before aa in all P(j)P(j).

Input Format

The first line contains a positive integer NN.

The second line contains NN positive integers describing TT.

The next three lines: the jj-th line contains NN positive integers describing P(j)P(j).

Output Format

Output one line with one integer, the answer.

3
2 3 1
1 2 3
1 2 3
2 3 1
1
4
3 1 2 4
4 3 2 1
1 2 3 4
1 2 4 3
0
5
1 3 2 4 5
4 3 5 2 1
4 3 1 2 5
1 2 4 3 5
3

Hint

Sample Explanation

For sample 11, only (2,3)(2,3) satisfies the conditions.

Constraints

For 100%100\% of the testdata, it is guaranteed that:

  • 2N5×1052\le N\le 5\times 10^5.
  • 1j3\forall 1\le j\le 3, P(j)P(j) is a permutation of 1N1\sim N.
  • TT is a permutation of 1N1\sim N.
Subtask ID NN\le Special Property Score
1 1 5000 5\, 000 None 7 7
2 2 5×105 5\times 10^5 Yes 8 8
3 3 5×104 5\times 10^4 None 29 29
4 4 5×105 5\times 10^5 56 56

Special property: P(1)=P(2)P(1)=P(2). That is, the first two people guessed the same permutation.

Translated by ChatGPT 5