A - 色塗り (Grid Coloring)

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score: 100 points

Problem Statement

President K is designing a pattern represented by a grid with N rows and N columns. To achieve this, he has decided to paint each cell with a color represented by an integer number. Let us refer to the cell in the i-th row (1 \leq i \leq N) and j-th column (1 \leq j \leq N) as cell (i,j).

Currently, the cells in the first column and first row are already painted. Specifically, cell (i,1) (1 \leq i \leq N) is painted with color A_i and cell (1,j) (1 \leq j \leq N) is painted with color B_j. Note that A_1 = B_1.

For the remaining unpainted cells, President K is going to paint them by the following procedure:

  • For each i=2,3,\dots,N in order, paint the cells in the i-th row as follows:
    • For each j=2,3,\dots,N in order, paint cell (i,j) with the color that has the larger number between:
      • The color of cell (i-1,j), and
      • The color of cell (i,j-1).
      If both colors have the same number, paint the cell with that color.

President K would like to determine the color that is painted on the largest number of cells after all N^2 cells have been painted, as well as the number of cells painted with that color.

Write a program that, given the size of the grid and the color information for the first column and first row, determines the color number painted on the largest number of cells and the number of cells painted with that color. If multiple colors are painted on the largest number of cells, output the largest color number among them.


Input

Read the following data from the standard input.

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

Output

Write one line to the standard output containing two integers separated by a space:

  1. The color number that is painted on the largest number of cells, and
  2. The number of cells painted with that color.

If multiple colors are painted on the largest number of cells, output the largest color number among them.


Constraints

  • 2 \leq N \leq 200\,000.
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N).
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq N).
  • A_1 = B_1.
  • Given values are all integers.

Subtasks

  1. (15 points) N \leq 500, A_i \leq 100\,000 (1 \leq i \leq N), B_j \leq 100\,000 (1 \leq j \leq N).
  2. (10 points) N \leq 500.
  3. (20 points) A_i \leq 2 (1 \leq i \leq N), B_j \leq 2 (1 \leq j \leq N).
  4. (25 points) A_i < A_{i+1} (1 \leq i \leq N-1), B_j < B_{j+1} (1 \leq j \leq N-1).
  5. (30 points) No additional constraints.

Sample Input 1

3
5 2 5
5 3 1

Sample Output 1

5 4

In this sample, the color of each cell in the grid will be as follows:

5 3 1
2 3 3
5 5 5

The color number painted on the largest number of cells is 5, which appears on 4 cells. Thus, print 5 and 4 in this order, separated by a space.

This sample input satisfies the constraints of Subtasks 1, 2, and 5.


Sample Input 2

3
1 7 8
1 3 5

Sample Output 2

8 3

In this sample, the color of each cell in the grid will be as follows:

1 3 5
7 7 7
8 8 8

The color numbers painted on the largest number of cells are 7 and 8, each painted on 3 cells. In this case, output the larger color number, 8, followed by the number of cells, 3, separated by a space.

This sample input satisfies the constraints of Subtasks 1, 2, 4, and 5.


Sample Input 3

4
2 1 2 1
2 1 1 2

Sample Output 3

2 10

This sample input satisfies the constraints of Subtasks 1, 2, 3, and 5.

配点: 100

問題文

K 理事長は縦 N 行,横 N 列のマス目で表される模様を作ろうとしている.そのために,各マスに整数の番号で表される色を塗ることにした.以降,上から i 行目 (1 \leqq i \leqq N),左から j 列目 (1 \leqq j \leqq N) のマスをマス (i,j) と呼ぶことにする.

現時点で,1 列目と 1 行目のマスには既に色が塗られている.具体的には,マス (i,1) (1 \leqq i \leqq N) は色 A_i で,マス (1,j) (1 \leqq j \leqq N) は色 B_j で塗られている.ここで A_1 = B_1 である.

残りのマスについて,K 理事長は以下の手順で色を塗っていく.

  • i=2,3,\dots,N の順に,以下の手順で i 行目のマスに色を塗る.
    • j=2,3,\dots,N の順に,マス (i,j)
      • マス (i-1,j) に塗られている色
      • マス (i,j-1) に塗られている色
      のうち番号が大きい方の色で塗る.番号が同じ場合は,その色で塗る.

K 理事長は,最終的に N^2 個のマスすべてに色が塗られたとき,最も多くのマスに塗られた色の番号,およびその色が塗られているマスの個数を求めたい.

マス目の大きさおよび 1 列目と 1 行目のマスの情報が与えられたとき,最も多くのマスに塗られた色の番号とその色が塗られているマスの個数を求めるプログラムを作成せよ.最も多くのマスに塗られた色の番号が複数存在する場合,そのうち最も番号の大きいものを求め出力すること.


入力

入力は以下の形式で標準入力から与えられる.

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

出力

標準出力に,最も多くのマスに塗られた色の番号と,その色が塗られているマスの個数を空白区切りで 1 行に出力せよ.最も多くのマスに塗られた色の番号が複数存在する場合,そのうち最も番号の大きいものを出力すること.


制約

  • 2 \leqq N \leqq 200\,000
  • 1 \leqq A_i \leqq 10^9 (1 \leqq i \leqq N).
  • 1 \leqq B_j \leqq 10^9 (1 \leqq j \leqq N).
  • A_1 = B_1
  • 入力される値はすべて整数である.

小課題

  1. (15 点) N \leqq 500A_i \leqq 100\,000 (1 \leqq i \leqq N),B_j \leqq 100\,000 (1 \leqq j \leqq N).
  2. (10 点) N \leqq 500
  3. (20 点) A_i \leqq 2 (1 \leqq i \leqq N),B_j \leqq 2 (1 \leqq j \leqq N).
  4. (25 点) A_i < A_{i+1} (1 \leqq i \leqq N-1),B_j < B_{j+1} (1 \leqq j \leqq N-1).
  5. (30 点) 追加の制約はない.

入力例 1

3
5 2 5
5 3 1

出力例 1

5 4

最終的に各マスに塗られる色の番号は以下のようになる.

5 3 1
2 3 3
5 5 5

最も多くのマスに塗られた色の番号は 5 であり,これは 4 個のマスに塗られているため,54 を空白区切りで出力する.

この入力例は小課題 1,2,5 の制約を満たす.


入力例 2

3
1 7 8
1 3 5

出力例 2

8 3

最終的に各マスに塗られる色の番号は以下のようになる.

1 3 5
7 7 7
8 8 8

最も多くのマスに塗られた色の番号は 78 であり,それぞれ 3 個のマスに塗られている.2 つの色のうち最も番号が大きい色は 8 であるため,83 を空白区切りで出力する.

この入力例は小課題 1,2,4,5 の制約を満たす.


入力例 3

4
2 1 2 1
2 1 1 2

出力例 3

2 10

この入力例は小課題 1,2,3,5 の制約を満たす.

B - 勇者ビ太郎 2 (Bitaro the Brave 2)

Time Limit: 1 sec / Memory Limit: 1024 MiB

Score: 100 points

Problem Statement

Bitaro, the brave hero, has set out on an adventure to defeat monsters.

Bitaro has a strength value, denoted as x, which starts at an initial value. There are N monsters, each labeled with a number from 1 to N. To defeat the i-th monster (1 \leq i \leq N), Bitaro must have a strength of at least A_i. Defeating the i-th monster increases Bitaro's strength by B_i.

Bitaro wants to defeat all the monsters using the following strategy:

  1. Start with a specific monster j (1 \leq j \leq N) and defeat the monsters in order: j, j + 1, \ldots, N.
  2. If j \geq 2, go back and defeat the monsters 1, 2, \ldots, j-1 in sequence.

Given the information about the monsters, write a program to determine the minimum initial strength x required for Bitaro to defeat all the monsters.


Input

Read the following data from the standard input.

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Output a single integer, the minimum initial strength x required for Bitaro to defeat all the monsters.


Constraints

  • 2 \leq N \leq 500\,000.
  • 0 \leq A_i \leq 10^9 (1 \leq i \leq N).
  • 0 \leq B_i \leq 10^9 (1 \leq i \leq N).
  • Given values are all integers.

Subtasks

  1. (10 points) N \leq 2\,000, and the minimum initial strength x is 10 or less.
  2. (21 points) N \leq 2\,000.
  3. (19 points) The minimum initial strength x is 10 or less.
  4. (22 points) B_i = 1 (1 \leq i \leq N).
  5. (28 points) No additional constraints.

Sample Input 1

5
1 3 2 8 6
4 3 1 1 2

Sample Output 1

1
  • Start with an initial strength of 1.
  • Defeat monsters in the following order:
    1. Defeat monster 1. Strength increases by 4 to 5.
    2. Defeat monster 2. Strength increases by 3 to 8.
    3. Defeat monster 3. Strength increases by 1 to 9.
    4. Defeat monster 4. Strength increases by 1 to 10.
    5. Defeat monster 5. Strength increases by 2 to 12.

This sample input satisfies the constraints of Subtasks 1,2,3 and 5.


Sample Input 2

5
1 6 3 3 2
1 2 1 0 1

Sample Output 2

3
  • Start with an initial strength of 3.
  • Defeat monsters in the following order:
    1. Defeat monster 3. Strength increases by 1 to 4.
    2. Defeat monster 4. Strength increases by 0 to 4.
    3. Defeat monster 5. Strength increases by 1 to 5.
    4. Defeat monster 1. Strength increases by 1 to 6.
    5. Defeat monster 2. Strength increases by 2 to 8.

This sample input satisfies the constraints of Subtasks 1,2,3 and 5.


Sample Input 3

10
11 9 8 12 7 7 8 12 9 10
1 1 1 1 1 1 1 1 1 1

Sample Output 3

9

This sample input satisfies the constraints of all the subtasks.


Sample Input 4

7
1125 638 0 37 737 820 1202
23 984 558 350 52 345 580

Sample Output 4

0

This sample input satisfies the constraints of Subtasks 1,2,3 and 5.

配点: 100

問題文

勇者のビ太郎は,モンスターを討伐しに冒険に出ることになった.

ビ太郎は強さという値を持っている.ビ太郎の強さの初期値を x とする. モンスターは N 体存在し,1 から N までの番号が付けられている.モンスター i (1 \leqq i \leqq N) を倒すには強さが A_i 以上であることが必要である.モンスター i を倒すと強さがB_i 増える.

ビ太郎は冒険において次のような行動をとることですべてのモンスターを倒したい.

  • ある j (1 \leqq j \leqq N) から始めて,モンスター j, j + 1, \ldots, N を順に倒す.
  • 次に,j \geqq 2 なら,モンスター 1, 2, \ldots, j-1 を順に倒す.

モンスターの情報が与えられたとき,すべてのモンスターを倒すために必要な強さの初期値 x の最小値を求めるプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

標準出力に,すべてのモンスターを倒すために必要な強さの初期値の最小値を 1 行で出力せよ.


制約

  • 2 \leqq N \leqq 500\,000
  • 0 \leqq A_i \leqq 10^9 (1 \leqq i \leqq N).
  • 0 \leqq B_i \leqq 10^9 (1 \leqq i \leqq N).
  • 入力される値はすべて整数である.

小課題

  1. (10 点) N \leqq 2\,000,必要な強さの初期値の最小値は 10 以下である.
  2. (21 点) N \leqq 2\,000
  3. (19 点) 必要な強さの初期値の最小値は 10 以下である.
  4. (22 点) B_i = 1 (1 \leqq i \leqq N).
  5. (28 点) 追加の制約はない.

入力例 1

5
1 3 2 8 6
4 3 1 1 2

出力例 1

1

強さの初期値が 1 であるとき,たとえば次のような順番ですべてのモンスターを倒すことができる.

  • 強さの初期値を 1 とする.
  • モンスター 1 を倒す.強さが 4 増えて,強さは 5 になる.
  • モンスター 2 を倒す.強さが 3 増えて,強さは 8 になる.
  • モンスター 3 を倒す.強さが 1 増えて,強さは 9 になる.
  • モンスター 4 を倒す.強さが 1 増えて,強さは 10 になる.
  • モンスター 5 を倒す.強さが 2 増えて,強さは 12 になる.

強さの初期値が 0 以下ですべてのモンスターを倒す方法は存在しないため,1 を出力する.

この入出力例は小課題 1,2,3,5 の制約を満たす.


入力例 2

5
1 6 3 3 2
1 2 1 0 1

出力例 2

3

強さの初期値が 3 であるとき,たとえば次のような順番ですべてのモンスターを倒すことができる.

  • 強さの初期値を 3 とする.
  • モンスター 3 を倒す.強さが 1 増えて,強さは 4 になる.
  • モンスター 4 を倒す.強さが 0 増えて,強さは 4 になる.
  • モンスター 5 を倒す.強さが 1 増えて,強さは 5 になる.
  • モンスター 1 を倒す.強さが 1 増えて,強さは 6 になる.
  • モンスター 2 を倒す.強さが 2 増えて,強さは 8 になる.

強さの初期値が 2 以下ですべてのモンスターを倒す方法は存在しないため,3 を出力する.

この入出力例は小課題 1,2,3,5 の制約を満たす.



入力例 3

10
11 9 8 12 7 7 8 12 9 10
1 1 1 1 1 1 1 1 1 1

出力例 3

9

この入出力例は小課題すべての制約を満たす.

入力例 4

7
1125 638 0 37 737 820 1202
23 984 558 350 52 345 580

出力例 4

0

この入出力例は小課題 1,2,3,5 の制約を満たす.

C - ミ・テレフェリコ (Mi Teleférico)

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score: 100 points

Problem Statement

La Paz, the capital city of Bolivia, is famous as a tourist spot and for an aerial cable car called Mi Teleférico. You are now visiting La Paz for sightseeing, and you want to visit as many sightseeing places as possible. In this task, we consider the following simplified situation.

There are N aerial cable car stations in La Paz, numbered from 1 to N in ascending order of altitude. There are M one-way lines, numbered from 1 to M. There are P aerial cable car companies, numbered from 1 to P. Each line is managed by a single company. Line i (1 \leq i \leq M) is operated from station A_i to station B_i, and is managed by the company C_i. Here, the line always runs from the lower altitude station to the higher altitude station. In other words, A_i < B_i holds.

The Bureau of transportation of La Paz issued unlimited ride passes for convenience. Each ride pass contains 2 integers l, r, which satisfy 1 \leq l \leq r \leq P. The pass enables the possessor to ride lines, which are managed by any one of company l, l + 1, \dots, r. In other words, for an integer i which satisfies 1 \leq i \leq M, the pass enables the possessor to ride line i when l \leq C_i \leq r holds. It is possible to use a single pass for several lines. Let a ride pass (l, r) denote this ride pass.

Now, Q tourists, numbered from 1 to Q, visit La Paz. Tourist j (1 \leq j \leq Q) has a ride pass (L_j, R_j) and X_j boliviano cash.

Each tourist's goal is to ensure that no station cannot be travelled from station 1, using only lines that can be ridden with the ride pass he or she has. Tourist j (1 \leq j \leq Q) can exchange his or her ride pass described in the following process to achieve their goal. Here, each tourist can exchange at most once.

  1. He or she chooses 2 integers l', r', which satisfy 1 \leq l' \leq r' \leq P.
  2. He or she exchanges a ride pass (L_j, R_j) for a ride pass (l', r'). It costs |L_j - l'| + |R_j - r'| boliviano as a fee.

Your purpose is to determine, for each tourist, whether or not he or she can achieve his or her goal within the cash he or she has.

Write a program which, given information about stations, lines, and tourists, determines whether or not he or she can achieve his or her goal within the cash he or she has for each tourist.


Input

Read the following data from the standard input.

N M P
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Q
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_Q R_Q X_Q

Output

Write Q lines to the standard output. On the j-th line (1 \leq j \leq Q), output Yes if tourist j can achieve his or her goal, and No otherwise.


Constraints

  • 2 \leq N \leq 300\,000.
  • 1 \leq M \leq 300\,000.
  • 1 \leq P \leq 10^9.
  • 1 \leq A_i < B_i \leq N (1 \leq i \leq M).
  • 1 \leq C_i \leq P (1 \leq i \leq M).
  • 1 \leq Q \leq 400\,000.
  • 1 \leq L_j \leq R_j \leq P (1 \leq j \leq Q).
  • 0 \leq X_j \leq 10^9 (1 \leq j \leq Q).
  • Given values are all integers.

Subtasks

  1. (7 points) N \leq 50, M \leq 50, Q \leq 50, X_j = 0 (1 \leq j \leq Q).
  2. (8 points) P \leq 10.
  3. (11 points) P \leq 100.
  4. (23 points) P \leq 300\,000, X_j = 0 (1 \leq j \leq Q).
  5. (9 points) P \leq 300\,000.
  6. (22 points) N \leq 8\,000, M \leq 8\,000.
  7. (20 points) No additional constraints.

Sample Input 1

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

Sample Output 1

Yes
No
No
Yes

First, tourist 1 has a ride pass (3, 7) and 0 boliviano cash initially. He or she can achieve his or her goal without exchange. Because, this pass enables tourist 1 to ride 4 lines, lines 1, 2, 3, 4, and he or she can move to each stations from station 1 by using these 4 lines as follows.

  • It is possible to move as station 1 \rightarrow 2 by using line 3.
  • It is possible to move as station 1 \rightarrow 2 \rightarrow 3 by using lines 1, 4 in this order.
  • It is possible to move as station 1 \rightarrow 2 \rightarrow 4 by using lines 3, 2 in this order.

Therefore, output Yes on the 1-st line.

Next, tourist 2 has a ride pass (5, 6) and 0 boliviano cash initially. However, He or she can't achieve his or her goal. Because, this pass enables tourist 2 to ride 2 lines, lines 3, 4, and he or she can't move station 4 from station 1 by using these 2 lines. Moreover, since tourist 2 has only 0 boliviano as cash, he or she can't exchange the pass with another pass.

Hence, output No on the 2-nd line.

Also, tourist 3 can't achieve his or her goal, and tourist 4 can achieve his or her goal. Thus, output No on the 3-rd line, and output Yes on the 4-th line.

This sample input satisfies the constraints of all the Subtasks.


Sample Input 2

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

Sample Output 2

Yes
No
Yes

The information of stations and lines is the same as in the sample Input 1.

First, tourist 1 has a ride pass (5, 6) and 10 boliviano cash initially. He or she can achieve his or her goal with exchange as below.

  1. He or she chooses 2 integers l' = 1, r' = 5, which satisfy 1 \leq l' \leq r' \leq P.
  2. He or she exchange a ride pass (5, 6) for a ride pass (1, 5). It costs |5 - 1| + |6 - 5| = 5 boliviano as a fee.

Therefore, output Yes on the 1-st line.

Next, tourist 2 has a ride pass (3, 4) and 1 boliviano cash initially. He or she can't achieve his or her goal with any exchange.

Consequently, output No on the 2-nd line.

Since Tourist 3 can achieve his or her goal, output Yes on the 3-rd line.

This sample input satisfies the constraints of Subtasks 2,3,5,6,7.


Sample Input 3

3 1 1000000000
1 2 6
1
1 1000000000 1000000000

Sample Output 3

No

It is not possible to move from station 1 to station 3 with these lines, so tourists can't achieve their goal regardless of their ride passes.

This sample input satisfies the constraints of Subtasks 6,7.


Sample Input 4

5 9 2000
2 3 1814
2 3 457
1 2 1226
3 4 1354
1 5 1050
1 2 1725
2 3 1383
1 5 1626
1 4 1795
5
850 1872 128
82 428 1217
487 924 573
1639 1926 202
202 420 25

Sample Output 4

Yes
Yes
Yes
Yes
No

This sample input satisfies the constraints of Subtasks 5,6,7.

配点: 100

問題文

ボリビアの首都であるラパスは観光地であるとともに,ミ・テレフェリコ(Mi Teleférico)というロープウェイ路線網でも有名である.あなたはラパスに観光に来ており,できるだけ多くの場所を観光したいと思っている.ここで,現実を単純化した次のような状況設定を考えたい.

ラパスには N 個のロープウェイ駅があり,標高が低い順に 1 から N までの番号が付けられている.また,M 個の一方通行の路線があり,1 から M までの番号が付けられている.さらに,P 個のロープウェイ会社があり,1 から P までの番号が付けられている.各路線は 1 つの会社によって管理されている.路線 i (1 \leqq i \leqq M) は駅 A_i から駅 B_i に向かって運行しており,会社 C_i によって管理されている.ここで,路線は必ず標高の低い駅から標高の高い駅に向かって運行している.すなわち,A_i < B_i が成立している.

利便性のために,ラパスの交通局はフリーパスを発行した.それぞれのフリーパスには 1 \leqq l \leqq r \leqq P を満たす 2 つの整数 l, r が書かれており,会社 l, l + 1, \dots, r によって管理されている路線に乗ることができる.すなわち,1 \leqq i \leqq M を満たす整数 i について l \leqq C_i \leqq r を満たすならば路線 i に乗ることができる.ここで,1 つのフリーパスを複数の路線で使うことも可能である.このフリーパスをフリーパス (l, r) とする.

さて,ラパスに 1 から Q までの番号が付けられた Q 人の観光客が訪れた.観光客 j (1 \leqq j \leqq Q) はフリーパス (L_j, R_j) と,現金 X_j ボリビアーノを持っている.

観光客の目標は,持っているフリーパスを使って乗ることができる路線のみを用いて,駅 1 から移動できない駅がないようにすることである.そのために,観光客 j (1 \leqq j \leqq Q) は以下の手順で表される交換を行うことができる.ただし,各観光客について,交換は高々 1 回しか行うことができない.

  1. 1 \leqq l' \leqq r' \leqq P を満たす 2 つの整数 l', r' を決める.
  2. フリーパス (L_j, R_j) とフリーパス (l', r') を交換する.手数料として |L_j - l'| + |R_j - r'| ボリビアーノを要する.

あなたの目的は,それぞれの観光客について,持っている現金の範囲内で目標を達成することができるかを判定することである.

路線と観光客の情報が与えられたとき,それぞれの観光客について,持っている現金の範囲内で目標を達成することができるかを判定するプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N M P
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Q
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_Q R_Q X_Q

出力

標準出力に Q 行で出力せよ.j 行目 (1 \leqq j \leqq Q) には,観光客 j が目標を達成することができる場合は Yes を,そうでない場合は No を出力せよ.


制約

  • 2 \leqq N \leqq 300\,000
  • 1 \leqq M \leqq 300\,000
  • 1 \leqq P \leqq 10^9
  • 1 \leqq A_i < B_i \leqq N (1 \leqq i \leqq M).
  • 1 \leqq C_i \leqq P (1 \leqq i \leqq M).
  • 1 \leqq Q \leqq 400\,000
  • 1 \leqq L_j \leqq R_j \leqq P (1 \leqq j \leqq Q).
  • 0 \leqq X_j \leqq 10^9 (1 \leqq j \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (7 点) N \leqq 50M \leqq 50Q \leqq 50X_j = 0 (1 \leqq j \leqq Q).
  2. (8 点) P \leqq 10
  3. (11 点) P \leqq 100
  4. (23 点) P \leqq 300\,000X_j = 0 (1 \leqq j \leqq Q).
  5. (9 点) P \leqq 300\,000
  6. (22 点) N \leqq 8\,000M \leqq 8\,000
  7. (20 点) 追加の制約はない.

入力例 1

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

出力例 1

Yes
No
No
Yes

まず,観光客 1 については,最初フリーパス (3, 7) と現金 0 ボリビアーノを持っている.この観光客は,フリーパスの交換を行わないことで目標を達成できる.なぜなら,フリーパス (3, 7) により乗ることができる路線は 1, 2, 3, 44 つであり,この 4 つの路線を用いて,以下のように駅 1 から各駅に移動することができるからである.

  • 路線 3 を用いることで,駅 1 \to 2 と移動することができる.
  • 路線 1, 4 をこの順に用いることで,駅 1 \to 2 \to 3 と移動することができる.
  • 路線 3, 2 をこの順に用いることで,駅 1 \to 2 \to 4 と移動することができる.

したがって,1 行目に Yes を出力する.

次に,観光客 2 については,最初フリーパス (5, 6) と現金 0 ボリビアーノを持っているが,この観光客は目標を達成することができない.なぜなら,フリーパス (5, 6) により乗ることができる路線は路線 3, 42 つのみであり,この 2 つの路線を用いて,駅 1 から駅 4 へ移動することができないからである.また,現金を 0 ボリビアーノしか持っていないため,異なるフリーパスと交換することができないからである.

したがって,2 行目に No を出力する.

さらに,観光客 3 については目標を達成することができず,観光客 4 については目標を達成することができるため,3 行目に No を出力し,4 行目に Yes を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 2

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

出力例 2

Yes
No
Yes

路線の情報は入力例 1 と同じである.

まず,観光客 1 については最初フリーパス (5, 6) と現金 10 ボリビアーノを持っている.この観光客は,フリーパスの交換を次のように行うことで目標を達成できる.

  1. 1 \leqq l' \leqq r' \leqq P を満たす 2 つの整数として l' = 1, r' = 5 と決める.
  2. フリーパス (5, 6) とフリーパス (1, 5) を交換する.手数料として |5-1|+|6-5|=5 ボリビアーノを要する.

したがって,1 行目に Yes を出力する.

次に,観光客 2 については最初フリーパス (3, 4) と現金 1 ボリビアーノを持っている.この観光客はどのようなフリーパスの交換を行っても,目標を達成することができない.

したがって,2 行目に No を出力する.

さらに,観光客 3 については目標を達成することができるため,3 行目に Yes を出力する.

この入力例は小課題 2,3,5,6,7 の制約を満たす.


入力例 3

3 1 1000000000
1 2 6
1
1 1000000000 1000000000

出力例 3

No

この路線において,駅 1 から駅 3 へ移動することができない.したがって,フリーパスによらず,観光客は目標を達成することはできない.

この入力例は小課題 6,7 の制約を満たす.


入力例 4

5 9 2000
2 3 1814
2 3 457
1 2 1226
3 4 1354
1 5 1050
1 2 1725
2 3 1383
1 5 1626
1 4 1795
5
850 1872 128
82 428 1217
487 924 573
1639 1926 202
202 420 25

出力例 4

Yes
Yes
Yes
Yes
No

この入力例は小課題 5,6,7 の制約を満たす.

D - 長いだけのネクタイ 2 (Just Long Neckties 2)

Time Limit: 3 sec / Memory Limit: 2048 MiB

Score: 100 points

Problem Statement

Just Odd Inventions Co., Ltd. is a company renowned for doing "just odd inventions." Here we just call it JOI Company.

To celebrate the 5th anniversary of their flagship product, "Just Long Neckties," JOI Company has invented a new product: "Just Stretchable Neckties." As its name suggests, this new necktie has the unique feature of being able to extend its length indefinitely.

JOI Company has decided to hold a showcase event to promote the new necktie, and you have been selected as the host of the event. At the event, several models wearing the new neckties will appear on stage. Initially, the length of every necktie worn by the models is set to 1.

After that, you will carry out a total of N performances to demonstrate the necktie's stretchable feature to the audience. Each performance is conducted as follows:

  1. First, you ask the audience to call out a number of their choice. Let this number be x.
  2. Next, you decide whether to respond or ignore it.
    • If you choose to respond to it, you must select one of the models on stage whose necktie length is currently less than or equal to x and set the length of that model's necktie to exactly x. (Note that you may select a model whose necktie length is already x.) However, if no model can be selected, the showcase event will fail.
    • If you choose to ignore it, you do nothing.

However, if you ignore the audience's number two or more times in a row, the audience will get angry, and the showcase event will fail.

The number of models on stage, denoted as k (k\geq 1), has not yet been determined. Since hiring models costs considerable money, it is desirable to keep k as small as possible. The minimum value of k required to prevent the showcase event from failing depends on the numbers called out by the audience during the performances. Fortunately, you possess precognitive abilities and have foreseen that the number called out by the audience during the i-th performance (1\leq i\leq N) will be A_i.

Write a program which, given the numbers called out by the audience during the showcase event, calculates the minimum number of models k required to prevent the showcase event from failing.


Input

Read the following data from the standard input.

N
A_1 A_2 \cdots A_N

Output

Write one line to the standard output. The output should contain the minimum number of models k required to prevent the showcase event from failing.


Constraints

  • 2 \leq N \leq 5\,000\,000.
  • 1\leq A_i \leq 21 (1 \leq i \leq N).
  • Given values are all integers.

Subtasks

  1. (10 points) N\leq 15.
  2. (6 points) N\leq 500, A_i \leq 2 (1 \leq i \leq N).
  3. (12 points) N\leq 500, A_i \leq 5 (1 \leq i \leq N).
  4. (18 points) N\leq 500, A_i \leq 15 (1 \leq i \leq N).
  5. (26 points) N\leq 500\,000, A_i \leq 15 (1 \leq i \leq N).
  6. (10 points) N\leq 500\,000.
  7. (18 points) No additional constraints.

Sample Input 1

5
5 3 4 2 1

Sample Output 1

2

When k=2, the showcase event can be conducted as follows, for example:

  • First, two models wearing the new neckties appear on stage. Initially, the length of each model's necktie is 1.
  • During the 1st performance, the audience calls out 5. You ignore this.
  • During the 2nd performance, the audience calls out 3. You respond to this by selecting the first model and setting their necktie length to 3. The necktie lengths of the two models are now 3 and 1, respectively.
  • During the 3rd performance, the audience calls out 4. You respond to this by selecting the first model and setting their necktie length to 4. The necktie lengths of the two models are now 4 and 1, respectively.
  • During the 4th performance, the audience calls out 2. You respond to this by selecting the second model and setting their necktie length to 2. The necktie lengths of the two models are now 4 and 2, respectively.
  • During the 5th performance, the audience calls out 1. You ignore this.

When k=1, the showcase event will always fail. For example, if you respond to the audience's numbers during the 2nd, 3rd, and 4th performances as described above, the only model's necktie length becomes 4 after the 3rd performance. Then, at the 4th performance, you cannot select a model whose necktie length is less than or equal to 2, so the showcase event fails.

Hence, the minimum number of models k required to prevent the showcase event from failing is 2, and the output should be 2.

This sample input satisfies the constraints of Subtasks 1,3,4,5,6, and 7.


Sample Input 2

6
2 1 1 2 2 1

Sample Output 2

1

When k=1, the showcase event can be conducted as follows, for example:

  • First, a model wearing the new necktie appear on stage. Initially, the length of the model's necktie is 1.
  • During the 1st performance, the audience calls out 2. You ignore this.
  • During the 2nd performance, the audience calls out 1. You respond to this by selecting the only model on stage and setting their necktie length to 1.
  • During the 3rd performance, the audience calls out 1. You respond to this by selecting the only model on stage and setting their necktie length to 1.
  • During the 4th performance, the audience calls out 2. You respond to this by selecting the only model on stage and setting their necktie length to 2.
  • During the 5th performance, the audience calls out 2. You respond to this by selecting the only model on stage and setting their necktie length to 2.
  • During the 6th performance, the audience calls out 1. You ignore this.

Note that in the example above, during the 2nd and 3rd performances, a model whose necktie length is already 1 is selected, and their necktie length is set to 1 again. Such a choice, where the necktie length does not change, is also allowed.

Hence, the minimum number of models k required to prevent the showcase event from failing is 1, and the output should be 1.

This sample input satisfies the constraints of all the subtasks.

\newline


Sample Input 3

10
2 4 6 7 4 5 5 3 4 1

Sample Output 3

3

This sample input satisfies the constraints of Subtasks 1,4,5,6, and 7.

配点: 100

問題文

Just Odd Inventions 社は,「ただ奇妙な発明 (just odd inventions)」をすることで知られている会社である. ここでは略して JOI 社と呼ぶ.

JOI 社は,看板商品である「長いだけのネクタイ」発売 5 周年を記念し,新しく「長くなるだけのネクタイ」を開発した. この新型ネクタイの特徴は,その名の通り長さをいくらでも伸ばせることである.

JOI 社は,新型ネクタイの宣伝を目的とした披露会の開催を決定し,その司会にあなたを抜擢した. 披露会ではまず,新型ネクタイを着用した何人かのモデルが舞台上に登壇する. 最初,各モデルが着用しているネクタイの長さはすべて 1 である.

その後,あなたはネクタイの長さを伸ばせる機能を観客に実感してもらうためのパフォーマンスN 回行う. 各パフォーマンスは以下のように行われる.

  1. まず,観客に好きな数を 1 つ唱えてもらう.ここで観客が唱えた数を x とおく.
  2. 次に,司会のあなたはこれに反応するか無視するかを選ぶ.
    • 反応することを選んだ場合,あなたは登壇しているモデルのうち着用しているネクタイの長さが x 以下であるような者を 1 人選び,そのモデルのネクタイの長さを x にする(着用しているネクタイの長さが元々 x であるようなモデルも選ぶことができる点に注意せよ). ただし,選ぶことのできるモデルが 1 人も存在しない場合,披露会は失敗に終わる.
    • 無視することを選んだ場合,何もしない.

ただし,観客の唱えた数を 2 回以上連続で無視してしまうと,観客が機嫌を損ね,披露会は失敗に終わる.

舞台上に登壇させるモデルの人数 k (k\geqq 1) はまだ決まっていないが,モデルを雇うのにはお金がかかるため,k の値はできるだけ小さい方が望ましい. 披露会が失敗に終わらないために必要なモデルの人数は各パフォーマンスで観客が唱える数に依存するが,あなたはその予知能力により,i 回目 (1\leqq i\leqq N) のパフォーマンスで観客が唱える数が A_i であることを予見した.

披露会で観客が唱える数の情報が与えられたとき,披露会が失敗に終わらないために必要なモデルの人数 k の最小値を求めるプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N
A_1 A_2 \cdots A_N

出力

標準出力に,披露会が失敗に終わらないために必要なモデルの人数 k の最小値を 1 行で出力せよ.


制約

  • 2 \leqq N \leqq 5\,000\,000
  • 1\leqq A_i \leqq 21 (1 \leqq i \leqq N).
  • 入力される値はすべて整数である.

小課題

  1. (10 点) N\leqq 15
  2. (6 点) N\leqq 500A_i \leqq 2 (1 \leqq i \leqq N).
  3. (12 点) N\leqq 500A_i \leqq 5 (1 \leqq i \leqq N).
  4. (18 点) N\leqq 500A_i \leqq 15 (1 \leqq i \leqq N).
  5. (26 点) N\leqq 500\,000A_i \leqq 15 (1 \leqq i \leqq N).
  6. (10 点) N\leqq 500\,000
  7. (18 点) 追加の制約はない.

入力例 1

5
5 3 4 2 1

出力例 1

2

k=2 のとき,例えば以下のように披露会を行うことができる.

  • まず,新型ネクタイを着用した 2 人のモデルが舞台上に登壇する.最初,各モデルのネクタイの長さはいずれも 1 である.
  • 1 回目のパフォーマンスでは,観客は 5 を唱える.あなたはこれを無視する.
  • 2 回目のパフォーマンスでは,観客は 3 を唱える.あなたはこれに反応して,1 人目のモデルを選び,そのネクタイの長さを 3 にする.各モデルのネクタイの長さはそれぞれ 3,1 になる.
  • 3 回目のパフォーマンスでは,観客は 4 を唱える.あなたはこれに反応して,1 人目のモデルを選び,そのネクタイの長さを 4 にする.各モデルのネクタイの長さはそれぞれ 4,1 になる.
  • 4 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれに反応して,2 人目のモデルを選び,そのネクタイの長さを 2 にする.各モデルのネクタイの長さはそれぞれ 4,2 になる.
  • 5 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれを無視する.

k=1 のとき,披露会は必ず失敗に終わる. 例えば,上記の進行例のように 2,3,4 回目のパフォーマンスで観客の唱えた数に反応した場合,3 回目のパフォーマンスが終了した時点で登壇している唯一のモデルのネクタイの長さが 4 になっているため, 4 回目のパフォーマンスにおいて,着用しているネクタイの長さが 2 以下であるようなモデルを選ぶことができず,披露会は失敗に終わる.

よって,披露会が失敗に終わらないために必要なモデルの人数 k の最小値は 2 であるため,2 を出力する.

この入力例は小課題 1,3,4,5,6,7 の制約を満たす.


入力例 2

6
2 1 1 2 2 1

出力例 2

1

k=1 のとき,例えば以下のように披露会を行うことができる.

  • まず,新型ネクタイを着用した 1 人のモデルが舞台上に登壇する.最初,モデルのネクタイの長さは 1 である.
  • 1 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれを無視する.
  • 2 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 1 にする.
  • 3 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 1 にする.
  • 4 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 2 にする.
  • 5 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 2 にする.
  • 6 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれを無視する.

なお,上記の進行例における 2,3 回目のパフォーマンスでは,元々ネクタイの長さが 1 であるようなモデルを選んでそのネクタイの長さを 1 にしているが,そのように,ネクタイの長さが変化しないようなモデルの選び方も許されていることに注意せよ.

よって,披露会が失敗に終わらないために必要なモデルの人数 k の最小値は 1 であるため,1 を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 3

10
2 4 6 7 4 5 5 3 4 1

出力例 3

3

この入力例は小課題 1,4,5,6,7 の制約を満たす.

E - 郵便局 (Post Office)

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score: 100 points

Problem Statement

In the JOI country, there are N post offices, numbered from 1 to N.
Each post office has an assigned "destination," and the destination of post office i is post office P_i. Note that it is possible for P_i = i.
If a package is sent from post office i at time t, it will arrive at post office P_i at time t + 1. However, a post office cannot send another package while it is in the process of sending a package.
Each post office can store an unlimited number of packages at any given time.

Now, M packages need to be delivered in the JOI country.
The j-th package arrives at post office A_j at time 0, and it must eventually be delivered to the assigned post office B_j.
Given the information about the post offices and the packages, write a program to determine whether it is possible to deliver all the packages to their assigned post offices, and if so, find the smallest possible time at which the last package arrives at its assigned post office.


Input

Read the following data from the standard input.

N
P_1 P_2 \cdots P_N
M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Output a single line to the standard output. If it is possible to deliver all the packages to their assigned post offices, output the smallest possible time at which the last package arrives at its assigned post office. Otherwise, output -1 instead.


Constraints

  • 2 \leqq N \leqq 200 \, 000.
  • 1 \leqq M \leqq 200 \, 000.
  • 1 \leqq P_i \leqq N (1 \leqq i \leqq N).
  • 1 \leqq A_j, B_j \leqq N (1 \leqq j \leqq M).
  • A_j \neq B_j (1 \leqq j \leqq M).
  • Given values are all integers.

Subtasks

  1. (3 points) N \leqq 3 \, 000M = 1.
  2. (9 points) N \leqq 3 \, 000M \leqq 3 \,000.
  3. (13 points) P = (1, 1, 2, \cdots, N-1), and \max(B_1, B_2, \dots, B_M) < \min(A_1, A_2, \dots, A_M).
  4. (25 points) P = (1, 1, 2, \cdots, N-1).
  5. (11 points) P = (N, 1, 2, \cdots, N-1).
  6. (25 points) P_1 = 1, P_i < i (2 \leqq i \leqq N).
  7. (14 points) No additional constraints.

Sample Input 1

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

Sample Output 1

3

For example, by sending the packages in the following way, all the packages can be delivered to their assigned post offices by time 3:

  • At time 0, packages 1, 2, and 3 are at post office 3. Send package 2 to post office 2.
  • At time 1, package 2 is at post office 2, and packages 1 and 3 are at post office 3. From post office 2, send package 2 to post office 1, and from post office 3, send package 3 to post office 2.
  • At time 2, package 2 is at post office 1, package 3 is at post office 2, and package 1 is at post office 3. From post office 2, send package 3 to post office 1, and from post office 3, send package 1 to post office 2.
  • At time 3, packages 2 and 3 are at post office 1, and package 1 is at post office 2. At this point, all the packages have reached their destinations.

Since it is not possible to deliver all the packages to their assigned post offices by time 2, output 3.

This sample input satisfies the constraints of Subtasks 2, 3, 4, 6, 7.


Sample Input 2

3
2 1 3
1
1 3

Sample Output 2

-1

No matter how the packages are sent, it is impossible to deliver a package from post office 1 to post office 3, so output -1.

This sample input satisfies the constraints of Subtasks 1, 2, 7.


Sample Input 3

7
1 1 2 3 4 5 6
6
4 2
5 1
5 3
6 2
7 3
7 6

Sample Output 3

5

This sample input satisfies the constraints of Subtasks 2, 4, 6, 7.


Sample Input 4

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

Sample Output 4

4

This sample input satisfies the constraints of Subtasks 2, 5, 7.


Sample Input 5

7
1 1 1 3 3 4 4
5
6 1
6 3
7 1
5 1
5 1

Sample Output 5

5

This sample input satisfies the constraints of Subtasks 2, 6, 7.


Sample Input 6

11
3 1 2 5 6 7 8 4 4 5 10
6
2 1
9 8
11 8
10 4
5 6
5 7

Sample Output 6

6

This sample input satisfies the constraints of Subtasks 2, 7.

配点: 100

問題文

JOI 国には N 個の郵便局があり,それぞれ 1 から N までの番号が付けられている. 各郵便局には「送り先」が 1 つだけ指定されており,郵便局 i の送り先は郵便局 P_i である.ただし P_i = i である可能性もある. もし時刻 t に郵便局 i から荷物を一つ発送した場合, 時刻 t + 1 に郵便局 P_i にその荷物が到着する.ただし,荷物を発送している間はその郵便局から別の荷物を発送することができない. また,各郵便局には個数の制限なく荷物を保管しておくことができる.

さて,これから JOI 国では M 個の荷物を届けることになっている. j 個目の荷物は時刻 0 に郵便局 A_j に到着し,最終的に指定の郵便局 B_j に届けなければならない. 郵便局と荷物の情報が与えられたとき,すべての荷物を指定の郵便局に届けられるかを判定し,もし可能ならば最後に荷物が指定の郵便局に届く時刻として考えられる最も小さな値を求めるプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N
P_1 P_2 \cdots P_N
M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

標準出力に 1 行で出力せよ. すべての荷物を指定の郵便局に届けられる場合は,最後に荷物が指定の郵便局に届く時刻として考えられる最も小さな値を出力せよ. そうでない場合は,代わりに -1 を出力せよ.


制約

  • 2 \leqq N \leqq 200\,000
  • 1 \leqq M \leqq 200\,000
  • 1 \leqq P_i \leqq N (1 \leqq i \leqq N).
  • 1 \leqq A_j, B_j \leqq N (1 \leqq j \leqq M).
  • A_j \neq B_j (1 \leqq j \leqq M).
  • 入力はすべて整数である.

小課題

  1. (3 点) N \leqq 3 \,000M = 1
  2. (9 点) N \leqq 3 \,000M \leqq 3 \,000
  3. (13 点) P = (1, 1, 2, \cdots, N-1).また,\max(B_1, B_2, \dots, B_M) < \min(A_1, A_2, \dots, A_M) である.
  4. (25 点) P = (1, 1, 2, \cdots, N-1)
  5. (11 点) P = (N, 1, 2, \cdots, N-1)
  6. (25 点) P_1 = 1P_i < i (2 \leqq i \leqq N).
  7. (14 点) 追加の制約はない.

入力例 1

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

出力例 1

3

例えば,以下のような送り方をすることによって時刻 3 までにすべての荷物を指定の郵便局に届けることができる.

  • 時刻 0 には,郵便局 3 に荷物 1,2,3 がある.このうち荷物 2 を郵便局 2 へ発送する.
  • 時刻 1 には,郵便局 2 に荷物 2,郵便局 3 に荷物 1,3 がある.郵便局 2 からは荷物 2 を郵便局 1 へ発送し,郵便局 3 からは荷物 3 を郵便局 2 へ発送する.
  • 時刻 2 には,郵便局 1 に荷物 2,郵便局 2 に荷物 3,郵便局 3 に荷物 1 がある.郵便局 2 からは荷物 3 を郵便局 1 へ発送し,郵便局 3 からは荷物 1 を郵便局 2 へ発送する.
  • 時刻 3 には,郵便局 1 に荷物 2,3,郵便局 2 に荷物 1 がある.このとき,すべての荷物が送り先に届いている.

時刻 2 までにすべての荷物を指定の郵便局に届けることはできないため,3 を出力する.

この入力例は小課題 2,3,4,6,7 の制約を満たす.


入力例 2

3
2 1 3
1
1 3

出力例 2

-1

どのような送り方をしても郵便局 1 から郵便局 3 へ荷物を運ぶことはできないため,-1 を出力する.

この入力例は小課題 1,2,7 の制約を満たす.


入力例 3

7
1 1 2 3 4 5 6
6
4 2
5 1
5 3
6 2
7 3
7 6

出力例 3

5

この入力例は小課題 2,4,6,7 の制約を満たす.


入力例 4

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

出力例 4

4

この入力例は小課題 2,5,7 の制約を満たす.


入力例 5

7
1 1 1 3 3 4 4
5
6 1
6 3
7 1
5 1
5 1

出力例 5

5

この入力例は小課題 2,6,7 の制約を満たす.


入力例 6

11
3 1 2 5 6 7 8 4 4 5 10
6
2 1
9 8
11 8
10 4
5 6
5 7

出力例 6

6

この入力例は小課題 2,7 の制約を満たす.