E - Cards Query Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。

  • 1 i j \colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる
  • 2 i \coloni に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \coloni が書かれたカードが入っている箱の番号を昇順ですべて答える

ただし、以下の点に注意してください。

  • 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
  • 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • 2 番目の形式のクエリについて、
    • 1 \leq i \leq N
    • このクエリが与えられる時点で箱 i にカードが入っている
  • 3 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
  • 出力するべき数は合計で 2 \times 10^5 個以下
  • 入力はすべて整数

入力

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_qq 個目のクエリを表しており、次のいずれかの形式で与えられる。

1 i j
2 i
3 i

出力

2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。


入力例 1

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

出力例 1

1 2
1 1 2
1 4
4

クエリを順に処理していきます。

  • カードに 1 を書き込み、箱 1 に入れます。
  • カードに 2 を書き込み、箱 4 に入れます。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 4 に入っているカードに書かれた数は 1, 2 です。
    • 1, 2 の順に出力してください。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 4 に入っているカードに書かれた数は 1, 1, 2 です。
    • 12 度出力することに注意してください。
  • 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 4 には数 1 が書かれたカードが 2 枚入っていますが、41 回しか出力しないことに注意してください。
  • 2 が書かれたカードが入っている箱は箱 4 です。

入力例 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

出力例 2

1 2 200000
1

Score : 300 points

Problem Statement

We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.

  • 1 i j \colon Write the number i on a blank card and put it into box j.
  • 2 i \colon Report all numbers written on the cards in box i, in ascending order.
  • 3 i \colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.

Here, note the following.

  • In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • For a query of the second kind:
    • 1 \leq i \leq N
    • Box i contains some cards when this query is given.
  • For a query of the third kind:
    • 1 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number i when this query is given.
  • At most 2 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:

1 i j
2 i
3 i

Output

Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.


Sample Input 1

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

Sample Output 1

1 2
1 1 2
1 4
4

Let us process the queries in order.

  • Write 1 on a card and put it into box 1.
  • Write 2 on a card and put it into box 4.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1 and 2.
    • Print 1 and 2 in this order.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1, 1, and 2.
    • Note that you should print 1 twice.
  • Boxes 1 and 4 contain a card with the number 1.
    • Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
  • Boxes 4 contains a card with the number 2.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1
F - Monotonically Increasing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。

注記

ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_NB_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で AB より早いと定義されます。

  • ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。

ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。

  • 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。

制約

  • 1 \le N \le M \le 10
  • 入力は全て整数。

入力

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

N M

出力

条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。


入力例 1

2 3

出力例 1

1 2 
1 3 
2 3 

条件を満たす数列は (1,2),(1,3),(2,3)3 個です。これらを辞書順で早い方から出力します。


入力例 2

3 5

出力例 2

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

Score : 300 points

Problem Statement

Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.

Notes

For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:

  • there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.

An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:

  • A_i < A_{i+1} for all integers i (1 \le i \le N-1).

Constraints

  • 1 \le N \le M \le 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).


Sample Input 1

2 3

Sample Output 1

1 2 
1 3 
2 3 

The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.


Sample Input 2

3 5

Sample Output 2

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 
G - AtCoder Wallpaper

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

AtCoder 社の壁紙の模様を xy 平面上に表現すると、以下のようになります。

  • 以下の 3 種類の直線で領域が分割されている。
    • x = n (n は整数)
    • y = n (n は偶数)
    • x + y = n (n は偶数)
  • 各領域は白もしくは黒で塗られている。いずれかの直線で隣接する 2 領域は異なる色で塗られている。
  • (0.5, 0.5) を含む領域は黒で塗られている。

下の図は、模様の一部を表したものです。

整数 A, B, C, D が与えられます。各辺が x, y 軸に平行で、左下の頂点が (A, B) にあり右上の頂点が (C, D) にあるような長方形を考えます。この長方形の内側に存在する黒で塗られた領域の面積を求め、それを 2 倍したものを出力してください。

出力する値は整数になることが証明できます。

制約

  • -10^9 \leq A, B, C, D \leq 10^9
  • A < C かつ B < D
  • 入力はすべて整数

入力

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

A B C D

出力

答えを一行に出力せよ。


入力例 1

0 0 3 3

出力例 1

10

求めるのは、以下の正方形で囲われた領域内の黒く塗られた領域の面積です。

これは 5 なので、 2 倍した 10 を出力します。


入力例 2

-1 -2 1 3

出力例 2

11

面積は 5.5 と小数になりますが、出力するべき値は整数になります。


入力例 3

-1000000000 -1000000000 1000000000 1000000000

出力例 3

4000000000000000000

これは長方形が最大のケースですが、出力は 64bit 符号付き整数の範囲に収まります。

Score : 450 points

Problem Statement

The pattern of AtCoder's wallpaper can be represented on the xy-plane as follows:

  • The plane is divided by the following three types of lines:
    • x = n (where n is an integer)
    • y = n (where n is an even number)
    • x + y = n (where n is an even number)
  • Each region is painted black or white. Any two regions adjacent along one of these lines are painted in different colors.
  • The region containing (0.5, 0.5) is painted black.

The following figure shows a part of the pattern.

You are given integers A, B, C, D. Consider a rectangle whose sides are parallel to the x- and y-axes, with its bottom-left vertex at (A, B) and its top-right vertex at (C, D). Calculate the area of the regions painted black inside this rectangle, and print twice that area.

It can be proved that the output value will be an integer.

Constraints

  • -10^9 \leq A, B, C, D \leq 10^9
  • A < C and B < D.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

A B C D

Output

Print the answer on a single line.


Sample Input 1

0 0 3 3

Sample Output 1

10

We are to find the area of the black-painted region inside the following square:

The area is 5, so print twice that value: 10.


Sample Input 2

-1 -2 1 3

Sample Output 2

11

The area is 5.5, which is not an integer, but the output value is an integer.


Sample Input 3

-1000000000 -1000000000 1000000000 1000000000

Sample Output 3

4000000000000000000

This is the case with the largest rectangle, where the output still fits into a 64-bit signed integer.

H - Ranges on Tree

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点の根付き木が与えられます。頂点 1 が根です。
i = 1, 2, \ldots, N-1 について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。

i = 1, 2, \ldots, N について、頂点 i を根とする部分木に含まれる頂点全体からなる集合を S_i で表します。(各頂点は自身を根とする部分木に含まれます。すなわち、i \in S_i です。)

また、整数 l, r について、l 以上 r 以下の整数全体からなる集合を [l, r] で表します。 すなわち、[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace です。

整数の 2 つ組を N 個並べた列 \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) であって以下の条件を満たすものを考えます。

  • 1 \leq i \leq N を満たすすべての整数 i について、1 \leq L_i \leq R_i
  • 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について次が成り立つ
    • S_i \subseteq S_j ならば、[L_i, R_i] \subseteq [L_j, R_j]
    • S_i \cap S_j = \emptyset ならば、[L_i, R_i] \cap [L_j, R_j] = \emptyset

そのような \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) が少なくとも 1 つ存在することが示せます。 それらのうち、登場する整数の最大値 \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace が最小のものを 1 つ出力してください。(複数ある場合はどれを出力しても正解となります。)

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 入力はすべて整数
  • 与えられるグラフは木である

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

下記の形式で N 行出力せよ。すなわち、i = 1, 2, \ldots, N について、i 行目に L_iR_i を空白区切りで出力せよ。

L_1 R_1
L_2 R_2
\vdots
L_N R_N

入力例 1

3
2 1
3 1

出力例 1

1 2
2 2
1 1

(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) が問題文中の条件を満たします。
実際、[L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset が成り立ちます。
また、\max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 であり、これが最小です。


入力例 2

5
3 4
5 4
1 2
1 4

出力例 2

1 3
3 3
2 2
1 2
1 1

入力例 3

5
4 5
3 2
5 2
3 1

出力例 3

1 1
1 1
1 1
1 1
1 1

Score : 500 points

Problem Statement

You are given a rooted tree with N vertices. The root is Vertex 1.
For each i = 1, 2, \ldots, N-1, the i-th edge connects Vertex u_i and Vertex v_i.

For each i = 1, 2, \ldots, N, let S_i denote the set of all vertices in the subtree rooted at Vertex i. (Each vertex is in the subtree rooted at itself, that is, i \in S_i.)

Additionally, for integers l and r, let [l, r] denote the set of all integers between l and r, that is, [l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace.

Consider a sequence of N pairs of integers \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) that satisfies the conditions below.

  • 1 \leq L_i \leq R_i for every integer i such that 1 \leq i \leq N.
  • The following holds for every pair of integers (i, j) such that 1 \leq i, j \leq N.
    • [L_i, R_i] \subseteq [L_j, R_j] if S_i \subseteq S_j
    • [L_i, R_i] \cap [L_j, R_j] = \emptyset if S_i \cap S_j = \emptyset

It can be shown that there is at least one sequence \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big). Among those sequences, print one that minimizes \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace, the maximum integer used. (If there are multiple such sequences, you may print any of them.)

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • All values in input are integers.
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print N lines in the format below. That is, for each i = 1, 2, \ldots, N, the i-th line should contain L_i and R_i separated by a space.

L_1 R_1
L_2 R_2
\vdots
L_N R_N

Sample Input 1

3
2 1
3 1

Sample Output 1

1 2
2 2
1 1

(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) satisfies the conditions.
Indeed, we have [L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset.
Additionally, \max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 is the minimum possible value.


Sample Input 2

5
3 4
5 4
1 2
1 4

Sample Output 2

1 3
3 3
2 2
1 2
1 1

Sample Input 3

5
4 5
3 2
5 2
3 1

Sample Output 3

1 1
1 1
1 1
1 1
1 1
I - Perfect Matching on a Tree

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

N 頂点の木 T が与えられます。T の頂点には 1 から N の番号がついており、 i\,(1\leq i \leq N-1) 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。

T を用いて、N 頂点の完全グラフ G を次のように定めます。

  • G の頂点 x と頂点 y の間の辺の重み w(x,y) を、T における頂点 x と頂点 y の間の最短距離とする

G最大重み最大マッチングを一つ求めてください。すなわち、\lfloor N/2 \rfloor 個の頂点のペアの集合 M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} であって、各頂点 1,2,\dots, NM に現れる回数がたかだか 1 回であるようなもののうち、 \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) が最大であるものを一つ求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • 入力されるグラフは木である
  • 入力はすべて整数

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

答えを \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} として、以下の形式で出力せよ。答えが複数あり得る場合、そのうちどれを出力しても良い。

x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}

入力例 1

4
1 2
2 3
3 4

出力例 1

2 4
1 3

T において、頂点 2,4 間の距離は 2、頂点 1,3 間の距離は 2 なので、マッチング \{(2,4),(1,3)\} の重みは 4 です。重みが 4 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、

2 3
1 4

などを出力しても正解になります。


入力例 2

3
1 2
2 3

出力例 2

1 3

T において、頂点 1,3 間の距離は 2 なので、マッチング \{(1,3)\} の重みは 2 です。重みが 2 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、

3 1

を出力しても正解になります。

Score : 550 points

Problem Statement

You are given a tree T with N vertices. The vertices are numbered 1 to N, and the i-th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally.

Using T, define a complete graph G with N vertices as follows:

  • The weight w(x,y) of the edge between vertices x and y in G is the shortest distance between vertices x and y in T.

Find one maximum weight maximum matching in G. That is, find a set of \lfloor N/2 \rfloor pairs of vertices M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} such that each vertex 1,2,\dots, N appears in M at most once, and \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) is maximized.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • The input graph is a tree.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print a solution as \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} in the following format. If multiple solutions exist, any of them is acceptable.

x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}

Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2 4
1 3

In T, the distance between vertices 2 and 4 is 2, and the distance between vertices 1 and 3 is 2, so the weight of the matching \{(2,4),(1,3)\} is 4. There is no matching with a weight greater than 4, so this is a maximum weight maximum matching. Other acceptable outputs include:

2 3
1 4

Sample Input 2

3
1 2
2 3

Sample Output 2

1 3

In T, the distance between vertices 1 and 3 is 2, so the weight of the matching \{(1,3)\} is 2. There is no matching with a weight greater than 2, so this is a maximum weight maximum matching. Another acceptable output is:

3 1