Logo
(追記) (追記ここまで)

31494번 - In-order 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB239939.130%

문제

The opening ceremony for the Olympic Games will take place on the river with teams on boats. The layout of the athletes on top of the boat has been designed in a very specific way: for each team, the $N$ athletes (conveniently numbered from 1ドル$ to $N$) are arranged as a binary tree.

The organiser has also designed the pre-order traversal, post-order traversal, and a (possibly empty) consecutive part of the in-order traversal of the binary tree that each team must follow.

Now, to make sure there are enough tree layouts so that each team can have a distinct one, you are asked to calculate the quantity of different possible in-order traversals, say $T,ドル modulo the prime number 999ドル,円 999,円 937$.

입력

The input consists of four lines. The first line contains the number $N$. Each subsequent line contains a list of $N$ space-separated integers. The second line contains a list $A_1, A_2, \dots , A_N,ドル where $A_k$ is the number of the $k$th athlete found in pre-order traversal. The third line contains a list $B_1, B_2, \dots , B_N,ドル where $B_k$ is the number of the $k$th athlete found in post-order traversal. The fourth line contains a list $C_1, C_2, \dots , C_N,ドル where $C_k$ is either the number of the $k$th athlete found in in-order traversal, or 0ドル$ if the organiser did not say who that $k$th athlete should be.

출력

The output should contain a single line, consisting of a single integer $S$: this is the only integer such that 0ドル \le S < 999,円 999,円 937$ and for which $T - S$ is divisible by 999ドル,円 999,円 937$.

제한

  • 1ドル \le N \le 500,円 000$
  • there exists at least one binary tree with such pre-order, post-order and in-order traversals
  • the integers $k$ for which $C_k \ge 1$ form a (possibly empty) sub-interval of the set $\{1, 2, \dots , N\}$; in other words, whenever $k \le \ell$ and both $C_k$ and $C_\ell$ are positive, all the integers $C_k , C_{k+1} , \dots , C_\ell$ are positive.

예제 입력 1

8
1 2 3 5 6 4 7 8
5 6 3 8 7 4 2 1
0 0 6 2 4 0 0 0

예제 출력 1

2

The graphs given above the problem statement are the two possible binary trees. Their in-order traversals are:

  • 5ドル$ 3ドル$ 6ドル$ 2ドル$ 4ドル$ 8ドル$ 7ドル$ 1ドル$
  • 5ドル$ 3ドル$ 6ドル$ 2ドル$ 4ドル$ 7ドル$ 8ドル$ 1ドル$

예제 입력 2

3
1 2 3
3 2 1
0 0 0

예제 출력 2

4

The four possible in-order traversals are:

  • 3ドル$ 2ドル$ 1ドル$
  • 2ドル$ 3ドル$ 1ドル$
  • 1ドル$ 3ドル$ 2ドル$
  • 1ドル$ 2ドル$ 3ドル$

예제 입력 3

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

예제 출력 3

3

The three possible in-order traversals are:

  • 2ドル$ 4ドル$ 3ドル$ 1ドル$
  • 1ドル$ 4ドル$ 3ドル$ 2ドル$
  • 3ドル$ 4ドル$ 2ドル$ 1ドル$

힌트

출처

ICPC > Regionals > Europe > Southwestern European Regional Contest > SWERC 2023-2024 M번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /