Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
xy 座標平面上の 2 つの格子点 (x_1, y_1), (x_2, y_2) からの距離がともに \sqrt{5} である格子点は存在しますか?
注記
xy 座標平面上にある点のうち、x 座標と y 座標がともに整数である点を格子点と呼びます。
また、xy 平面上の 2 点 (a, b), (c, d) の距離をユークリッド距離 \sqrt{(a - c)^2 + (b-d)^2} として定義します。
参考として、xy 平面の (0, 0) の上に黒丸を、(0, 0) からの距離が \sqrt{5} となる格子点の上に白丸を書いた図は以下のようになります。(x,y いずれかが整数となる部分に目盛り線を引いています。)

制約
- -10^9 \leq x_1 \leq 10^9
- -10^9 \leq y_1 \leq 10^9
- -10^9 \leq x_2 \leq 10^9
- -10^9 \leq y_2 \leq 10^9
- (x_1, y_1) \neq (x_2, y_2)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
x_1 y_1 x_2 y_2
出力
条件を満たす格子点が存在する場合は Yes を、存在しない場合は No を出力せよ。
入力例 1
0 0 3 3
出力例 1
Yes
- 点 (2,1) と (x_1, y_1) の距離は \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5}
- 点 (2,1) と (x_2, y_2) の距離は \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5}
- 点 (2, 1) は格子点
なので点 (2, 1) は条件を満たします。よって Yes を出力します。
なお、点 (1, 2) も条件を満たすことが同様にして確認できます。
入力例 2
0 1 2 3
出力例 2
No
条件を満たす格子点は存在しません。よって No を出力します。
入力例 3
1000000000 1000000000 999999999 999999999
出力例 3
Yes
点 (10^9 + 1, 10^9 - 2) および点 (10^9 - 2, 10^9 + 1)が条件を満たします。
Score : 300 points
Problem Statement
On an xy-coordinate plane, is there a lattice point whose distances from two lattice points (x_1, y_1) and (x_2, y_2) are both \sqrt{5}?
Notes
A point on an xy-coordinate plane whose x and y coordinates are both integers is called a lattice point.
The distance between two points (a, b) and (c, d) is defined to be the Euclidean distance between them, \sqrt{(a - c)^2 + (b-d)^2}.
The following figure illustrates an xy-plane with a black circle at (0, 0) and white circles at the lattice points whose distances from (0, 0) are \sqrt{5}. (The grid shows where either x or y is an integer.)

Constraints
- -10^9 \leq x_1 \leq 10^9
- -10^9 \leq y_1 \leq 10^9
- -10^9 \leq x_2 \leq 10^9
- -10^9 \leq y_2 \leq 10^9
- (x_1, y_1) \neq (x_2, y_2)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
x_1 y_1 x_2 y_2
Output
If there is a lattice point satisfying the condition, print Yes; otherwise, print No.
Sample Input 1
0 0 3 3
Sample Output 1
Yes
- The distance between points (2,1) and (x_1, y_1) is \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5};
- the distance between points (2,1) and (x_2, y_2) is \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5};
- point (2, 1) is a lattice point,
so point (2, 1) satisfies the condition. Thus, Yes should be printed.
One can also assert in the same way that (1, 2) also satisfies the condition.
Sample Input 2
0 1 2 3
Sample Output 2
No
No lattice point satisfies the condition, so No should be printed.
Sample Input 3
1000000000 1000000000 999999999 999999999
Sample Output 3
Yes
Point (10^9 + 1, 10^9 - 2) and point (10^9 - 2, 10^9 + 1) satisfy the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の正数列 A=(A_1,A_2,\dots,A_N) が与えられます。以下で定義される長さ N の数列 B = (B_1,B_2,\dots,B_N) を求めてください。
- i=1,2,\dots,N について、B_i を次のように定義する:
- A_i と等しい要素が i の直前に出現した位置を B_i とする。そのような位置が存在しなければ B_i=-1 とする。
より正確には、正整数 j であって,A_i = A_j となる j < i が存在するならば、そのうち最大の j を B_i とする。そのような j が存在しなければ B_i=-1 とする。
- A_i と等しい要素が i の直前に出現した位置を B_i とする。そのような位置が存在しなければ B_i=-1 とする。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
B の要素を空白区切りで1行に出力せよ。
入力例 1
5 1 2 1 1 3
出力例 1
-1 -1 1 3 -1
- i=1: A_1=1 より前に 1 は現れないので、B_1=-1 です。
- i=2: A_2=2 より前に 2 は現れないので、B_2=-1 です。
- i=3: A_3=1 の直前に現れた 1 は A_1 なので、B_3=1 です。
- i=4: A_4=1 の直前に現れた 1 は A_3 なので、B_4=3 です。
- i=5: A_5=3 より前に 3 は現れないので、B_5=-1 です。
入力例 2
4 1 1000000000 1000000000 1
出力例 2
-1 -1 2 1
Score : 300 points
Problem Statement
You are given a sequence of N positive numbers, A = (A_1, A_2, \dots, A_N). Find the sequence B = (B_1, B_2, \dots, B_N) of length N defined as follows.
- For i = 1, 2, \dots, N, define B_i as follows:
- Let B_i be the most recent position before i where an element equal to A_i appeared. If such a position does not exist, let B_i = -1.
More precisely, if there exists a positive integer j such that A_i = A_j and j < i, let B_i be the largest such j. If no such j exists, let B_i = -1.
- Let B_i be the most recent position before i where an element equal to A_i appeared. If such a position does not exist, let B_i = -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the elements of B in one line, separated by spaces.
Sample Input 1
5 1 2 1 1 3
Sample Output 1
-1 -1 1 3 -1
- i = 1: There is no 1 before A_1 = 1, so B_1 = -1.
- i = 2: There is no 2 before A_2 = 2, so B_2 = -1.
- i = 3: The most recent occurrence of 1 before A_3 = 1 is A_1, so B_3 = 1.
- i = 4: The most recent occurrence of 1 before A_4 = 1 is A_3, so B_4 = 3.
- i = 5: There is no 3 before A_5 = 3, so B_5 = -1.
Sample Input 2
4 1 1000000000 1000000000 1
Sample Output 2
-1 -1 2 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
1 個の 0 のみからなる数列 A=(0) があります。
また、L と R のみからなる長さ N の文字列 S=s_1s_2\ldots s_N が与えられます。
i=1,2,\ldots ,N の順番で、次の操作を行います。
- s_i が
Lのとき、A 内にある i-1 のすぐ左に i を挿入する - s_i が
Rのとき、A 内にある i-1 のすぐ右に i を挿入する
最終的な A を求めてください。
制約
- 1\leq N \leq 5\times 10^5
- N は整数である
- |S| = N
- s_i は
LかRのいずれかである
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
最終的な A を空白区切りで出力せよ。
入力例 1
5 LRRLR
出力例 1
1 2 4 5 3 0
はじめ、A=(0) です。
s_1 が L なので、A=(1,0) となります。
s_2 が R なので、A=(1,2,0) となります。
s_3 が R なので、A=(1,2,3,0) となります。
s_4 が L なので、A=(1,2,4,3,0) となります。
s_5 が R なので、A=(1,2,4,5,3,0) となります。
入力例 2
7 LLLLLLL
出力例 2
7 6 5 4 3 2 1 0
Score : 400 points
Problem Statement
There is a sequence that contains one 0, A=(0).
Additionally, you are given a string of length N, S=s_1s_2\ldots s_N, consisting of L and R.
For each i=1, 2, \ldots, N in this order, the following will be done.
- If s_i is
L, insert i to the immediate left of i-1 in A. - If s_i is
R, insert i to the immediate right of i-1 in A.
Find the final contents of A.
Constraints
- 1\leq N \leq 5\times 10^5
- N is an integer.
- |S| = N
- s_i is
LorR.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the final contents of A, separated by spaces.
Sample Input 1
5 LRRLR
Sample Output 1
1 2 4 5 3 0
Initially, A=(0).
S_1 is L, which makes it A=(1,0).
S_2 is R, which makes it A=(1,2,0).
S_3 is R, which makes it A=(1,2,3,0).
S_4 is L, which makes it A=(1,2,4,3,0).
S_5 is R, which makes it A=(1,2,4,5,3,0).
Sample Input 2
7 LLLLLLL
Sample Output 2
7 6 5 4 3 2 1 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1,\ldots,N の並び替えである長さ N の数列 A=(A_1,\ldots,A_N) があります。
あなたは A を知りませんが、M 個の整数の組 (X_i,Y_i) について、A_{X_i}<A_{Y_i} が成り立つことを知っています。
A を一意に特定できるかどうか判定し、できるなら A を求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1\leq X_i,Y_i \leq N
- 入力は全て整数である
- 入力に矛盾しない A が存在する
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 Y_1 \vdots X_M Y_M
出力
A を一意に特定できるとき、1行目に Yes と出力し、2行目に A_1,\ldots,A_N をこの順に空白区切りで出力せよ。
A を一意に特定できないとき、No とのみ出力せよ。
入力例 1
3 2 3 1 2 3
出力例 1
Yes 3 1 2
A=(3,1,2) であると一意に特定できます。
入力例 2
3 2 3 1 3 2
出力例 2
No
A として (2,3,1),(3,2,1) の 2 通りが考えられます。
入力例 3
4 6 1 2 1 2 2 3 2 3 3 4 3 4
出力例 3
Yes 1 2 3 4
Score : 500 points
Problem Statement
There is a length-N sequence A=(A_1,\ldots,A_N) that is a permutation of 1,\ldots,N.
While you do not know A, you know that A_{X_i}<A_{Y_i} for M pairs of integers (X_i,Y_i).
Can A be uniquely determined? If it is possible, find A.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1\leq X_i,Y_i \leq N
- All values in the input are integers.
- There is an A consistent with the input.
Input
The input is given from Standard Input in the following format:
N M X_1 Y_1 \vdots X_M Y_M
Output
If A can be uniquely determined, print Yes in the first line. Then, print A_1,\ldots,A_N in the second line, separated by spaces.
If A cannot be uniquely determined, just print No.
Sample Input 1
3 2 3 1 2 3
Sample Output 1
Yes 3 1 2
We can uniquely determine that A=(3,1,2).
Sample Input 2
3 2 3 1 3 2
Sample Output 2
No
Two sequences (2,3,1) and (3,2,1) can be A.
Sample Input 3
4 6 1 2 1 2 2 3 2 3 3 4 3 4
Sample Output 3
Yes 1 2 3 4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の木 T があり、
頂点および辺はそれぞれ頂点 1, 頂点 2, \ldots, 頂点 N、辺 1, 辺 2, \ldots, 辺 (N-1) と番号づけられています。
特に、辺 i (1\leq i\leq N-1) は頂点 U_i と頂点 V_i を結んでいます。
また、各頂点には重みが定められており、最初、各頂点の重みはすべて 1 です。
Q 個のクエリが与えられるので、順に処理してください。 各クエリは次の 2 種類のいずれかです。
1 x w: 頂点 x の重みを w 増加させる。2 y: 辺 y を削除すると、T は 2 つの部分木(連結成分)に分かれる。それぞれの部分木に含まれる頂点の重みの総和をその部分木の重みとするとき、2 つの部分木の重みの差を出力する。
2 種類目のクエリについて、T から任意の辺を 1 つ選んで削除したとき、T は必ず 2 つの部分木に分かれることが証明できます。
また、2 種類目のクエリでは、実際に辺を削除しているわけではないことに注意してください。
制約
- 2\leq N\leq 3\times 10^5
- 1\leq U_i,V_i\leq N
- 1\leq Q\leq 3\times 10^5
- 1\leq x\leq N
- 1\leq w\leq 1000
- 1\leq y\leq N-1
- 入力はすべて整数
- 与えられるグラフは木である。
- 2 種類目のクエリが少なくとも 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリ \mathrm{query}_i (1\leq i\leq Q) は次の形式のいずれかで与えられる。
1 x w
2 y
出力
2 種類目のクエリの個数を K 個として、K 行出力せよ。i 行目 (1\leq i\leq K) には i 番目の 2 種類目のクエリに対する答えを出力せよ。
入力例 1
6 1 2 1 3 2 4 4 5 4 6 5 2 1 1 1 3 2 1 1 4 10 2 5
出力例 1
2 1 17
木 T の構造、頂点番号の対応は下図左の通りです。最初、各頂点の重みはすべて 1 です。
1 番目のクエリでは、辺 1 を削除することを考えます。
このとき、頂点 1 を含む部分木と頂点 2 を含む部分木に分かれます。
頂点 1 を含む部分木の重みは 2、頂点 2 を含む部分木の重みは 4 であるため、その差の 2 を出力します。(下図右)

2 番目のクエリでは、頂点 1 の重みを 3 増加させます。
3 番目のクエリでは、辺 1 を削除することを考えます。
頂点 1 を含む部分木の重みは 5、頂点 2 を含む部分木の重みは 4 であるため、その差の 1 を出力します。(下図左)
4 番目のクエリでは、頂点 4 の重みを 10 増加させます。
5 番目のクエリでは、辺 5 を削除することを考えます。
このとき、頂点 4 を含む部分木と頂点 6 のみからなる部分木に分かれます。
頂点 4 を含む部分木の重みは 18、頂点 6 のみからなる部分木の重みは 1 であるため、その差の 17 を出力します。(下図右)

よって、2 種類目のクエリの答えである 2,1,17 をこの順に改行区切りで出力します。
Score : 500 points
Problem Statement
There is a tree T with N vertices.
The vertices are labeled as vertex 1, vertex 2, \ldots, vertex N, and the edges are labeled as edge 1, edge 2, \ldots, edge (N-1).
Edge i (1\leq i\leq N-1) connects vertices U_i and V_i.
Each vertex has a weight; initially, the weight of every vertex is 1.
You are given Q queries to process in order. Each query is of one of the following two types:
1 x w: Increase the weight of vertex x by w.2 y: If edge y were removed, the tree T would be split into two subtrees (connected components). For each subtree, let its weight be the sum of the weights of its vertices. Output the difference between the weights of the two subtrees.
For a query of the second type, it can be proved that removing any edge of T always splits it into exactly two subtrees.
Note that queries of the second type do not actually delete remove the edge.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 1 \leq U_i, V_i \leq N
- 1 \leq Q \leq 3 \times 10^5
- 1 \leq x \leq N
- 1 \leq w \leq 1000
- 1 \leq y \leq N-1
- All input values are integers.
- The given graph is a tree.
- There is at least one query of the second type.
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}
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query \mathrm{query}_i (1\leq i\leq Q) is given in one of the following formats:
1 x w
2 y
Output
Let K be the number of queries of the second type. Output K lines; the i-th line (1 \leq i \leq K) should contain the answer to the i-th query of the second type.
Sample Input 1
6 1 2 1 3 2 4 4 5 4 6 5 2 1 1 1 3 2 1 1 4 10 2 5
Sample Output 1
2 1 17
The structure of the tree T and the vertex numbering are as shown on the left of the figure below. Initially, the weight of every vertex is 1.
In the 1st query, consider deleting edge 1.
The tree splits into the subtree containing vertex 1 and the subtree containing vertex 2.
The subtree with vertex 1 has weight 2; the subtree with vertex 2 has weight 4. Output the difference, 2 (figure below, right).

The 2nd query increases the weight of vertex 1 by 3.
In the 3rd query, consider deleting edge 1.
The subtree with vertex 1 has weight 5; the subtree with vertex 2 has weight 4. Output the difference, 1 (figure below, left).
The 4th query increases the weight of vertex 4 by 10.
In the 5th query, consider deleting edge 5.
The tree splits into the subtree containing vertex 4 and the subtree consisting only of vertex 6.
The subtree with vertex 4 has weight 18; the subtree with vertex 6 has weight 1. Output the difference, 17 (figure below, right).

Thus, output the answers to the queries of second type in order: 2, 1, and 17, separated by newlines.