A - Atcoder Handles

Time Limit: 1 sec / Memory Limit: 256 MB

Max Score: 250 Points

Problem Statement

Mr.X, who the handle name is T, looked at the list which written N handle names, S_1, S_2, ..., S_N.
But he couldn't see some parts of the list. Invisible part is denoted ?.

Please calculate all possible index of the handle name of Mr.X when you sort N+1 handle names (S_1, S_2, ..., S_N and T) in lexicographical order.
Note: If there are pair of people with same handle name, either one may come first.

Input

The input is given from standard input in the following format.
N
S_1
S_2
 :
S_N
T

Output

  • Calculate the possible index and print in sorted order. The output should be separated with a space. Don't print a space after last number.
  • Put a line break in the end.

Constraints

  • 1 ≤ N ≤ 10000
  • 1 ≤ |S_i|, |T| ≤ 20 (|A| is the length of A.)
  • S_i consists from lower-case alphabet and ?.
  • T consists from lower-case alphabet.

Scoring

Subtask 1 [ 130 points ]

  • There are no ?'s.

Subtask 2 [ 120 points ]

  • There are no additional constraints.

Sample Input 1

2
tourist
petr
e

Sample Output 1

1
There are no invisible part, so there are only one possibility. The sorted handle names are e, petr, tourist, so e comes first.

Sample Input 2

2
?o?r?s?
?et?
e

Sample Output 2

1 2 3
If ?o?r?s? is tourist, and ?et? is petr, the sorted handle names are e, petr, tourist, so e comes first.
If ?o?r?s? is aobrcsd, and ?et? is petr, the sorted handle names are aobrcsd, e, petr, so e comes second.
If ?o?r?s? is aobrcsd, and ?et? is dete, the sorted handle names are aobrcsd, dete, e, so e comes third.
So all indexes are possible.

Sample Input 3

4
e
e
e
e
e

Sample Output 3

1 2 3 4 5
If people have same handle name, which one may come first.

Sample Input 4

5
??
??
d?
?e
?f
zzz

Sample Output 4

6

Sample Input 5

7
atcoder
topcoder
codeforces
hackerrank
csacademy
codechef
atcoder
square

Sample Output 5

7

Sample Input 6

7
??i?
?o???g????
??m??x?
?h?????i
s????
?og????
u??
square

Sample Output 6

1 2 3 4 5 6 7

配点: 250

問題文

人物Xは、N 個のハンドルネーム S_1, S_2, ..., S_N が書かれたリストを見た。
しかし、そのリストの一部は見えない。見えない箇所は ? で表される。
人物Xのハンドルネーム T がもしリストに入った場合、人物X含む N+1 人を辞書順で並び替えたときに何番目の可能性があるか、すべて求めなさい。
ただし、名前が同じ人がいた場合、どちらが先に来る可能性もあることに注意せよ。

入力

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

N
S_1
S_2
 :
S_N
T

出力

  • 何番目がありうるかをすべて求め、数字の昇順で空白区切りで出力すること。(最後の数字の後には空白をつけない)
  • また、最後には改行を入れること。

制約

  • 1 ≤ N ≤ 10000
  • 1 ≤ |S_i|, |T| ≤ 20 (ここでは |A| を文字列 A の長さとする)
  • S_i は英小文字または ? で構成される。
  • T は英小文字で構成される。

得点

小課題1 [ 130 点 ]

  • リストに見えない部分が存在しない。

小課題2 [ 120 点 ]

  • 追加の制約はない。

入力例1

2
tourist
petr
e

出力例1

1
見えない部分はないので、3個のハンドルネームを辞書順で表すと、e, petr, tourist の順番である。
よって、e は1番目である。

入力例2

2
?o?r?s?
?et?
e

出力例2

1 2 3
もし、?o?r?s?tourist であり、?et?petr の場合、e, petr, tourist の順になり、e は1番目になる。
もし、?o?r?s?aobrcsd であり、?et?petr の場合、aobrcsd, e, petr の順になり、e は2番目になる。
もし、?o?r?s?aobrcsd であり、?et?dete の場合、aobrcsd, dete, e の順に並び、e は3番目になる。
よって、すべての可能性がありうる。

入力例3

4
e
e
e
e
e

出力例3

1 2 3 4 5
同じ名前の人が複数人いる場合、どの位置に来る可能性もあることに注意せよ。

入力例4

5
??
??
d?
?e
?f
zzz

出力例4

6

入力例5

7
atcoder
topcoder
codeforces
hackerrank
csacademy
codechef
atcoder
square

出力例5

7

入力例6

7
??i?
?o???g????
??m??x?
?h?????i
s????
?og????
u??
square

出力例6

1 2 3 4 5 6 7

B - Buildings are Colorful!

Time Limit: 1 sec / Memory Limit: 256 MB

Max Score: 350 Points

Problem Statement

There are N buildings along the line. The i-th building from the left is colored in color i, and its height is currently a_i meters.
Chokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least K buildings from the left.

You can increase height of buildings, but it costs 1 yens to increase 1 meters. It means you cannot make building that height is not integer.
You cannot decrease height of buildings.
Calculate the minimum cost of satisfying Chokudai's objective.
Note: "Building i can see from the left" means there are no j exists that (height of building j) ≥ (height of building i) and j < i.

Input Format

N K
a_1 a_2 a_3 ... a_N

Output Format

Print the minimum cost in one line. In the end put a line break.

Constraints

  • 1 ≤ K ≤ N ≤ 15
  • 1 ≤ a_i ≤ 10^9

Scoring

Subtask 1 [120 points]
  • N = K
Subtask 2 [90 points]
  • N ≤ 5
  • a_i ≤ 7
Subtask 3 [140 points]
  • There are no additional constraints.

Sample Input 1

5 5
3949 3774 3598 3469 3424

Sample Output 1

1541
The optimal solution is (height of buildings from the left) = [3949, 3950, 3951, 3952, 3953].

Sample Input 2

5 3
7 4 2 6 4

Sample Output 2

7
The optimal solution is (height of buildings from the left) = [7, 8, 2, 9, 4].
配点:350

問題文

N 個の建物が左から右へと一直線上に並んでいます。左から i 番目の建物は色 i で塗られており、高さは現在 a_i です。 高橋君は市長である。彼はカラフルなものが好きなので、左から見たときに K 色以上の色の建物が見えるという条件を満たしてほしいと思いました。

現在の状況から、建物の高さを増やすことができます。しかし、高さを 1 増やすごとに 1 円かかります。また、高さを減らすことはできません。
そのとき、高橋君の目的を達成するために必要な金額を求めなさい。
ただし、「建物 i が見える」とは、建物jの高さ ≥ 建物iの高さ (j < i) となるような j が存在しないのと同じものとします。

入力

N K
a_1 a_2 a_3 ... a_N

出力

最小金額を 1 行に出力しなさい。
また、最後には改行を入れること。

制約

  • 1 ≤ K ≤ N ≤ 15
  • 1 ≤ a_i ≤ 10^9

得点

小課題1 [120 点]
  • N = K
小課題2 [90 点]
  • N ≤ 5
  • a_i ≤ 7
小課題3 [140 点]
  • 追加の制約はない。

入力例1

5 5
3949 3774 3598 3469 3424

出力例1

1541
建物の高さを左から順に 3949, 3950, 3951, 3952, 3953 とすると高橋君はすべての建物を見ることができます。

入力例2

5 3
7 4 2 6 4

出力例2

7
建物の高さを左から順に 7, 8, 2, 9, 4 とするとコスト 7 で目標を達成することができます。
C - Calendar 2

Time Limit: 1 sec / Memory Limit: 256 MB

Max Score: 500 Points

Problem Statement

We have a grid with n rows and 7 columns. We call it a calendar. The cell at i-th row and j-th column is denoted (i, j).
Initially, each cell at (i, j) contains the integer 7i + j - 8, and each cell is white.

Snuke likes painting, so he decided integer m, and did q operations with a calendar.
・In i-th operation, he paint black on the cell in which an integer is written such remainder of dividing by m is a_i.

Please count the number of connected white parts.
Note that if two adjacent cells are white, the cells belong to the same connected part.

Input Format

The input format is following:
n m q
a_1 a_2 ... a_q

Output Format

Print the number of connected part in one line.

Constraints

  • n10^{12}
  • 7n is divisible by m.
  • 1 ≤ qm10^5
  • 0a_1 < a_2 < ... < a_q < m

Scoring

Subtask 1 [100 points]
  • n100000.
Subtask 2 [90 points]
  • m is divisible by 7.
  • a_{i + 1} - a_i = 1.
Subtask 3 [200 points]
  • m is divisible by 7.
Subtask 4 [110 points]
  • There are no additional constraints.

Sample Input 1

7 7 3
1 3 5

Sample Output 1

4
The calendar looks like this:

Sample Input 2

10 14 8
5 6 7 8 9 10 11 12

Sample Output 2

10
The calendar looks like this:
配点:500

問題文

n × 7 のカレンダーがある。上から i 番目のマス、左から j 番目のマスを (i, j) で表す。
そのとき、マス (i, j) には整数 7i + j - 8 が書かれている。つまりカレンダーは次のようになっている。
すぬけ君は、塗り絵が大好きなので、このカレンダーを使って次のような遊びをした。
各マスは最初白く塗られており、すぬけ君は整数 m を決め、次のような操作を q 回行った。
  • i 回目の操作では、m で割った余りが a_i であるマスを黒く塗る。
すぬけ君は、白い部分は何個に分かれるか求めたかったので、白い連結な部分の個数を求めなさい。
ただし、白いマスに対して上下左右の4方向に白いマスがあれば、このマスは同じ連結な部分とみなすことにする。

入力

入力は、次の形式で与えられる。
n m q
a_1 a_2 ... a_q

出力

連結な部分の個数を1行に出力しなさい。

制約

  • n10^{12}
  • 7nm で割り切れる。
  • 1 ≦ qm10^5
  • 0a_1 < a_2 < ... < a_q < m

得点

小課題1 [100 点]
  • n100000.
小課題2 [90 点]
  • m7 の倍数
  • a_{i + 1} - a_i = 1.
小課題3 [200 点]
  • m7 の倍数
小課題4 [110 点]
  • 追加の制約はない。

入力例1

7 7 3
1 3 5

出力例1

4
次のようなカレンダーになる。よって、連結な部分の個数は 4 となる。

入力例2

10 14 8
5 6 7 8 9 10 11 12

出力例2

10
次のようなカレンダーになる。よって、連結な部分の個数は 14 10 (2020/8/08 訂正) となる。
D - Driving on a Tree

Time Limit: 1 sec / Memory Limit: 256 MB

Max Score: $800$ Points

Problem Statement

There is an undirected connected graph with $N$ vertices and $N-1$ edges. The i-th edge connects u_i and v_i.

E869120 the coder moves in the graph as follows:
  • He move to adjacent vertex, but he can't a visit vertex two or more times.
  • He ends move when there is no way to move.
  • Otherwise, he moves randomly. (equal probability) If he has $p$ way to move this turn, he choose each vertex with $1/p$ probability.
Calculate the expected value of the number of turns, when E869120 starts from vertex i, for all i (1 ≤ i ≤ N).

Input

The input is given from standard input in the following format.

N
u_1 v_1
u_2 v_2
 :
u_{N-1} v_{N-1}

Output

In i-th (1 ≤ i ≤ N) line, print the expected vaule of the number of turns E869120 moves.
The relative error or absolute error of output should be within 10^{-6}.

Constraints

  • $1 \le N \le 150,000$
  • The graph is connected.

Subtasks

Subtask 1 [ $190$ points ]

  • There is no vertex which degree is more than 2.
  • This means the graph looks like a list.

Subtask 2 [ $220$ points ]

  • 1 ≤ N ≤ 1000.

Subtask 3 [ $390$ points ]

  • There are no additional constraints.

Sample Input 1

4
1 2
2 3
2 4

Sample Output 1

2.0
1.0
2.0
2.0

Sample Input 2

4
1 2
2 4
4 3

Sample Output 2

3.0
1.5
3.0
1.5

Sample Input 3

5
1 2
2 3
3 4
4 5

Sample Output 3

4.0
2.0
2.0
2.0
4.0

Sample Input 4

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

Sample Output 4

2.000000000000
1.666666666667
1.666666666667
3.000000000000
3.000000000000
3.000000000000
3.000000000000

Sample Input 5

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

Sample Output 5

3.666666666667
2.250000000000
3.666666666667
2.833333333333
2.555555555556
2.666666666667
4.333333333333
2.666666666667
5.333333333333
2.500000000000
2.500000000000
5.000000000000

Sample Input 6

2
1 2

Sample Output 6

1.0
1.0
配点:$800$ 点

問題文

$N$頂点$N-1$辺の連結であるグラフ、つまり、「木」が与えられます。辺 i は頂点 u_iv_i を結んでいます。
E869120は以下のような操作を行えなくなるまで繰り返します。
  • 隣り合った頂点に動く。ただし、同じ頂点を2度通ってはいけない。
  • 動ける頂点がない場合、そこで操作は終了となる。
  • どこに動くかは等確率にランダムに選ぶ。つまり、次に動ける頂点が$p$個である場合、それぞれの頂点に$1/p$の確率で動くことになる。
最初、頂点 i にE869120君がいるとき、動く回数の期待値をすべての i に対して計算しなさい。

入力

入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
 :
u_{N-1} v_{N-1}

出力

  • $i$行目に、頂点$i$から出発した場合の動く回数の期待値を出力しなさい。
  • ただし、絶対誤差もしくは相対誤差は$10^{-6}$以内でなければなりません。

制約

  • $1 \le N \le 150,000$
  • 与えられるグラフは連結である。

小課題

小課題1 [ $190$点 ]

  • 与えられるグラフは線のようになっている。つまり、どの頂点からも辺が$3$本以上出ていることはない。

小課題2 [ $220$ 点 ]

  • $1 \le N \le 1000$

小課題3 [ $390$ 点 ]

  • 追加の制約はない。

入力例1

4
1 2
2 3
2 4

出力例1

2.0
1.0
2.0
2.0

入力例2

4
1 2
2 4
4 3

出力例2

3.0
1.5
3.0
1.5

入力例3

5
1 2
2 3
3 4
4 5

出力例3

4.0
2.0
2.0
2.0
4.0

入力例4

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

出力例4

2.000000000000
1.666666666667
1.666666666667
3.000000000000
3.000000000000
3.000000000000
3.000000000000

入力例5

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

出力例5

3.666666666667
2.250000000000
3.666666666667
2.833333333333
2.555555555556
2.666666666667
4.333333333333
2.666666666667
5.333333333333
2.500000000000
2.500000000000
5.000000000000

入力例6

2
1 2

出力例6

1.0
1.0

E - Enormous Atcoder Railroad

Time Limit: 1 sec / Memory Limit: 512 MB

Max Score: 1000 Points

Problem Statement

There is a railroad company in Atcoder Kingdom, "Atcoder Railroad".
There are N + 1 stations numbered 0, 1, 2, ..., N along a railway.
Currently, two kinds of train are operated, local and express.
A local train stops at every station, and it takes one minute from station i to i + 1, and vice versa.
An express train only stops at station S_0, S_1, S_2, ..., S_{K-1} (0 = S_0 < S_1 < S_2 < ... < S_{K-1} = N). It takes one minute from station S_i to S_{i + 1}, and vice versa.
But the president of Atcoder Railroad, Semiexp said it is not very convenient so he planned to operate one more kind of train, "semi-express".
The stations where the semi-express stops (This is T_0, T_1, T_2, ..., T_{L-1}, 0 = T_0 < T_1 < T_2 < ... < T_{L-1} = N) have to follow following conditions:
From station T_i to T_{i+1} takes 1 minutes, and vice versa.
  • The center of Atcoder Kingdom is station 0, and you have to be able to go to station i atmost X minutes.
  • If the express stops at the station, semi-express should stops at the station.
Print the number of ways of the set of the station where semi-express stops (sequence T).
Since the answer can be large, print the number modulo 10^9 + 7.

Input Format

N K X
S_0 S_1 S_2 ... S_{K-1}

Output Format

Print the number of ways of the set of the station where semi-express stops, mod 10^9 + 7 in one line.
Print \n (line break) in the end.

Constraints

  • 2 ≤ K ≤ 2500.
  • 1 ≤ X ≤ 2500.
  • S_0 = 0, S_{K-1} = N.
  • 1 ≤ S_{i + 1} - S_i ≤ 10000.

Scoring

Subtask 1 [120 points]
  • N, K, X ≤ 15.
Subtask 2 [90 points]
  • K, X ≤ 15.
  • S_{i + 1} - S_i ≤ 15.
Subtask 3 [260 points]
  • K, X ≤ 40.
  • S_{i + 1} - S_i ≤ 40.
Subtask 4 [160 points]
  • K, X ≤ 300.
  • S_{i + 1} - S_i ≤ 300.
Subtask 5 [370 points]
  • There are no additional constraints.

Sample Input 1

7 2 3
0 7

Sample Output 1

55
The set of trains that stops station 0 and 7, and can't satisfy the condition is:
[0, 7], [0, 1, 7], [0, 1, 2, 7], [0, 1, 6, 7], [0, 1, 2, 6, 7], [0, 1, 2, 3, 6, 7], [0, 1, 2, 5, 6, 7], [0, 1, 2, 3, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7], 9 ways.
Therefore, the number of ways is 2^6 - 9 = 55.
配点:1000

問題文

Atcoder国には、「Atcoder鉄道」という鉄道会社がある。
これは、駅 0 から駅 N までの N + 1 個の駅で成り立っており、一直線上に並んでいる。
普通電車はすべての駅に停車し、駅 ii + 1 の間を1分で走行する。(両方向に行ける)
急行電車は駅 S_0, S_1, S_2,..., S_{K-1} に停車し、駅 S_iS_{i + 1} の間を1分で走行する。(両方向に行ける)
しかし、これだけでは足りないと思ったAtcoder鉄道の社長であるsemiexpさんは、新たに「準急列車」というものを作ろうと計画した。
準急列車の停車する駅 T_0, T_1, T_2,..., T_{L-1} (L は準急電車の停車駅の個数で、0 = T_0 < T_1 < T_2 < ... < T_{L-1} = N) は、次の条件を満たすように決めることにしました。
ただし、その場合は駅 T_iT_{i + 1} の間を1分で走行するものとする。(両方向に行ける)
  • Atcoder国の中心地は駅0なので、普通、急行、準急を最適に使うことによって駅0から乗車時間 X 分以内ですべての街にたどり着けるようにしたい。
  • 急行列車の停車する駅は、必ず準急列車が停車しなければならない。
そのとき、準急列車が停車する駅の組み合わせの個数を求めなさい。
ただし、こたえが大きくなる可能性があるので、10^9 + 7 で割った余りを求めなさい。

入力

N K X
S_0 S_1 S_2 ... S_{K-1}

出力

通り数を 10^9 + 7 で割った余りを1行に出力しなさい。
最後には改行を入れること。

制約

  • 2 ≤ K ≤ 2500.
  • 1 ≤ X ≤ 2500.
  • S_0 = 0, S_{K-1} = N.
  • 1 ≤ S_{i + 1} - S_i ≤ 10000.

得点

小課題1 [120 点]
  • N, K, X ≤ 15.
小課題2 [90 点]
  • K, X ≤ 15
  • .
  • S_{i + 1} - S_i ≤ 15.
小課題3 [260 点]
  • K, X ≤ 40.
  • S_{i + 1} - S_i40.
小課題4 [160 点]
  • K, X ≤ 300.
  • S_{i + 1} - S_i ≤ 300.
小課題5 [370 点]
  • 追加の制約はない。

入力例1

7 2 3
0 7

出力例1

55
目的を達成できない駅の集合は、[0, 7], [0, 1, 7], [0, 1, 2, 7], [0, 1, 6, 7], [0, 1, 2, 6, 7], [0, 1, 2, 3, 6, 7], [0, 1, 2, 5, 6, 7], [0, 1, 2, 3, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7]9 個です。
よって、合計で 2^6 - 9 = 55 個となります。
F - Find the Route!

Time Limit: 2 sec / Memory Limit: 512 MB

Max Score: 1150 Points
Task statement was updated.

Problem Statement

There is a grid which size is $H \times W$. the upper-left cell is $(1,1)$ and the lower-right cell is $(H,W)$.
There is $N$ arrows. Arrow which start point is $(a_i,b_i)$ of direction is $c_i$, and size is $d_i$. ($d_i$ may be negative)
It is guaranteed that there are no two arrow which start point is same.

Sothe want to move from cell $(sx,sy)$ to cell $(gx,gy)$ with arrows.
But it may not possible to move to goal in initial grid.
So, Snuke decided to change some arrows.

Sothe can change each arrow as follows:
  • He can't change the start point of this arrow.
  • It costs $e_i$ if he change the direction of this arrow.
  • It costs $f \times |d_i-G|$ if he change d_i to $G$.

He can't add or erase arrows.

Please calculate the minimum cost that he can move to $(gx,gy)$.

If he can't move to goal, please output '-1'.

Note: Arrows are directed, and he can't turn in the middle of the arrow.

Input

The input is given from standard input in the following format.

H W N f
sx sy gx gy
a_1 b_1 c_1 d_1 e_1
a_2 b_2 c_2 d_2 e_2
 :   :   :
a_N b_N c_N d_N e_N

Output

  • Please output a single integer: The minimum cost to clear this puzzle.
  • If you can't achieve the objective, print -1.
  • Print \n (line break) in the end.

Constraints

  • $1 \le H,W \le 100000$
  • $1 \le N \le 70000$
  • $1 \le f,e_i \le 1000000$
  • $1 \le d_i \le 100000$
  • $1 \le a_i,sx,tx \le H$
  • $1 \le b_i,sy,ty \le W$
  • $c_i$ is N, E, S, or W, which means North, East, South, West.

Subtasks

Subtask 1 [ 190 points ]

  • $H=1$
  • $W \le 600$

Subtask 2 [ 170 points ]

  • $H,W \le 80$

Subtask 3 [ 360 points ]

  • $H,W \le 600$

Subtask 4 [ 430 points ]

  • There is no additional constraints.

Sample Input 1

4 4 2 2
1 1 2 2
1 1 E 1 1
1 2 E 2 2

Sample Output 1

4

Sample Input 2

1 4 2 10
1 1 1 4
1 1 E 1 4
1 3 W 1 4

Sample Output 2

14

Sample Input 3

1 8 4 9
1 3 1 6
1 1 E 7 2
1 8 W 7 5
1 3 W 2 5
1 6 E 2 8

Sample Output 3

14

Sample Input 4

5 5 7 10
1 2 4 5
1 2 E 2 6
2 3 S 2 7
3 1 N 1 8
3 2 W 1 10
4 1 E 4 12
5 5 N 3 13
5 1 E 2 14

Sample Output 4

14
配点: $1150$ 点

問題文

$H \times W$のグリッドがあります。左上の座標は$(1,1)$で、右下の座標は$(H,W)$です。
N 個のマスからは矢印が引かれており、座標$(a_i,b_i)$から出ている矢印の先は、方向$c_i$に、距離$d_i$飛んだ場所、となっています。
また、同じマスから複数の矢印が出ていることはありません。

そこで、E869120は座標$(sx,sy)$から$(gx,gy)$へ矢印のみをたどって行きたいと思っています。
しかし、今の盤面だとゴールまで行けない場合があります。
そこで、E869120はグリッドを変えることにしました。しかし、グリッドを変えるのにはコストがかかります。

彼は、各矢印の方向や向きを以下のように変えることができる。
  • 方向$c_i$を変えるのにコスト$e_i$かかる。
  • ・距離 $d_i$ の値を $G$ に変えるのに $f*|d_i-G|$ かかる。ただし、$|p|$ は $p$ の絶対値。($d_i$ は負の値も取りうる。また、$f$はどの矢印についても共通の値である)
  • ただし、$d_i$の値を負にしてもよい。その場合、その矢印は逆向きになり、矢印の大きさは $|d_i|$ となる。


そのとき、スタートからゴールまで矢印のみをたどって行けるような盤面にするための、最小コストを求めてください。
ただし、矢印は一方通行であり、矢印の途中で曲がったりすることはできないとします。

ただし、グリッドを変えてもゴールにたどり着けない場合、「-1」と出力すること。

注意:ここでいう座標系は、(p1,p2)という形で表され、p1が小さい方が北側、p2 が小さい方が左側(西側)である。


入力

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

H W N f
sx sy gx gy
a_1 b_1 c_1 d_1 e_1
a_2 b_2 c_2 d_2 e_2
 :   :   :
a_N b_N c_N d_N e_N

出力

スタートからゴールにたどり着くための、矢印を変えるコストの合計を最小値を1行に出力しなさい。
また、最後には改行を入れること。

制約

  • $1 \le H,W \le 100000$
  • $1 \le N \le 70000$
  • $1 \le f,e_i \le 1000000$
  • $1 \le d_i \le 100000$
  • $1 \le a_i,sx,tx \le H$
  • $1 \le b_i,sy,ty \le W$
  • $c_i$はN,E,S,Wのどれかである。Nは上方向、Eは東方向。

小課題

小課題1 [ $190$点 ]

  • H=1を満たす。
  • W≦600を満たす。

小課題2 [ $170$ 点 ]

  • H,W≦80を満たす。

小課題3 [ $360$ 点 ]

  • H,W≦600を満たす。

小課題4 [ $430$ 点 ]

  • 追加の制約はない。

入力例1

4 4 2 2
1 1 2 2
1 1 E 1 1
1 2 E 2 2

出力例1

4

入力例2

1 4 2 10
1 1 1 4
1 1 E 1 4
1 3 W 1 4

出力例2

14

入力例3

1 8 4 9
1 3 1 6
1 1 E 7 2
1 8 W 7 5
1 3 W 2 5
1 6 E 2 8

出力例3

14

入力例4

5 5 7 10
1 2 4 5
1 2 E 2 6
2 3 S 2 7
3 1 N 1 8
3 2 W 1 10
4 1 E 4 12
5 5 N 3 13
5 1 E 2 14

出力例4

14

G - Get the Salary of Atcoder

Time Limit: 2.5 sec / Memory Limit: 1024 MB

Max Score: 1450 Points

Problem Statement

There are N workers in Atcoder company. Each worker is numbered 0 through N - 1, and the boss for worker i is p_i like a tree structure and the salary is currently a_i. (p_i < i, especially p_0 = -1 because worker 0 is a president)
In atcoder, the boss of boss of boss of ... (repeated k times) worker i called "k-th upper boss", and "k-th lower subordinate" called for vice versa.

You have to process Q queries for Atcoder:
  • Query 1: You are given v_i, d_i, x_i. Increase the salary of worker v_i, and all j-th (1 ≤ j ≤ d_i) lower subordinates by x_i.
  • Query 2: You are given v_i, d_i. Calculate the sum of salary of worker v_i and all j-th (1 ≤ j ≤ d_i) lower subordinates.
  • Query 3: You are given pr_i, ar_i. Now Atcoder has a new worker c! (c is the current number of workers) The boss is pr_i, and the first salary is ar_i.
Process all queries!!!

Input Format

Let the i-th query query_i, the input format is following:
N Q
p_0 a_0
p_1 a_1
 :   :
p_{N - 1} a_{N - 1}
query_0
query_1
 :   :
query_{Q - 1}
THe format of query_i is one of the three format:
1 v_i d_i x_i
2 v_i d_i
3 pr_i ar_i

Output Format

Print the result in one line for each query 2.

Constraints

  • N ≤ 400000
  • Q ≤ 50000
  • p_i < i for all valid i.
  • In each question 1 or 2, worker v_i exists.
  • d_i ≤ 400000
  • 0 ≤ a_i, x_i ≤ 1000

Scoring

Subtask 1 [170 points]
  • N, Q ≤ 5000
Subtask 2 [310 points]
  • p_i + 1 = i for all valid i.
Subtask 3 [380 points]
  • There are no query 3.
Subtask 4 [590 points]
  • There are no additional constraints.

Sample Input 1

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

Sample Output 1

15
12
30
8

Sample Input 2

7 9
-1 1
0 5
0 7
0 8
1 3
4 1
5 1
2 1 1
2 1 2
1 1 2 3
1 4 1 1
2 3 1
2 0 2
3 6 1
3 7 11
2 0 15

Sample Output 2

8
9
8
31
49
配点:1450

問題文

現在 N 人のAtcoder社員がいる。それぞれの人は 0N-1 で番号付けされている。
社員 i の直接の上司は p_i (p_i < i) である。ただし、社員 0 は社長であるため、p_0 = -1 である。また、社員 i の最初の給料は a_i である。
また、Atcoderでは、社員 i の上司の上司の上司の...上司 (k 回繰り返されるとする) を 「k 個上の上司」とし、その逆は「k 個下の部下」と呼ばれている。
また、次のような処理 / 質問を合計 Q 個処理しなければなりません。
  • タイプ1:v_i, d_i, x_i が与えられ、社員 v_i と、この j (1 ≤ j ≤ d_i) 個下の部下全員に対し、給料を x_i 上げる。(x_i は負のときもある)
  • タイプ2:v_i, d_i が与えられ、社員 v_i と、この j (1 ≤ j ≤ d_i) 個下の部下全員の給料の合計を求める。
  • タイプ3:pr_i, ar_i が与えられ、現在の社員数を c としたときに新入社員 cpr_i の直接の部下として配置する。また最初の給料を ar_i とする。
そのとき、すべての質問に答えなさい。

入力

i 番目の処理 / 質問を query_i としたとき、次のような形式で与えられる。
N Q
p_0 a_0
p_1 a_1
 :   :
p_{N - 1} a_{N - 1}
query_0
query_1
 :   :
query_{Q - 1}
また、query_i は、次の3つの形式のどれかで与えられる。
1 v_i d_i x_i
2 v_i d_i
3 pr_i ar_i

出力

各質問2に対し、答えをそれぞれ1行に出力しなさい。

制約

  • N ≤ 400000
  • Q ≤ 50000
  • p_i < i が常に成立する
  • それぞれの処理 / 質問に対し、社員 v_i はその時点で存在する
  • d_i ≤ 400000
  • 0 ≤ a_i, x_i ≤ 1000

得点

小課題1 [170 点]
  • N, Q ≤ 5000
小課題2 [310 点]
  • p_i + 1 = i がすべての i に対して成立する。
小課題4 [380 点]
  • タイプ3のクエリは存在しない。
小課題5 [590 点]
  • 追加の制約はない。

入力例1

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

出力例1

15
12
30
8

入力例2

7 9
-1 1
0 5
0 7
0 8
1 3
4 1
5 1
2 1 1
2 1 2
1 1 2 3
1 4 1 1
2 3 1
2 0 2
3 6 1
3 7 11
2 0 15

出力例2

8
9
8
31
49
H - Huge Kingdom: Atcoder

Time Limit: 4 sec / Memory Limit: 256 MB

Max Score: $1500$ Points

Problem Statement

Huge kingdom: $Atcoder$ contains $N$ towns and $N-1$ roads. This kingdom is connected.

You have to detect the kingdom's road.
In order to detect, You can ask this form of questions.
  • You can output a string $S$. Length of $S$ must be $N$ and The character of string $S$ must be represented by '$0$' or '$1$'.
  • Computer paint the towns with white or black according to the character string you output.
  • If $i$-th character of $S$ is '0', computer paint town $i$ white.
  • If $i$-th character of $S$ is '1', computer paint town $i$ black.
  • Please consider an undirected graph $G$.
  • For each edge, computer do the following processing: If both ends painted black, computer adds this edge to $G$.
  • Computer returns value $x$: sum of "diameter of tree"$^2$ about each connected components.
For example, when $S$="11001111" and the kingdom's structure is this, computer returns 10.
Please detect the structure of $Atcoder$ as few questions as possible.

Input, Output

This is a reactive problem.
The first input is as follows:

$N$
  • $N$ is number of towns in $Atcoder$.

Next, you can ask questions.
The question must be in the form as follows:
? $S$
$S$ is a string. The mean of $S$ written in the problem statement. Length of $S$ must be $N$.

The answer of question is as follows:
$x$
The mean of $x$ written in the problem statement.

Finally, your program must output as follows:
! $(a_1,b_1)$ $(a_2,b_2)$ $(a_3,b_3)$ … $(a_{N-1},b_{N-1})$
This output means your program detects all roads of $Atcoder$.
$(a_i,b_i)$ means there is a road between $a_i$ and $b_i$.

You can output roads any order, but you must output the answer on a single line.

Constraints

  • $N$=200
  • $Atcoder$ contains $N-1$ roads and this kingdom is connected.
  • All cases were made randomly.

Scoring

Let the number of questions be $L$.

  • If $L > 20000$, $score$ = $0$ points
  • If $18000 < L ≦ 20000$, $score$ = $200$ points
  • If $5000 < L ≦ 18000$, $score$ = $650-L/40$ points
  • If $4000 < L ≦ 5000$, $score$ = $800-L/20$ points
  • If $2000 < L ≦ 4000$, $score$ = $1400-L/5$ points
  • If $1200 < L ≦ 2000$, $score$ = $1500-L/4$ points
  • If $700 < L ≦ 1200$, $score$ = $1850-L/2$ points
  • If $L ≦ 700$, $score$ = $1500$ points

There is $5$ cases, so points of each case is $score/5$.

Sample Input

This case is $N=4$. This case is not include because this is not $N=200$.
N=4
0 1
1 2
1 3

Sample Output

Sample interaction is as follows:
Input from computer Output
4
? 1111
4
? 1101
4
? 1001
0
? 1100
1
? 1011
0
! (0,1) (1,2) (1,3)

In this sample, structure of $Atcoder$ is as follows:

This question is not always meaningful.
この問題はマラソン問題です。 writer解が必ずしも満点であるとは限りません。できるだけ良い点数を取りましょう。
配点: $1500$ 点

問題文

Atcoder王国は、$N$個の街と$N-1$個の道から成り立っています。
また、その王国は連結であります。つまり「木」です。

あなたは、その王国の構造を当てなければなりません。
ここでいう構造というのは、それぞれの道がどの番号の街とどの番号の街をつないでいるのか、という事です。

さて、あなたは構造を当てるにおいて、以下のような質問ができます。
  • 文字列$S$($N$文字)を出力する。その時、$S_i$=1の時、$i$番目の街を黒く塗り、$S_i$=0の時、$i$番目の街を白く塗ることを意味する。
  • $N$頂点のグラフ$G$を考える。
  • 王国の道の両端の街が、両方とも黒く塗られている場合、グラフ$G$にその辺を追加する。
  • グラフ$G$の各連結成分についての、「木の直径」を2乗した値の総和が返される。
例えば、以下のような構造で、S="11001111"の場合、以下のような値が返ってくる。
その時、できるだけ少ない質問回数で王国の構造を当てなさい。

入出力

これはリアクティブ問題である。
最初、以下のように入力される。

$N$
  • 1行に、Atcoder王国の街の個数$N$が与えられる。

次に、あなたは以下の様な質問をすることができる。
質問は、以下のような形式ですることができる。
? $S$
$S$は、質問する文字列(=街の塗り方)である。$S$は$N$文字でなければならない。

また、質問は、以下のように返される。
$d$
質問で出力された文字列$S$について、問題文で与えられた作り方でグラフ$G$を作る。
グラフ$G$の各連結成分についての、「木の直径」を2乗した値の総和が返される。

最後に、あなたは以下のような出力をしなければならない。
! $(a_1,b_1)$ $(a_2,b_2)$ $(a_3,b_3)$ … $(a_{N-1},b_{N-1})$
それは、Atcoder王国の構造を突き止めたことを示す。
$(a_i,b_i)$は、街$a_i$と街$b_i$を直接つなぐ道があることを示す。

ただし、出力する道の順番はどのようなものでも良い。また、それぞれの道を出力する方法は2通りあるが、【(1,2),(2,1)など】その出力の順番もどれでもよい。

制約

  • $N$ = $200$
  • Atcoder王国には$N-1$本の道があり、連結である
  • Atcoder王国の街の番号は$0$以上$N-1$以下である。(0-based)
  • ケースはランダムに作られた。

テストケースの生成方法

以下の操作を、街が全て連結(連結成分が1つ)になるまで繰り返す。
  • ランダムな街の番号$u$, $v$を選ぶ。
  • もし、今の状態で街$u$から街$v$まで、いくつかの道路を使ってたどり着けない場合、街$u$と$v$の間に道路を結ぶ。
  • そうでない場合、何もしない。最初の操作に戻る。

得点

あなたが質問した回数を$L$とする。その時得点の理論値は以下のようになる。

  • $L > 20000$の時、$0$点
  • $18000 < L ≦ 20000$の時、$200$点
  • $5000 < L ≦ 18000$の時、$650-L/40$点
  • $4000 < L ≦ 5000$の時、$800-L/20$点
  • $2000 < L ≦ 4000$の時、$1400-L/5$点
  • $1200 < L ≦ 2000$の時、$1500-L/4$点
  • $700 < L ≦ 1200$の時、$1850-L/2$点
  • $L ≦ 700$の時、$1500$点

ケースは5個あるので、得点は理論値を$5$で割った値になる。

入力例

このケースは、$N=4$である。実際のケースではそのようなものは存在しない。(制約を満たしていないため)
N=4
0 1
1 2
1 3

出力例

以下、やりとりの例である。
プログラムへの入力 プログラムの出力
4
? 1111
4
? 1101
4
? 1001
0
? 1100
1
? 1011
0
! (0,1) (1,2) (1,3)

その王国は、以下の図のような構造をしています。

ただし, 必ずしも意味のある質問をしているとは限らない。
また, これらの情報で必ずしも盤面が把握できるとは限らず, エスパーをすることも許されます。