実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
AtCoder Beginner Contest は、今回で 214 回目の開催となりました。
今までの AtCoder Beginner Contest において、出題される問題数は次のように変化しました。
- 1 回目から 125 回目までは 4 問
- 126 回目から 211 回目までは 6 問
- 212 回目から 214 回目までは 8 問
N 回目の AtCoder Beginner Contest において出題された問題数を求めてください。
制約
- 1 \leq N \leq 214
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
214
出力例 1
8
入力例 2
1
出力例 2
4
入力例 3
126
出力例 3
6
Score : 100 points
Problem Statement
This is the 214-th AtCoder Beginner Contest (ABC).
The ABCs so far have had the following number of problems.
- The 1-st through 125-th ABCs had 4 problems each.
- The 126-th through 211-th ABCs had 6 problems each.
- The 212-th through 214-th ABCs have 8 problems each.
Find the number of problems in the N-th ABC.
Constraints
- 1 \leq N \leq 214
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
214
Sample Output 1
8
Sample Input 2
1
Sample Output 2
4
Sample Input 3
126
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
3 種類の文字 . | * からなる、長さ N の文字列 S が与えられます。
S には | がちょうど 2 つ、* がちょうど 1 つ含まれます。
2 つの | で囲まれた部分の中に * が含まれるか判定してください。
含まれている場合 in と、含まれていない場合 out と出力してください。
より厳密には、* より前にある文字のいずれかが | であり、かつ、* より後ろにある文字のいずれかが | であるか判定してください。
制約
- 3\leq N\leq 100
- N は整数
- S は
.|*からなる長さ N の文字列 - S に
|はちょうど 2 個含まれる - S に
*はちょうど 1 個含まれる
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
2 つの | に囲まれた部分の中に * が含まれている場合 in と、含まれていない場合 out と 1 行に出力せよ。
入力例 1
10 .|..*...|.
出力例 1
in
2 つの | に囲まれた部分は |..*...| です。
この中に * が含まれているため、in と出力してください。
入力例 2
10 .|..|.*...
出力例 2
out
2 つの | に囲まれた部分は |..| です。
この中に * は含まれていないため、out と出力してください。
入力例 3
3 |*|
出力例 3
in
Score : 100 points
Problem Statement
You are given a string S of length N consisting of three kinds of characters: ., |, and *.
S contains exactly two | and exactly one *.
Determine whether the * is between the two |, and if so, print in; otherwise, print out.
More formally, determine whether one of the characters before the * is | and one of the characters after the * is |.
Constraints
- 3\leq N\leq 100
- N is an integer.
- S is a string of length N consisting of
.,|, and*. - S contains exactly two
|. - S contains exactly one
*.
Input
The input is given from Standard Input in the following format:
N S
Output
Print a single line containing in if the * is between the two |, and out otherwise.
Sample Input 1
10 .|..*...|.
Sample Output 1
in
Between the two |, we have |..*...|, which contains *, so you should print in.
Sample Input 2
10 .|..|.*...
Sample Output 2
out
Between the two |, we have |..|, which does not contain *, so you should print out.
Sample Input 3
3 |*|
Sample Output 3
in
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
キーエンスには世界各地に N 個の拠点があり、1 から N までの番号が付けられています。 拠点 i には W_i 人の社員が所属しており、世界標準時で 0 時のとき拠点 i は X_i 時です。
あなたはキーエンス全社で 1 時間の会議を開きたいです。 各社員は、会議の開催時間帯が所属する拠点における 9:00-18:00 の時間帯に完全に含まれる場合にのみ会議に参加できます。 なるべく多くの社員が参加できるように会議の開催時間帯を決めるとき、会議に参加できる社員の数の最大値を求めてください。
制約
- 1\leq N \leq 1000
- 1\leq W_i \leq 10^6
- 0\leq X_i < 24
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N W_1 X_1 W_2 X_2 \vdots W_N X_N
出力
会議に参加できる社員の数の最大値を出力せよ。
入力例 1
3 5 0 3 3 2 18
出力例 1
8
世界標準時で 14:00-15:00 の時間帯に会議を開催することを考えます。
- 拠点 1 における 14:00-15:00 の時間帯に会議が開催されるため、拠点 1 に所属する 5 人の社員は会議に参加できます。
- 拠点 2 における 17:00-18:00 の時間帯に会議が開催されるため、拠点 2 に所属する 3 人の社員は会議に参加できます。
- 拠点 3 における 8:00-9:00 の時間帯に会議が開催されるため、拠点 3 に所属する 2 人の社員は会議に参加できません。
よって、合計で 5+3=8 人の社員が会議に参加できます。 これより多くの社員が参加できるような会議の開催時間帯はありません。
入力例 2
2 1 10 1000000 20
出力例 2
1000000
入力例 3
6 31 3 20 8 11 5 4 3 47 14 1 18
出力例 3
67
Score : 250 points
Problem Statement
Keyence has N bases worldwide, numbered 1 to N. Base i has W_i employees, and at 0 o'clock in Coordinated Universal Time (UTC), it is X_i o'clock at base i.
You want to hold a one-hour meeting across the entire company. Each employee can only participate in the meeting if the meeting time is completely within the 9:00-18:00 time slot at their base. Find the maximum number of employees who can participate when deciding the meeting time to allow as many employees as possible to participate.
Constraints
- 1\leq N \leq 1000
- 1\leq W_i \leq 10^6
- 0\leq X_i < 24
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N W_1 X_1 W_2 X_2 \vdots W_N X_N
Output
Print the maximum number of employees who can participate in the meeting.
Sample Input 1
3 5 0 3 3 2 18
Sample Output 1
8
Consider holding the meeting from 14:00 to 15:00 in UTC.
- The meeting is held from 14:00 to 15:00 at base 1, so the 5 employees at base 1 can participate in the meeting.
- The meeting is held from 17:00 to 18:00 at base 2, so the 3 employees at base 2 can participate in the meeting.
- The meeting is held from 8:00 to 9:00 at base 3, so the 2 employees at base 3 cannot participate in the meeting.
Thus, a total of 5+3=8 employees can participate in the meeting. No meeting time allows more employees to participate.
Sample Input 2
2 1 10 1000000 20
Sample Output 2
1000000
Sample Input 3
6 31 3 20 8 11 5 4 3 47 14 1 18
Sample Output 3
67
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) と、長さ M の数列 B=(B_1,B_2,\dots,B_M) が与えられます。ここで、A,B のすべての要素は互いに相異なります。A,B のすべての要素を昇順に並べた長さ N+M の数列 C=(C_1,C_2,\dots,C_{N+M}) において、A に現れる要素が2つ連続するかどうか判定してください。
制約
- 1\leq N,M \leq 100
- 1\leq A_i,B_j \leq 200
- A_1, A_2, \dots, A_N, B_1, B_2, \dots, B_M は相異なる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
A に現れる要素が C において2つ連続するならば Yes を、そうでないなら No を出力せよ。
入力例 1
3 2 3 2 5 4 1
出力例 1
Yes
C=(1,2,3,4,5) です。A に現れる 2,3 が C で連続しているため、Yes を出力します。
入力例 2
3 2 3 1 5 4 2
出力例 2
No
C=(1,2,3,4,5) です。A に現れる要素が C で連続している箇所はないため、No を出力します。
入力例 3
1 1 1 2
出力例 3
No
Score : 200 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_N) of length N and a sequence B=(B_1,B_2,\dots,B_M) of length M. Here, all elements of A and B are pairwise distinct. Determine whether the sequence C=(C_1,C_2,\dots,C_{N+M}) formed by sorting all elements of A and B in ascending order contains two consecutive elements appearing in A.
Constraints
- 1 \leq N, M \leq 100
- 1 \leq A_i, B_j \leq 200
- A_1, A_2, \dots, A_N, B_1, B_2, \dots, B_M are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
If C contains two consecutive elements appearing in A, print Yes; otherwise, print No.
Sample Input 1
3 2 3 2 5 4 1
Sample Output 1
Yes
C=(1,2,3,4,5). Since 2 and 3 from A occur consecutively in C, print Yes.
Sample Input 2
3 2 3 1 5 4 2
Sample Output 2
No
C=(1,2,3,4,5). Since no two elements from A occur consecutively in C, print No.
Sample Input 3
1 1 1 2
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君が運営する SNS「Twidai」にはユーザー 1 からユーザー N までの N 人のユーザーがいます。 Twidai では、ユーザーは別のユーザーをフォローすることや、フォローを解除することができます。
Twidai がサービスを開始してから、Q 回の操作が行われました。 i 回目 (1\leq i\leq Q) の操作は 3 つの整数 T _ i, A _ i, B _ i で表され、それぞれ次のような操作を表します。
- T _ i=1 のとき:ユーザー A _ i がユーザー B _ i をフォローしたことを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしている場合、ユーザーのフォロー状況に変化はない。
- T _ i=2 のとき:ユーザー A _ i がユーザー B _ i のフォローを解除したことを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしていない場合、ユーザーのフォロー状況に変化はない。
- T _ i=3 のとき:ユーザー A _ i とユーザー B _ i が互いにフォローしているかをチェックすることを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしており、かつユーザー B _ i がユーザー A _ i をフォローしているとき、このチェックに対して
Yesと答え、そうでないときこのチェックに対してNoと答える必要がある。
サービス開始時には、どのユーザーも他のユーザーをフォローしていません。
すべての T _ i=3 であるような操作に対して、i が小さいほうから順番に正しい答えを出力してください。
制約
- 2 \leq N \leq 10 ^ 9
- 1 \leq Q \leq 2\times10 ^ 5
- T _ i=1,2,3\ (1\leq i\leq Q)
- 1 \leq A _ i \leq N\ (1\leq i\leq Q)
- 1 \leq B _ i \leq N\ (1\leq i\leq Q)
- A _ i\neq B _ i\ (1\leq i\leq Q)
- T _ i=3 となる i\ (1\leq i\leq Q) が存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q T _ 1 A _ 1 B _ 1 T _ 2 A _ 2 B _ 2 \vdots T _ Q A _ Q B _ Q
出力
T _ i=3 であるような i\ (1\leq i\leq Q) の個数を X として、X 行出力せよ。 j\ (1\leq j\leq X) 行目には j 番目の T _ i=3 であるような操作に対する答えを出力せよ。
入力例 1
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
出力例 1
No Yes No No
Twidai には 3 人のユーザーがいます。 9 回の操作はそれぞれ次のようになっています。
- ユーザー 1 がユーザー 2 をフォローします。そのほかにフォローしている/されているユーザーはいません。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 2 をフォローしていますが、ユーザー 2 はユーザー 1 をフォローしていません。この操作への正しい答えは
Noです。 - ユーザー 2 がユーザー 1 をフォローします。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 2 をフォローしており、ユーザー 2 はユーザー 1 をフォローしています。この操作への正しい答えは
Yesです。 - ユーザー 2 がユーザー 3 をフォローします。
- ユーザー 3 がユーザー 2 をフォローします。
- ユーザー 1 とユーザー 3 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 3 をフォローしておらず、ユーザー 3 もユーザー 1 をフォローしていません。この操作への正しい答えは
Noです。 - ユーザー 1 がユーザー 2 のフォローを解除します。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 2 はユーザー 1 をフォローしていますが、ユーザー 1 はユーザー 2 をフォローしていません。この操作への正しい答えは
Noです。
入力例 2
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
出力例 2
Yes No
同じユーザーに対して何度もフォロー操作をする場合があります。
入力例 3
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
出力例 3
No No No No Yes Yes No No No Yes Yes
Score : 300 points
Problem Statement
Takahashi runs an SNS "Twidai," which has N users from user 1 through user N. In Twidai, users can follow or unfollow other users.
Q operations have been performed since Twidai was launched. The i-th (1\leq i\leq Q) operation is represented by three integers T_i, A_i, and B_i, whose meanings are as follows:
- If T_i = 1: it means that user A_i follows user B_i. If user A_i is already following user B_i at the time of this operation, it does not make any change.
- If T_i = 2: it means that user A_i unfollows user B_i. If user A_i is not following user B_i at the time of this operation, it does not make any change.
- If T_i = 3: it means that you are asked to determine if users A_i and B_i are following each other. You need to print
Yesif user A_i is following user B_i and user B_i is following user A_i, andNootherwise.
When the service was launched, no users were following any users.
Print the correct answers for all operations such that T_i = 3 in ascending order of i.
Constraints
- 2 \leq N \leq 10 ^ 9
- 1 \leq Q \leq 2\times10 ^ 5
- T _ i=1,2,3\ (1\leq i\leq Q)
- 1 \leq A _ i \leq N\ (1\leq i\leq Q)
- 1 \leq B _ i \leq N\ (1\leq i\leq Q)
- A _ i\neq B _ i\ (1\leq i\leq Q)
- There exists i\ (1\leq i\leq Q) such that T _ i=3.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q T _ 1 A _ 1 B _ 1 T _ 2 A _ 2 B _ 2 \vdots T _ Q A _ Q B _ Q
Output
Print X lines, where X is the number of i's (1\leq i\leq Q) such that T _ i=3. The j-th (1\leq j\leq X) line should contain the answer to the j-th operation such that T _ i=3.
Sample Input 1
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
Sample Output 1
No Yes No No
Twidai has three users. The nine operations are as follows.
- User 1 follows user 2. No other users are following or followed by any users.
- Determine if users 1 and 2 are following each other. User 1 is following user 2, but user 2 is not following user 1, so
Nois the correct answer to this operation. - User 2 follows user 1.
- Determine if users 1 and 2 are following each other. User 1 is following user 2, and user 2 is following user 1, so
Yesis the correct answer to this operation. - User 2 follows user 3.
- User 3 follows user 2.
- Determine if users 1 and 3 are following each other. User 1 is not following user 3, and user 3 is not following user 1, so
Nois the correct answer to this operation. - User 1 unfollows user 2.
- Determine if users 1 and 2 are following each other. User 2 is following user 1, but user 1 is not following user 2, so
Nois the correct answer to this operation.
Sample Input 2
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
Sample Output 2
Yes No
A user may follow the same user multiple times.
Sample Input 3
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
Sample Output 3
No No No No Yes Yes No No No Yes Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
冷蔵庫に N 種類の材料があります。これらを材料 1、\dots、材料 N と呼びます。材料 i は Q_i グラムあります。
あなたは 2 種類の料理を作れます。料理 A は、1 人分を作るのに各材料 i (1 \leq i \leq N) が A_i グラム必要です。料理 B は、1 人分を作るのに各材料 i が B_i グラム必要です。どちらも整数人分しか作れません。
冷蔵庫にある材料のみを使って、最大で合計何人分の料理を作れますか。
制約
- 1 \leq N \leq 10
- 1 \leq Q_i \leq 10^6
- 0 \leq A_i \leq 10^6
- A_i \geq 1 であるような i が存在する。
- 0 \leq B_i \leq 10^6
- B_i \geq 1 であるような i が存在する。
- 入力値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q_1 Q_2 \dots Q_N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
最大で合計 S 人分の料理を作れるとして、整数 S を出力せよ。
入力例 1
2 800 300 100 100 200 10
出力例 1
5
この冷蔵庫には、800 グラムの材料 1 と 300 グラムの材料 2 があります。
100 グラムの材料 1 と 100 グラムの材料 2 で料理 A を 1 人分作れ、200 グラムの材料 1 と 10 グラムの材料 2 で料理 B を 1 人分作れます。
料理 A を 2 人分、料理 B を 3 人分作るのに必要な材料 1 の量は 100 \times 2 + 200 \times 3 = 800 グラム、材料 2 の量は 100 \times 2 + 10 \times 3 = 230 グラムで、いずれも冷蔵庫にある量を超えません。このようにして合計 5 人分の料理を作ることができますが、6 人分を作る方法はなく、答えは 5 です。
入力例 2
2 800 300 100 0 0 10
出力例 2
38
800 グラムの材料 1 で料理 A を 8 人分、300 グラムの材料 2 で料理 B を 30 人分、合計 38 人分作れます。
入力例 3
2 800 300 801 300 800 301
出力例 3
0
何も作れません。
入力例 4
10 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
出力例 4
222222
Score: 300 points
Problem Statement
Your refrigerator has N kinds of ingredients. Let us call them ingredient 1, \dots, ingredient N. You have Q_i grams of ingredient i.
You can make two types of dishes. To make one serving of dish A, you need A_i grams of each ingredient i (1 \leq i \leq N). To make one serving of dish B, you need B_i grams of each ingredient i. You can only make an integer number of servings of each type of dish.
Using only the ingredients in the refrigerator, what is the maximum total number of servings of dishes you can make?
Constraints
- 1 \leq N \leq 10
- 1 \leq Q_i \leq 10^6
- 0 \leq A_i \leq 10^6
- There is an i such that A_i \geq 1.
- 0 \leq B_i \leq 10^6
- There is an i such that B_i \geq 1.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q_1 Q_2 \dots Q_N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Assuming that you can make a maximum total of S servings of dishes, print the integer S.
Sample Input 1
2 800 300 100 100 200 10
Sample Output 1
5
This refrigerator has 800 grams of ingredient 1 and 300 grams of ingredient 2.
You can make one serving of dish A with 100 grams of ingredient 1 and 100 grams of ingredient 2, and one serving of dish B with 200 grams of ingredient 1 and 10 grams of ingredient 2.
To make two servings of dish A and three servings of dish B, you need 100 \times 2 + 200 \times 3 = 800 grams of ingredient 1, and 100 \times 2 + 10 \times 3 = 230 grams of ingredient 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5.
Sample Input 2
2 800 300 100 0 0 10
Sample Output 2
38
You can make 8 servings of dish A with 800 grams of ingredient 1, and 30 servings of dish B with 300 grams of ingredient 2, for a total of 38 servings.
Sample Input 3
2 800 300 801 300 800 301
Sample Output 3
0
You cannot make any dishes.
Sample Input 4
10 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
Sample Output 4
222222
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
以下のような、無限に広い六角形のグリッドがあります。最初、全てのマスは白です。

ある六角形のマスは 2 つの整数 i,j を用いて (i,j) と表されます。
マス (i,j) は以下の 6 つのマスと隣接します。
- (i-1,j-1)
- (i-1,j)
- (i,j-1)
- (i,j+1)
- (i+1,j)
- (i+1,j+1)
高橋くんは、 N 個のマス (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) を黒く塗りました。
黒いマスがいくつの連結成分をなすか求めてください。
ただし、ある 2 つの黒いマスが同じ連結成分に属するとは、この 2 つの黒いマスの間をいくつかの隣接する黒いマスを辿って移動できることを指します。
制約
- 入力は全て整数
- 1 \le N \le 1000
- |X_i|,|Y_i| \le 1000
- (X_i,Y_i) は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
6 -1 -1 0 1 0 2 1 0 1 2 2 0
出力例 1
3
高橋くんがマスを黒く塗った後、グリッドは下図の状態になります。

黒いマスがなす連結成分は以下の 3 つです。
- (-1,-1)
- (1,0),(2,0)
- (0,1),(0,2),(1,2)
入力例 2
4 5 0 4 1 -3 -4 -2 -5
出力例 2
4
入力例 3
5 2 1 2 -1 1 0 3 1 1 -1
出力例 3
1
Score : 400 points
Problem Statement
We have an infinite hexagonal grid shown below. Initially, all squares are white.

A hexagonal cell is represented as (i,j) with two integers i and j.
Cell (i,j) is adjacent to the following six cells:
- (i-1,j-1)
- (i-1,j)
- (i,j-1)
- (i,j+1)
- (i+1,j)
- (i+1,j+1)
Takahashi has painted N cells (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) black.
Find the number of connected components formed by the black cells.
Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell.
Constraints
- All values in the input are integers.
- 1 \le N \le 1000
- |X_i|,|Y_i| \le 1000
- The pairs (X_i,Y_i) are distinct.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
6 -1 -1 0 1 0 2 1 0 1 2 2 0
Sample Output 1
3
After Takahashi paints cells black, the grid looks as shown below.

The black squares form the following three connected components:
- (-1,-1)
- (1,0),(2,0)
- (0,1),(0,2),(1,2)
Sample Input 2
4 5 0 4 1 -3 -4 -2 -5
Sample Output 2
4
Sample Input 3
5 2 1 2 -1 1 0 3 1 1 -1
Sample Output 3
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
AtCoder 国には駅 1, 駅 2,\ldots, 駅 N の N 個の駅があります。
AtCoder 国に存在する電車の情報が M 個与えられます。 i 番目 (1\leq i\leq M) の情報は正整数の 6 つ組 (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i) で表され、次のような情報に対応しています。
- t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i それぞれについて、次のような電車が存在する。
- 時刻 t に 駅 A _ i を出発し、時刻 t+c _ i に駅 B _ i に到着する。
これらの情報にあてはまらない電車は存在せず、電車以外の方法である駅から異なる駅へ移動することはできません。
また、乗り換えにかかる時間は無視できるとします。
駅 S から駅 N に到着できる最終時刻を f(S) とします。
より厳密には、整数の 4 つ組の列 \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} であって、次のすべての条件を満たすものが存在するような t の最大値を f(S) とします。
- t\leq t _ 1
- A _ 1=S,B _ k=N
- すべての 1\leq i\lt k について、B _ i=A _ {i+1}
- すべての 1\leq i\leq k について、時刻 t _ i に駅 A _ i を出発して時刻 t _ i+c _ i に駅 B _ i に到着する電車が存在する。
- すべての 1\leq i\lt k について、t _ i+c _ i\leq t _ {i+1}
ただし、そのような t が存在しないとき f(S)=-\infty とします。
f(1),f(2),\ldots,f(N-1) を求めてください。
制約
- 2\leq N\leq2\times10 ^ 5
- 1\leq M\leq2\times10 ^ 5
- 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M)
- 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M)
- A _ i\neq B _ i\ (1\leq i\leq M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M l _ 1 d _ 1 k _ 1 c _ 1 A _ 1 B _ 1 l _ 2 d _ 2 k _ 2 c _ 2 A _ 2 B _ 2 \vdots l _ M d _ M k _ M c _ M A _ M B _ M
出力
N-1 行出力せよ。
k 行目には f(k)\neq-\infty ならば f(k) を、f(k)=-\infty ならば Unreachable を出力せよ。
入力例 1
6 7 10 5 10 3 1 3 13 5 10 2 3 4 15 5 10 7 4 6 3 10 2 4 2 5 7 10 2 3 5 6 5 3 18 2 2 3 6 3 20 4 2 1
出力例 1
55 56 58 60 17
AtCoder 国に走っている電車は以下の図のようになります(発着時間の情報は図には含まれていません)。

駅 2 から駅 6 に到着できる最終時刻について考えます。 次の図のように、駅 2 を時刻 56 に出発して駅 2\rightarrow 駅 3\rightarrow 駅 4\rightarrow 駅 6 のように移動することで駅 6 に到着することができます。

駅 2 を時刻 56 より遅く出発して駅 6 に到着することはできないため、f(2)=56 です。
入力例 2
5 5 1000000000 1000000000 1000000000 1000000000 1 5 5 9 2 6 2 3 10 4 1 6 2 3 1 1 1 1 3 5 3 1 4 1 5 1
出力例 2
1000000000000000000 Unreachable 1 Unreachable
駅 1 を時刻 10 ^ {18} に出発して駅 5 に時刻 10 ^ {18}+10 ^ 9 に到着するような電車が存在します。それ以降に駅 1 を出発する電車はないため、f(1)=10 ^ {18} です。 このように、答えが 32\operatorname{bit} 整数におさまらない場合もあります。
また、時刻 14 に駅 2 を出発して時刻 20 に駅 3 に到着するような電車は 2 番目の情報と 3 番目の情報の両方で存在が保証されています。 このように、複数の情報に重複している電車もあります。
入力例 3
16 20 4018 9698 2850 3026 8 11 2310 7571 7732 1862 13 14 2440 2121 20 1849 11 16 2560 5115 190 3655 5 16 1936 6664 39 8822 4 16 7597 8325 20 7576 12 5 5396 1088 540 7765 15 1 3226 88 6988 2504 13 5 1838 7490 63 4098 8 3 1456 5042 4 2815 14 7 3762 6803 5054 6994 10 9 9526 6001 61 8025 7 8 5176 6747 107 3403 1 5 2014 5533 2031 8127 8 11 8102 5878 58 9548 9 10 3788 174 3088 5950 3 13 7778 5389 100 9003 10 15 556 9425 9458 109 3 11 5725 7937 10 3282 2 9 6951 7211 8590 1994 15 12
出力例 3
720358 77158 540926 255168 969295 Unreachable 369586 466218 343148 541289 42739 165772 618082 16582 591828
Score: 450 points
Problem Statement
In the country of AtCoder, there are N stations: station 1, station 2, \ldots, station N.
You are given M pieces of information about trains in the country. The i-th piece of information (1\leq i\leq M) is represented by a tuple of six positive integers (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i), which corresponds to the following information:
- For each t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i, there is a train as follows:
- The train departs from station A _ i at time t and arrives at station B _ i at time t+c _ i.
No trains exist other than those described by this information, and it is impossible to move from one station to another by any means other than by train.
Also, assume that the time required for transfers is negligible.
Let f(S) be the latest time at which one can arrive at station N from station S.
More precisely, f(S) is defined as the maximum value of t for which there is a sequence of tuples of four integers \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} that satisfies all of the following conditions:
- t\leq t _ 1
- A _ 1=S,B _ k=N
- B _ i=A _ {i+1} for all 1\leq i\lt k,
- For all 1\leq i\leq k, there is a train that departs from station A _ i at time t _ i and arrives at station B _ i at time t _ i+c _ i.
- t _ i+c _ i\leq t _ {i+1} for all 1\leq i\lt k.
If no such t exists, set f(S)=-\infty.
Find f(1),f(2),\ldots,f(N-1).
Constraints
- 2\leq N\leq2\times10 ^ 5
- 1\leq M\leq2\times10 ^ 5
- 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M)
- 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M)
- A _ i\neq B _ i\ (1\leq i\leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M l _ 1 d _ 1 k _ 1 c _ 1 A _ 1 B _ 1 l _ 2 d _ 2 k _ 2 c _ 2 A _ 2 B _ 2 \vdots l _ M d _ M k _ M c _ M A _ M B _ M
Output
Print N-1 lines.
The k-th line should contain f(k) if f(k)\neq-\infty, and Unreachable if f(k)=-\infty.
Sample Input 1
6 7 10 5 10 3 1 3 13 5 10 2 3 4 15 5 10 7 4 6 3 10 2 4 2 5 7 10 2 3 5 6 5 3 18 2 2 3 6 3 20 4 2 1
Sample Output 1
55 56 58 60 17
The following diagram shows the trains running in the country (information about arrival and departure times is omitted).

Consider the latest time at which one can arrive at station 6 from station 2. As shown in the following diagram, one can arrive at station 6 by departing from station 2 at time 56 and moving as station 2\rightarrow station 3\rightarrow station 4\rightarrow station 6.

It is impossible to depart from station 2 after time 56 and arrive at station 6, so f(2)=56.
Sample Input 2
5 5 1000000000 1000000000 1000000000 1000000000 1 5 5 9 2 6 2 3 10 4 1 6 2 3 1 1 1 1 3 5 3 1 4 1 5 1
Sample Output 2
1000000000000000000 Unreachable 1 Unreachable
There is a train that departs from station 1 at time 10 ^ {18} and arrives at station 5 at time 10 ^ {18}+10 ^ 9. There are no trains departing from station 1 after that time, so f(1)=10 ^ {18}. As seen here, the answer may not fit within a 32\operatorname{bit} integer.
Also, both the second and third pieces of information guarantee that there is a train that departs from station 2 at time 14 and arrives at station 3 at time 20. As seen here, some trains may appear in multiple pieces of information.
Sample Input 3
16 20 4018 9698 2850 3026 8 11 2310 7571 7732 1862 13 14 2440 2121 20 1849 11 16 2560 5115 190 3655 5 16 1936 6664 39 8822 4 16 7597 8325 20 7576 12 5 5396 1088 540 7765 15 1 3226 88 6988 2504 13 5 1838 7490 63 4098 8 3 1456 5042 4 2815 14 7 3762 6803 5054 6994 10 9 9526 6001 61 8025 7 8 5176 6747 107 3403 1 5 2014 5533 2031 8127 8 11 8102 5878 58 9548 9 10 3788 174 3088 5950 3 13 7778 5389 100 9003 10 15 556 9425 9458 109 3 11 5725 7937 10 3282 2 9 6951 7211 8590 1994 15 12
Sample Output 3
720358 77158 540926 255168 969295 Unreachable 369586 466218 343148 541289 42739 165772 618082 16582 591828
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N + Q 頂点のグラフがあり、頂点には 1, 2, \ldots, N + Q の番号がついています。グラフにははじめ辺がありません。
このグラフに対して i = 1, 2, \ldots, Q の順に以下の操作を行います。
- L_i \leq j \leq R_i を満たす各整数 j について頂点 N + i と頂点 j の間にコスト C_i の無向辺を追加する
すべての操作を終えた後グラフは連結であるか判定し、連結である場合はこのグラフの最小全域木のコストを求めてください。
ただし、最小全域木とはコストが最小の全域木のことを指し、全域木のコストとは全域木に使われた辺のコストの和のことを指します。
制約
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq N
- 1 \leq C_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_Q R_Q C_Q
出力
グラフが連結である場合は最小全域木のコストを出力せよ。そうでない場合は -1 を出力せよ。
入力例 1
4 3 1 2 2 1 3 4 2 4 5
出力例 1
22
以下の辺からなる全域木が最小全域木のひとつとなります。
- 頂点 1 と 5 を結ぶコスト 2 の辺
- 頂点 2 と 5 を結ぶコスト 2 の辺
- 頂点 1 と 6 を結ぶコスト 4 の辺
- 頂点 3 と 6 を結ぶコスト 4 の辺
- 頂点 3 と 7 を結ぶコスト 5 の辺
- 頂点 4 と 7 を結ぶコスト 5 の辺
2 + 2 + 4 + 4 + 5 + 5 = 22 であるため、22 を出力します。
入力例 2
6 2 1 2 10 4 6 10
出力例 2
-1
グラフは非連結です。
入力例 3
200000 4 1 200000 1000000000 1 200000 998244353 1 200000 999999999 1 200000 999999999
出力例 3
199651870599998
Score : 500 points
Problem Statement
There is a graph with N + Q vertices, numbered 1, 2, \ldots, N + Q. Initially, the graph has no edges.
For this graph, perform the following operation for i = 1, 2, \ldots, Q in order:
- For each integer j satisfying L_i \leq j \leq R_i, add an undirected edge with cost C_i between vertices N + i and j.
Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph.
A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq N
- 1 \leq C_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_Q R_Q C_Q
Output
If the graph is connected, print the cost of a minimum spanning tree. Otherwise, print -1.
Sample Input 1
4 3 1 2 2 1 3 4 2 4 5
Sample Output 1
22
The following edges form a minimum spanning tree:
- An edge with cost 2 connecting vertices 1 and 5
- An edge with cost 2 connecting vertices 2 and 5
- An edge with cost 4 connecting vertices 1 and 6
- An edge with cost 4 connecting vertices 3 and 6
- An edge with cost 5 connecting vertices 3 and 7
- An edge with cost 5 connecting vertices 4 and 7
Since 2 + 2 + 4 + 4 + 5 + 5 = 22, print 22.
Sample Input 2
6 2 1 2 10 4 6 10
Sample Output 2
-1
The graph is disconnected.
Sample Input 3
200000 4 1 200000 1000000000 1 200000 998244353 1 200000 999999999 1 200000 999999999
Sample Output 3
199651870599998