Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 100 点
問題文
N 個の整数 A _ 1,A _ 2,\ldots,A _ N が与えられます。
これらの値がすべて等しいならば Yes
、そうでなければ No
と出力してください。
制約
- 2\leq N\leq100
- 1\leq A _ i\leq100\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
与えられた A _ 1,A _ 2,\ldots,A _ N の値がすべて等しいなら Yes
を、そうでなければ No
を 1 行で出力せよ。
入力例 1
3 3 2 4
出力例 1
No
A _ 1\neq A _ 2 なので、No
と出力してください。
入力例 2
4 3 3 3 3
出力例 2
Yes
A _ 1=A _ 2=A _ 3=A _ 4 なので、Yes
と出力してください。
入力例 3
10 73 8 55 26 97 48 37 47 35 55
出力例 3
No
Score : 100 points
Problem Statement
You are given N integers A _ 1,A _ 2,\ldots,A _ N.
If their values are all equal, print Yes
; otherwise, print No
.
Constraints
- 2\leq N\leq100
- 1\leq A _ i\leq100\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
Print a single line containing Yes
if the values of the given A _ 1,A _ 2,\ldots,A _ N are all equal, and No
otherwise.
Sample Input 1
3 3 2 4
Sample Output 1
No
We have A _ 1\neq A _ 2, so you should print No
.
Sample Input 2
4 3 3 3 3
Sample Output 2
Yes
We have A _ 1=A _ 2=A _ 3=A _ 4, so you should print Yes
.
Sample Input 3
10 73 8 55 26 97 48 37 47 35 55
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 200 点
問題文
正の整数 N が与えられます。
N=2^x3^y を満たす整数 x,y が存在するなら Yes
、そうでなければ No
と出力してください。
制約
- 1\leq N\leq10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
条件を満たす整数 x,y が存在するなら Yes
、そうでなければ No
と 1 行に出力せよ。
入力例 1
324
出力例 1
Yes
x=2,y=4 とすると、2^x3^y=2^23^4=4\times81=324 となるため条件を満たします。
よって、Yes
と出力してください。
入力例 2
5
出力例 2
No
どのような整数 x,y をとっても 2^x3^y=5 とすることはできません。
よって、No
と出力してください。
入力例 3
32
出力例 3
Yes
x=5,y=0 とすると、2^x3^y=32\times1=32 となるため、Yes
と出力してください。
入力例 4
37748736
出力例 4
Yes
Score : 200 points
Problem Statement
You are given a positive integer N.
If there are integers x and y such that N=2^x3^y, print Yes
; otherwise, print No
.
Constraints
- 1\leq N\leq10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print a single line containing Yes
if there are integers x and y that satisfy the condition, and No
otherwise.
Sample Input 1
324
Sample Output 1
Yes
For x=2,y=4, we have 2^x3^y=2^23^4=4\times81=324, so the condition is satisfied.
Thus, you should print Yes
.
Sample Input 2
5
Sample Output 2
No
There are no integers x and y such that 2^x3^y=5.
Thus, you should print No
.
Sample Input 3
32
Sample Output 3
Yes
For x=5,y=0, we have 2^x3^y=32\times1=32, so you should print Yes
.
Sample Input 4
37748736
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 300 点
問題文
高橋君は英小文字からなる文字列 T を青木君に向けて送りました。その結果、青木君は英小文字からなる文字列 T' を受信しました。
T' は T から一部が変更されてしまっている可能性があり、具体的には、下記の 4 つのうちのちょうど 1 つが成り立つことがわかっています。
- T' は、T と等しい。
- T' は、T のいずれか 1 つの位置(先頭と末尾も含む)に英小文字を 1 つ挿入して得られる文字列である。
- T' は、T からある 1 文字を削除して得られる文字列である。
- T' は、T のある 1 文字を別の英小文字に変更して得られる文字列である。
青木君が受信した文字列 T' と、英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が入力として与えられるので、 S_1, S_2, \ldots, S_N のうち、高橋君が送った文字列 T と等しい可能性があるものをすべて求めてください。
制約
- N は整数
- 1 \leq N \leq 5 \times 10^5
- S_i と T' は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
- S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N T' S_1 S_2 \vdots S_N
出力
S_1, S_2, \ldots, S_N のうち T と等しい可能性があるものすべての添字を昇順に並べた列を (i_1, i_2, \ldots, i_K) とする。 この列の長さ K および列自体を、下記の形式にしたがって出力せよ。
K i_1 i_2 \ldots i_K
入力例 1
5 ababc ababc babc abacbc abdbc abbac
出力例 1
4 1 2 3 4
S_1, S_2, \ldots, S_5 のうち、T と等しい可能性があるものは S_1, S_2, S_3, S_4 の 4 つであることが下記の通りわかります。
- S_1 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_1 =ababc
と等しいからです。 - S_2 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_2 =babc
の先頭に文字a
を挿入して得られる文字列だからです。 - S_3 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_3 =abacbc
から 4 文字目のc
を削除して得られる文字列だからです。 - S_4 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_4 =abdbc
の 3 文字目のd
をb
に変更して得られる文字列だからです。 - S_5 は T と等しい可能性はありません。なぜなら、S_5 =
abbac
を T としたとき、T' =ababc
は問題文中の 4 つの条件をいずれも満たさないからです。
入力例 2
1 aoki takahashi
出力例 2
0
入力例 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
出力例 3
6 1 2 4 7 8 9
Score : 300 points
Problem Statement
Takahashi sent a string T consisting of lowercase English letters to Aoki. As a result, Aoki received a string T' consisting of lowercase English letters.
T' may have been altered from T. Specifically, exactly one of the following four conditions is known to hold.
- T' is equal to T.
- T' is a string obtained by inserting one lowercase English letter at one position (possibly the beginning and end) in T.
- T' is a string obtained by deleting one character from T.
- T' is a string obtained by changing one character in T to another lowercase English letter.
You are given the string T' received by Aoki and N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters. Find all the strings among S_1, S_2, \ldots, S_N that could equal the string T sent by Takahashi.
Constraints
- N is an integer.
- 1 \leq N \leq 5 \times 10^5
- S_i and T' are strings of length between 1 and 5 \times 10^5, inclusive, consisting of lowercase English letters.
- The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N T' S_1 S_2 \vdots S_N
Output
Let (i_1, i_2, \ldots, i_K) be the sequence of indices of all the strings among S_1, S_2, \ldots, S_N that could be equal to T, in ascending order. Print the length K of this sequence, and the sequence itself, in the following format:
K i_1 i_2 \ldots i_K
Sample Input 1
5 ababc ababc babc abacbc abdbc abbac
Sample Output 1
4 1 2 3 4
Among S_1, S_2, \ldots, S_5, the strings that could be equal to T are S_1, S_2, S_3, S_4, as explained below.
- S_1 could be equal to T, because T' =
ababc
is equal to S_1 =ababc
. - S_2 could be equal to T, because T' =
ababc
is obtained by inserting the lettera
at the beginning of S_2 =babc
. - S_3 could be equal to T, because T' =
ababc
is obtained by deleting the fourth characterc
from S_3 =abacbc
. - S_4 could be equal to T, because T' =
ababc
is obtained by changing the third characterd
in S_4 =abdbc
tob
. - S_5 could not be equal to T, because if we take S_5 =
abbac
as T, then T' =ababc
does not satisfy any of the four conditions in the problem statement.
Sample Input 2
1 aoki takahashi
Sample Output 2
0
Sample Input 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
Sample Output 3
6 1 2 4 7 8 9
Time Limit: 4 sec / Memory Limit: 1024 MB
配点 : 425 点
問題文
数字のみからなる、長さ N の文字列 S が与えられます。
S を並べ替えてできる文字列を十進法の整数として解釈したもののうち、平方数であるようなものがいくつあるか求めてください。
より厳密には、次のようになります。
S の先頭から i 番目 (1\leq i\leq N) の数字に対応する数を s _ i とします。
(1, \ldots, N) の順列 P=(p _ 1,p _ 2,\ldots,p _ N) によって \displaystyle \sum _ {i=1} ^ N s _ {p _ i}10 ^ {N-i} と書ける整数のうち、平方数であるようなものがいくつあるか求めてください。
制約
- 1\leq N\leq 13
- S は数字のみからなる長さ N の文字列
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを 1 行で出力せよ。
入力例 1
4 4320
出力例 1
2
P=(4,2,3,1) とすると、s _ 4\times10 ^ 3+s _ 2\times10 ^ 2+s _ 3\times10 ^ 1+s _ 1=324=18 ^ 2 となります。
P=(3,2,4,1) とすると、s _ 3\times10 ^ 3+s _ 2\times10 ^ 2+s _ 4\times10 ^ 1+s _ 1=2304=48 ^ 2 となります。
これら以外の並べ替え方では平方数にならないため、2 を出力してください。
入力例 2
3 010
出力例 2
2
P=(1,3,2) もしくは P=(3,1,2) とすると、\displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=1=1 ^ 2 となります。
P=(2,1,3) もしくは P=(2,3,1) とすると、\displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=100=10 ^ 2 となります。
これら以外の並べ替え方では平方数にならないため、2 を出力してください。 異なる並べ替え方でも、並べ替えた結果の数が同じなら 1 つと数えることに注意してください。
入力例 3
13 8694027811503
出力例 3
840
Score : 425 points
Problem Statement
You are given a string S of length N consisting of digits.
Find the number of square numbers that can be obtained by interpreting a permutation of S as a decimal integer.
More formally, solve the following.
Let s _ i be the number corresponding to the i-th digit (1\leq i\leq N) from the beginning of S.
Find the number of square numbers that can be represented as \displaystyle \sum _ {i=1} ^ N s _ {p _ i}10 ^ {N-i} with a permutation P=(p _ 1,p _ 2,\ldots,p _ N) of (1, \dots, N).
Constraints
- 1\leq N\leq 13
- S is a string of length N consisting of digits.
- N is an integer.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer in a single line.
Sample Input 1
4 4320
Sample Output 1
2
For P=(4,2,3,1), we have s _ 4\times10 ^ 3+s _ 2\times10 ^ 2+s _ 3\times10 ^ 1+s _ 1=324=18 ^ 2.
For P=(3,2,4,1), we have s _ 3\times10 ^ 3+s _ 2\times10 ^ 2+s _ 4\times10 ^ 1+s _ 1=2304=48 ^ 2.
No other permutations result in square numbers, so you should print 2.
Sample Input 2
3 010
Sample Output 2
2
For P=(1,3,2) or P=(3,1,2), we have \displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=1=1 ^ 2.
For P=(2,1,3) or P=(2,3,1), we have \displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=100=10 ^ 2.
No other permutations result in square numbers, so you should print 2. Note that different permutations are not distinguished if they result in the same number.
Sample Input 3
13 8694027811503
Sample Output 3
840
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N 、および、英小文字からなる文字列 T が与えられます。
1 以上 N 以下の 2 つの整数からなる組 (i, j) は N^2 個ありますが、そのうち下記の条件を満たすものの個数を出力してください。
- S_i と S_j をこの順に連結して得られる文字列は、T を(連続とは限らない)部分列として含む。
制約
- N は整数
- 1 \leq N \leq 5 \times 10^5
- S_i および T は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
- S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N T S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
3 bac abba bcb aaca
出力例 1
3
問題文中の条件を満たす組 (i, j) は、下記に示す 3 個の組 (1, 2), (1, 3), (2, 3) です。
- (i, j) = (1, 2) について、S_1 と S_2 をこの順に連結して得られる文字列
abbabcb
はbac
を部分列として含みます。 - (i, j) = (1, 3) について、S_1 と S_3 をこの順に連結して得られる文字列
abbaaaca
はbac
を部分列として含みます。 - (i, j) = (2, 3) について、S_2 と S_3 をこの順に連結して得られる文字列
bcbaaca
はbac
を部分列として含みます。
入力例 2
5 xx x x x x x
出力例 2
25
入力例 3
1 y x
出力例 3
0
入力例 4
10 ms mkgn m hlms vmsle mxsm nnzdhi umsavxlb ffnsybomr yvmm naouel
出力例 4
68
Score : 500 points
Problem Statement
You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters, and a string T consisting of lowercase English letters.
There are N^2 pairs (i, j) of integers between 1 and N, inclusive. Print the number of pairs among them that satisfy the following condition.
- The concatenation of S_i and S_j in this order contains T as a (not necessarily contiguous) subsequence.
Constraints
- N is an integer.
- 1 \leq N \leq 5 \times 10^5
- S_i and T are strings of length 1 to 5 \times 10^5, inclusive, consisting of lowercase English letters.
- The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N T S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
3 bac abba bcb aaca
Sample Output 1
3
The pairs (i, j) that satisfy the condition in the problem statement are (1, 2), (1, 3), (2, 3), as seen below.
- For (i, j) = (1, 2), the concatenation
abbabcb
of S_1 and S_2 in this order containsbac
as a subsequence. - For (i, j) = (1, 3), the concatenation
abbaaaca
of S_1 and S_3 in this order containsbac
as a subsequence. - For (i, j) = (2, 3), the concatenation
bcbaaca
of S_2 and S_3 in this order containsbac
as a subsequence.
Sample Input 2
5 xx x x x x x
Sample Output 2
25
Sample Input 3
1 y x
Sample Output 3
0
Sample Input 4
10 ms mkgn m hlms vmsle mxsm nnzdhi umsavxlb ffnsybomr yvmm naouel
Sample Output 4
68
Time Limit: 5 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる有向グラフがあります。各辺には美しさとコストの 2 つの正整数値が定められています。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i から頂点 v_i への有向辺であり、その美しさは b_i 、コストは c_i です。 ここで、u_i \lt v_i が制約として保証されます。
頂点 1 から頂点 N へのパス P を 1 つ選んだときの、下記の値としてあり得る最大値を求めてください。
- P 上のすべての辺の美しさの総和を、P 上のすべての辺のコストの総和で割った値
ここで、与えられるグラフにおいて頂点 1 から頂点 N へのパスが 1 個以上存在することが制約として保証されます。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 1 \leq b_i, c_i \leq 10^4
- 頂点 1 から頂点 N へのパスが存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 b_1 c_1 u_2 v_2 b_2 c_2 \vdots u_M v_M b_M c_M
出力
答えを出力せよ。出力された値と真の値の相対誤差もしくは絶対誤差が 10^{-9} 以下のとき、正答と判定される。
入力例 1
5 7 1 2 3 6 1 3 9 5 2 3 1 5 2 4 5 3 2 5 1 9 3 4 4 8 4 5 2 7
出力例 1
0.7500000000000000
パス P として、 2, 6, 7 番目の辺をこの順に通り頂点 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 とたどるパスを選んだとき、「 P 上のすべての辺の美しさの総和を P 上のすべての辺のコストの総和で割った値」 は、 (b_2 + b_6 + b_7) / (c_2 + c_6 + c_7) = (9 + 4 + 2) / (5 + 8 + 7) = 15 / 20 = 0.75 であり、これがあり得る最大値です。
入力例 2
3 3 1 3 1 1 1 3 2 1 1 3 3 1
出力例 2
3.0000000000000000
入力例 3
10 20 3 4 1 2 7 9 4 5 2 4 4 5 4 5 1 4 6 9 4 1 9 10 3 2 6 10 5 5 5 6 1 2 5 6 5 2 2 3 2 3 6 10 4 4 4 6 3 4 4 8 4 1 3 5 3 2 2 4 3 2 3 5 4 2 1 5 3 4 1 2 4 2 3 7 2 2 7 8 1 3
出力例 3
1.8333333333333333
Score : 500 points
Problem Statement
There is a directed graph with N vertices and M edges. Each edge has two positive integer values: beauty and cost.
For i = 1, 2, \ldots, M, the i-th edge is directed from vertex u_i to vertex v_i, with beauty b_i and cost c_i. Here, the constraints guarantee that u_i \lt v_i.
Find the maximum value of the following for a path P from vertex 1 to vertex N.
- The total beauty of all edges on P divided by the total cost of all edges on P.
Here, the constraints guarantee that the given graph has at least one path from vertex 1 to vertex N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 1 \leq b_i, c_i \leq 10^4
- There is a path from vertex 1 to vertex N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 b_1 c_1 u_2 v_2 b_2 c_2 \vdots u_M v_M b_M c_M
Output
Print the answer. Your output will be judged as correct if the relative or absolute error from the true answer is at most 10^{-9}.
Sample Input 1
5 7 1 2 3 6 1 3 9 5 2 3 1 5 2 4 5 3 2 5 1 9 3 4 4 8 4 5 2 7
Sample Output 1
0.7500000000000000
For the path P that passes through the 2-nd, 6-th, and 7-th edges in this order and visits vertices 1 \rightarrow 3 \rightarrow 4 \rightarrow 5, the total beauty of all edges on P divided by the total cost of all edges on P is (b_2 + b_6 + b_7) / (c_2 + c_6 + c_7) = (9 + 4 + 2) / (5 + 8 + 7) = 15 / 20 = 0.75, and this is the maximum possible value.
Sample Input 2
3 3 1 3 1 1 1 3 2 1 1 3 3 1
Sample Output 2
3.0000000000000000
Sample Input 3
10 20 3 4 1 2 7 9 4 5 2 4 4 5 4 5 1 4 6 9 4 1 9 10 3 2 6 10 5 5 5 6 1 2 5 6 5 2 2 3 2 3 6 10 4 4 4 6 3 4 4 8 4 1 3 5 3 2 2 4 3 2 3 5 4 2 1 5 3 4 1 2 4 2 3 7 2 2 7 8 1 3
Sample Output 3
1.8333333333333333
Time Limit: 6 sec / Memory Limit: 1024 MB
配点 : 600 点
問題文
高橋くんは、長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) を持っています。 A は (1,2,\ldots,N) の順列です。
高橋くんは、操作を Q 回行って、1+Q 個の数列を作ろうとしています。 0 番の数列を A として、高橋くんは一連の操作を始めます。 i 回目 (1\leq i\leq Q) の操作は、整数の 3 つ組 (t _ i,s _ i,x _ i) を用いて表され、次のような操作に対応します( 入出力例の説明も参考にしてください)。
- t _ i=1 のとき、高橋くんは s _ i 番 (0\leq s _ i\lt i) の数列から x _ i 個目より後の要素を取り除き、取り除いた要素を順序を保って新しく i 番の数列とする。
- t _ i=2 のとき、高橋くんは s _ i 番 (0\leq s _ i\lt i) の数列から値が x _ i より大きい要素を取り除き、取り除いた要素を順序を保って新しく i 番の数列とする。
ただし、長さ L の数列 X について、X のどの要素も「0 個目より後の要素」です。 また、L\leq l を満たす任意の l について X のどの要素も「l 個目より後の要素」ではありません。
i=1,2,\ldots,Q について、i 回目の操作が終わった直後の i 番の数列の長さを求めてください。
制約
- 1\leq N\leq2\times10^5
- 1\leq A _ i\leq N\ (1\leq i\leq N)
- A _ i\neq A _ j\ (1\leq i\lt j\leq N)
- 1\leq Q\leq2\times10^5
- t _ i=1,2\ (1\leq i\leq Q)
- 0\leq s _ i\lt i\ (1\leq i\leq Q)
- 0\leq x _ i\leq N\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N Q t _ 1 s _ 1 x _ 1 t _ 2 s _ 2 x _ 2 \vdots t _ Q s _ Q x _ Q
出力
答えを Q 行で出力せよ。 i 行目 (1\leq i\leq Q) には、i 回目の操作が終わった直後における i 番の列の長さを出力せよ。
入力例 1
10 1 8 7 4 5 6 3 2 9 10 5 2 0 4 1 1 2 2 0 2 2 2 5 1 0 1
出力例 1
6 4 2 3 1
はじめ、高橋くんは長さ 10 の数列 A=(1,8,7,4,5,6,3,2,9,10) を持っています。 高橋くんは、0 番の数列を A=(1,8,7,4,5,6,3,2,9,10) として一連の操作を開始します。
1 回目の操作では、0 番の数列の 4 より大きな要素 8,7,5,6,9,10 を取り除き、これらを新しく 1 番の数列にします。この操作が終わったとき、0,1 番の数列はそれぞれ (1,4,3,2),(8,7,5,6,9,10) になります。
2 回目の操作では、1 番の数列の 2 個目より後の要素 5,6,9,10 を取り除き、これらを新しく 2 番の数列にします。この操作が終わったとき、0,1,2 番の数列はそれぞれ (1,4,3,2),(8,7),(5,6,9,10) になります。
3 回目以降の操作について、i 回目の操作が終わったときの 0,1,2,\ldots,i 番の数列はそれぞれ以下のようになります。
- (1,2),(8,7),(5,6,9,10),(4,3)
- (1,2),(8,7),(5),(4,3),(6,9,10)
- (1),(8,7),(5),(4,3),(6,9,10),(2)
i=1,2,\ldots,5 回目の操作が終わったときの i 番の数列の長さはそれぞれ 6,4,2,3,1 なので、これらをそれぞれの行に出力してください。
入力例 2
8 6 7 8 4 5 1 3 2 5 2 0 0 1 1 0 2 2 0 1 3 8 2 2 3
出力例 2
8 8 8 0 0
操作の結果、空の数列が生じることもあります。
入力例 3
30 20 6 13 11 29 30 9 10 16 5 8 25 1 19 12 18 7 2 4 27 3 22 23 24 28 21 14 26 15 17 10 1 0 22 1 0 21 2 0 15 1 0 9 1 3 6 2 3 18 1 6 2 1 0 1 2 5 20 2 7 26
出力例 3
8 1 8 4 2 5 3 8 1 1
Score : 600 points
Problem Statement
Takahashi has a sequence of length N: A=(A _ 1,A _ 2,\ldots,A _ N). A is a permutation of (1,2,\ldots,N).
He is going to perform Q operations to create 1+Q sequences. He lets A be the sequence numbered 0, and then begins the series of operations. The i-th operation (1\leq i\leq Q) is represented by a triple of integers (t _ i,s _ i,x _ i) and corresponds to the following operation (see also the explanations in Sample Input/Output).
- When t _ i=1, he removes the elements following the x _ i-th element from the sequence numbered s _ i (0\leq s _ i\lt i), and creates a new sequence numbered i with the removed elements, keeping their order.
- When t _ i=2, he removes the elements greater than x _ i from the sequence numbered s _ i (0\leq s _ i\lt i), and creates a new sequence numbered i with the removed elements, keeping their order.
For a sequence X of length L, every element of X is among "the elements following the 0-th element." Also, for any l such that L\leq l, no element of X is among "the elements following the l-th element."
For i=1,2,\ldots,Q, find the length of the sequence numbered i immediately after the i-th operation.
Constraints
- 1\leq N\leq2\times10^5
- 1\leq A _ i\leq N\ (1\leq i\leq N)
- A _ i\neq A _ j\ (1\leq i\lt j\leq N)
- 1\leq Q\leq2\times10^5
- t _ i=1,2\ (1\leq i\leq Q)
- 0\leq s _ i\lt i\ (1\leq i\leq Q)
- 0\leq x _ i\leq N\ (1\leq i\leq Q)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N Q t _ 1 s _ 1 x _ 1 t _ 2 s _ 2 x _ 2 \vdots t _ Q s _ Q x _ Q
Output
Print Q lines. The i-th line (1\leq i\leq Q) should contain the length of the sequence numbered i immediately after the i-th operation.
Sample Input 1
10 1 8 7 4 5 6 3 2 9 10 5 2 0 4 1 1 2 2 0 2 2 2 5 1 0 1
Sample Output 1
6 4 2 3 1
Initially, Takahashi has a sequence A=(1,8,7,4,5,6,3,2,9,10) of length 10. He lets A=(1,8,7,4,5,6,3,2,9,10) be the sequence numbered 0, and then begins the series of operations.
In the first operation, he removes elements greater than 4 from the sequence numbered 0, namely 8,7,5,6,9,10, and creates a new sequence numbered 1 with these elements. After this operation, the sequences numbered 0 and 1 are (1,4,3,2) and (8,7,5,6,9,10), respectively.
In the second operation, he removes the elements following the 2-nd element from the sequence numbered 1, namely 5,6,9,10, and creates a new sequence numbered 2 with these elements. After this operation, the sequences numbered 0, 1, and 2 are (1,4,3,2), (8,7), and (5,6,9,10), respectively.
For the third and subsequent operations, the sequences numbered 0,1,2,\ldots,i after the i-th operation are as follows:
- (1,2),(8,7),(5,6,9,10),(4,3)
- (1,2),(8,7),(5),(4,3),(6,9,10)
- (1),(8,7),(5),(4,3),(6,9,10),(2)
The length of the sequence numbered i after the i-th operation for i=1,2,\ldots,5 is 6,4,2,3,1. Print each of these in its own line.
Sample Input 2
8 6 7 8 4 5 1 3 2 5 2 0 0 1 1 0 2 2 0 1 3 8 2 2 3
Sample Output 2
8 8 8 0 0
The operations may create empty sequences.
Sample Input 3
30 20 6 13 11 29 30 9 10 16 5 8 25 1 19 12 18 7 2 4 27 3 22 23 24 28 21 14 26 15 17 10 1 0 22 1 0 21 2 0 15 1 0 9 1 3 6 2 3 18 1 6 2 1 0 1 2 5 20 2 7 26
Sample Output 3
8 1 8 4 2 5 3 8 1 1