E - Min Max Pair

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

1 以上 N 以下の整数からなる長さ N の数列 a = (a_1, \dots, a_N) が与えられます。

以下の条件を全て満たす整数 i, j の組の総数を求めてください。

  • 1 \leq i \lt j \leq N
  • \min(a_i, a_j) = i
  • \max(a_i, a_j) = j

制約

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq a_i \leq N \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

4
1 3 2 4

出力例 1

2

(i, j) = (1, 4), (2, 3) が条件を満たします。


入力例 2

10
5 8 2 2 1 6 7 2 9 10

出力例 2

8

Score : 300 points

Problem Statement

You are given a sequence a = (a_1, \dots, a_N) of length N consisting of integers between 1 and N.

Find the number of pairs of integers i, j that satisfy all of the following conditions:

  • 1 \leq i \lt j \leq N
  • \min(a_i, a_j) = i
  • \max(a_i, a_j) = j

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq a_i \leq N \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

4
1 3 2 4

Sample Output 1

2

(i, j) = (1, 4), (2, 3) satisfy the conditions.


Sample Input 2

10
5 8 2 2 1 6 7 2 9 10

Sample Output 2

8
F - Rotation

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。

以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。

  • 1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。
  • 2 x: Sx 番目の文字を出力する。

制約

  • 2 \le N \le 5 \times 10^5
  • 1 \le Q \le 5 \times 10^5
  • 1 \le x \le N
  • |S|=N
  • S は英小文字からなる。
  • 2 x の形式のクエリが 1 個以上与えられる。
  • N,Q,x はすべて整数。

入力

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

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

それぞれのクエリは以下の形式で与えられる。ここで、t1 または 2 である。

t x

出力

2 x の形式の各クエリについて、答えを一行に出力せよ。


入力例 1

3 3
abc
2 2
1 1
2 2

出力例 1

b
a

1 個目のクエリのとき、Sabc なので 2 文字目の b を出力します。 2 個目のクエリのとき、Sabc から cab に変わります。 3 個目のクエリのとき、Scab なので 2 文字目の a を出力します。


入力例 2

10 8
dsuccxulnl
2 4
2 7
1 2
2 7
1 1
1 2
1 3
2 5

出力例 2

c
u
c
u

Score : 300 points

Problem Statement

You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.

Process Q queries. Each query is of one of the following two types.

  • 1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.
  • 2 x: Print the x-th character of S.

Constraints

  • 2 \le N \le 5 \times 10^5
  • 1 \le Q \le 5 \times 10^5
  • 1 \le x \le N
  • |S|=N
  • S consists of lowercase English letters.
  • At least one query in the format 2 x.
  • N, Q, x are all integers.

Input

Input is given from Standard Input in the following format:

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

Each query is in the following format, where t is 1 or 2:

t x

Output

For each query in the format 2 x, print the answer in a single line.


Sample Input 1

3 3
abc
2 2
1 1
2 2

Sample Output 1

b
a

In the 1-st query, S is abc, so the 2-nd character b should be printed. In the 2-nd query, S is changed from abc to cab. In the 3-rd query, S is cab, so the 2-nd character a should be printed.


Sample Input 2

10 8
dsuccxulnl
2 4
2 7
1 2
2 7
1 1
1 2
1 3
2 5

Sample Output 2

c
u
c
u
G - Sequence Query

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

空の数列 A があります。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 xAx を追加する。

  • 2 x kAx 以下の要素のうち、大きい方から k 番目の値を出力する。(k\bf{5} 以下)
    ただし、Ax 以下の要素が k 個以上存在しないときは -1 と出力する。

  • 3 x kAx 以上の要素のうち、小さい方から k 番目の値を出力する。(k\bf{5} 以下)
    ただし、Ax 以上の要素が k 個以上存在しないときは -1 と出力する。

制約

  • 1\leq Q \leq 2\times 10^5
  • 1\leq x\leq 10^{18}
  • 1\leq k\leq 5
  • 入力は全て整数である

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

i 番目のクエリ \text{query}_i では、まずクエリの種類 c_i (1,2,3 のいずれか) が与えられる。
c_i=1 の場合は x が追加で与えられ、c_i=2,3 の場合は x,k が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2 x k
3 x k

出力

c_i=2,3 を満たすクエリの個数を q として q 行出力せよ。
j(1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

出力例 1

20
20
30
-1
-1
1

\text{query}_{1,2,3,4} が終了した段階で、A=(20,10,30,20) となっています。

\text{query}_{5,6,7} について、A15 以上の要素は (20,30,20) です。
このうち小さい方から 1 番目の値は 202 番目の値は 203 番目の値は 30 です。

Score : 400 points

Problem Statement

We have an empty sequence A.
Given Q queries, process them in order.
Each query is of one of the following three types.

  • 1 x : Insert x to A.

  • 2 x k : Among the elements of A that are less than or equal to x, print the k-th largest value. (k is no more than \bf{5})
    If there are less than k elements of A that are less than or equal to x, then print -1.

  • 3 x k : Among the elements of A that are greater than or equal to x, print the k-th smallest value. (k is no more than \bf{5})
    If there are less than k elements of A that are greater than or equal to x, then print -1.

Constraints

  • 1\leq Q \leq 2\times 10^5
  • 1\leq x\leq 10^{18}
  • 1\leq k\leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

In the i-th query \text{query}_i, the type of query c_i (which is either 1, 2, or 3) is given first.
If c_i=1, then x is additionally given; if c_i=2, 3, then x and k are additionally given.

In other words, each query is given in one of the following three formats:

1 x
2 x k
3 x k

Output

Print q lines, where q is the number of queries such that c_i=2,3.
The j-th line (1\leq j\leq q) should contain the answer for the j-th such query.


Sample Input 1

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

Sample Output 1

20
20
30
-1
-1
1

After \text{query}_{1,2,3,4} have been processed, we have A=(20,10,30,20).

For \text{query}_{5,6,7}, the elements of A greater than or equal to 15 are (20,30,20).
The 1-st smallest value of them is 20; the 2-nd is 20; the 3-rd is 30.

H - Takahashi Quest

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

高橋くんは冒険に出ようとしています。

冒険では、N 個の出来事が起こります。 i 番目 (1\leq i\leq N) の出来事は整数の組 (t _ i,x _ i) (1\leq t _ i\leq 2,1\leq x _ i\leq N) で表され、次のような出来事です。

  • t _ i=1 のとき、タイプ x _ i のポーションを 1 つ発見する。高橋くんは、発見したポーションを拾うか捨てるかのどちらかを選択する。
  • t _ i=2 のとき、タイプ x _ i のモンスター 1 体と遭遇する。高橋くんがタイプ x _ i のポーションを持っている場合、それを 1 つ消費することでモンスターを撃退することができる。モンスターを撃退しなかった場合、高橋くんは敗北する。

高橋くんが敗北することなく全てのモンスターを撃退することができるか判定してください。

高橋くんが全てのモンスターを撃退することができない場合、-1 を出力してください。

高橋くんが全てのモンスターを撃退することができる場合、高橋君が冒険の途中で持っているポーションの個数の最大値を K とします。 高橋くんが敗北しないような戦略全体にわたる K の最小値を K _ {\min} とします。 K _ {\min} の値と、K _ {\min} を達成する高橋くんの行動を出力してください。

制約

  • 1\leq N\leq2\times10^5
  • 1\leq t _ i\leq2\ (1\leq i\leq N)
  • 1\leq x _ i\leq N\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
t _ 1 x _ 1
t _ 2 x _ 2
\vdots
t _ N x _ N

出力

高橋くんが全てのモンスターを撃退することができない場合、-1 を出力せよ。 高橋くんが全てのモンスターを撃退することができる場合、1 行目には K _ {\min} の値を、2 行目には t _ i=1 であるような i すべてについて昇順に、i 番目の出来事で発見したポーションを拾うなら 1 を、拾わないなら 0 を空白区切りで出力せよ。 K _ {\min} を達成し、高橋くんが敗北せず冒険を終えられるような行動が複数ある場合、どれを出力しても構わない。


入力例 1

13
1 2
1 3
1 1
1 3
1 2
2 3
1 3
1 3
2 3
1 3
2 2
2 3
2 1

出力例 1

3
1 1 1 0 0 1 0 1

出力例は、次のような行動に対応しています。

  • タイプ 2,3,1 のポーションをこの順に発見する。これらのポーションをすべて拾う。
  • タイプ 3,2 のポーションをこの順に発見する。これらのポーションをいずれも拾わない。
  • タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 3 のポーションを発見する。このポーションを拾う。
  • タイプ 3 のポーションを発見する。このポーションを拾わない。
  • タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 3 のポーションを発見する。このポーションを拾う。
  • タイプ 2 のモンスターと遭遇する。持っているタイプ 2 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 1 のモンスターと遭遇する。持っているタイプ 1 のポーションを 1 つ消費してモンスターを撃退する。

この行動では、K の値は 3 となります。

K\leq 2 として敗北しない方法はないので、求める K _ {\min} の値は 3 です。 K=3 を満たして高橋くんが敗北しない行動は複数ありますが、どれを出力しても構いません。


入力例 2

4
2 3
1 4
2 1
1 2

出力例 2

-1

高橋くんはかならず最初に遭遇するモンスターに敗北してしまいます。


入力例 3

30
1 25
1 2
1 10
1 18
2 18
1 11
2 11
1 21
1 6
2 2
2 10
1 11
1 24
1 11
1 3
1 2
1 18
2 25
1 8
1 10
1 11
2 18
2 10
1 10
2 2
1 24
1 10
2 10
1 25
2 6

出力例 3

4
1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0

Score : 450 points

Problem Statement

Takahashi will embark on an adventure.

During the adventure, N events will occur. The i-th event (1\leq i\leq N) is represented by a pair of integers (t _ i,x _ i) (1\leq t _ i\leq 2,1\leq x _ i\leq N) and is as follows:

  • If t _ i=1, he finds one potion of type x _ i. He can choose to pick it up or discard it.
  • If t _ i=2, he encounters one monster of type x _ i. If he has a potion of type x _ i, he can use one to defeat the monster. If he does not defeat it, he will be defeated.

Determine whether he can defeat all the monsters without being defeated.

If he cannot defeat all the monsters, print -1.

Otherwise, let K be the maximum number of potions he has at some point during the adventure. Let K _ {\min} be the minimum value of K across all strategies where he will not be defeated. Print the value of K _ {\min} and the actions of Takahashi that achieve K _ {\min}.

Constraints

  • 1\leq N\leq2\times10^5
  • 1\leq t _ i\leq2\ (1\leq i\leq N)
  • 1\leq x _ i\leq N\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
t _ 1 x _ 1
t _ 2 x _ 2
\vdots
t _ N x _ N

Output

If Takahashi cannot defeat all the monsters, print -1. If he can, print the value of K _ {\min} in the first line, and in the second line, for each i such that t _ i=1 in ascending order, print 1 if he picks up the potion found at the i-th event, and 0 otherwise, separated by spaces. If multiple sequences of actions achieve K _ {\min} and allow him to finish the adventure without being defeated, you may print any of them.


Sample Input 1

13
1 2
1 3
1 1
1 3
1 2
2 3
1 3
1 3
2 3
1 3
2 2
2 3
2 1

Sample Output 1

3
1 1 1 0 0 1 0 1

The sample output corresponds to the following actions:

  • Find potions of types 2,3,1 in this order. Pick up all of them.
  • Find potions of types 3,2 in this order. Do not pick up any of them.
  • Encounter a type-3 monster. Use one type-3 potion to defeat it.
  • Find a type-3 potion. Pick it up.
  • Find a type-3 potion. Do not pick it up.
  • Encounter a type-3 monster. Use one type-3 potion to defeat it.
  • Find a type-3 potion. Pick it up.
  • Encounter a type-2 monster. Use one type-2 potion to defeat it.
  • Encounter a type-3 monster. Use one type-3 potion to defeat it.
  • Encounter a type-1 monster. Use one type-1 potion to defeat it.

In this sequence of actions, the value of K is 3.

There is no way to avoid defeat with K\leq 2, so the sought value of K _ {\min} is 3. There are multiple sequences of actions that satisfy K=3 and allow him to avoid defeat; you may print any of them.


Sample Input 2

4
2 3
1 4
2 1
1 2

Sample Output 2

-1

He will inevitably be defeated by the first monster he encounters.


Sample Input 3

30
1 25
1 2
1 10
1 18
2 18
1 11
2 11
1 21
1 6
2 2
2 10
1 11
1 24
1 11
1 3
1 2
1 18
2 25
1 8
1 10
1 11
2 18
2 10
1 10
2 2
1 24
1 10
2 10
1 25
2 6

Sample Output 3

4
1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
I - Virus 2

実行時間制限: 4 sec / メモリ制限: 1024 MiB

配点 : 550

問題文

部屋 1, 部屋 2, \ldots, 部屋 N と番号づけられた N 個の部屋に人が 1 人ずつ住んでおり、 また、いくつかの相異なる 2 つの部屋の間は通路によって結ばれています。 通路は M 本あり、i 本目の通路は部屋 U_i と部屋 V_i を結んでおり、長さは W_i です。

ある日(これを 0 日目とします)の夜に、部屋 A_1,A_2,\ldots, A_K に住んでいる K 人がウイルスに(新しく)感染してしまいました。 さらにその後の D 日間で i 日目 (1\leq i\leq D) には次のように感染が広がりました。

(i-1) 日目の夜の時点で感染していた人は、i 日目の夜の時点でも感染していた。
そうでない人については、(i-1) 日目の夜の時点で感染していた人の住んでいる部屋のうちの少なくとも 1 つから 距離 X_i 以内の部屋に住んでいた時かつその時に限り、新しく感染した。 ここで、部屋 P,Q の間の距離は、部屋 P から 部屋 Q まで通路のみを使って移動する時に通る通路の長さの総和としてあり得る最小値として定義される。 ただし、部屋 P から 部屋 Q へ通路のみを使って移動する事ができない時、距離は 10^{100} とする。

i (1\leq i\leq N) について、部屋 i に住んでいる人がそれぞれ何日目の夜に(新しく)感染したか出力してください。ただし、D 日目の夜の時点で感染していない場合は -1 を出力してください。

制約

  • 1 \leq N\leq 3\times 10^5
  • 0 \leq M\leq 3\times 10^5
  • 1 \leq U_i < V_i\leq N
  • (U_i,V_i) はすべて異なる。
  • 1\leq W_i\leq 10^9
  • 1 \leq K\leq N
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1 \leq D\leq 3\times 10^5
  • 1\leq X_i\leq 10^9
  • 入力はすべて整数

入力

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

N M
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
K
A_1 A_2 \ldots A_K
D
X_1 X_2 \ldots X_D

出力

N 行出力せよ。
i 行目 (1\leq i\leq N) には、部屋 i に住んでいる人が何日目の夜に(新しく)感染したか出力せよ。


入力例 1

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

出力例 1

0
1
1
2

次のように感染は広がります。

  • 0 日目の夜、部屋 1 に住んでいる人が感染する。
  • 部屋 1 と部屋 2,3,4 の間の距離はそれぞれ 2,3,5 である。よって、X_1=3 であるから、1 日目の夜、部屋 2,3 に住んでいる人が新しく感染する。
  • 部屋 3 と部屋 4 の間の距離は 2 である。よって、X_2=3 であるから、2 日目の夜、部屋 4 に住んでいる人も感染する。

よって、部屋 1,2,3,4 に住んでいる人はそれぞれ 0,1,1,2 日目に新しく感染したため、0,1,1,2 をこの順で各行に出力します。


入力例 2

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

出力例 2

0
1
2
-1
2
0
-1

入力例 3

5 1
1 2 5
2
1 3
3
3 7 5

出力例 3

0
2
0
-1
-1

どの 2 つの部屋の間も通路のみを使って移動できるとは限らないことに注意してください。

Score : 550 points

Problem Statement

There are N rooms numbered 1, 2, \ldots, N, each with one person living in it, and M corridors connecting two different rooms. The i-th corridor connects room U_i and room V_i with a length of W_i.

One day (we call this day 0), the K people living in rooms A_1, A_2, \ldots, A_K got (newly) infected with a virus. Furthermore, on the i-th of the following D days (1\leq i\leq D), the infection spread as follows.

People who were infected at the end of the night of day (i-1) remained infected at the end of the night of day i.
For those who were not infected, they were newly infected if and only if they were living in a room within a distance of X_i from at least one room where an infected person was living at the end of the night of day (i-1). Here, the distance between rooms P and Q is defined as the minimum possible sum of the lengths of the corridors when moving from room P to room Q using only corridors. If it is impossible to move from room P to room Q using only corridors, the distance is set to 10^{100}.

For each i (1\leq i\leq N), print the day on which the person living in room i was newly infected. If they were not infected by the end of the night of day D, print -1.

Constraints

  • 1 \leq N\leq 3\times 10^5
  • 0 \leq M\leq 3\times 10^5
  • 1 \leq U_i < V_i\leq N
  • All (U_i,V_i) are different.
  • 1\leq W_i\leq 10^9
  • 1 \leq K\leq N
  • 1\leq A_1<A_2<\cdots<A_K\leq N
  • 1 \leq D\leq 3\times 10^5
  • 1\leq X_i\leq 10^9
  • All input values are integers.

Input

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

N M
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
K
A_1 A_2 \ldots A_K
D
X_1 X_2 \ldots X_D

Output

Print N lines.
The i-th line (1\leq i\leq N) should contain the day on which the person living in room i was newly infected.


Sample Input 1

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

Sample Output 1

0
1
1
2

The infection spreads as follows.

  • On the night of day 0, the person living in room 1 gets infected.
  • The distances between room 1 and rooms 2,3,4 are 2,3,5, respectively. Thus, since X_1=3, the people living in rooms 2 and 3 are newly infected on the night of day 1.
  • The distance between rooms 3 and 4 is 2. Thus, since X_2=3, the person living in room 4 also gets infected on the night of day 2.

Therefore, the people living in rooms 1,2,3,4 were newly infected on days 0,1,1,2, respectively, so print 0,1,1,2 in this order on separate lines.


Sample Input 2

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

Sample Output 2

0
1
2
-1
2
0
-1

Sample Input 3

5 1
1 2 5
2
1 3
3
3 7 5

Sample Output 3

0
2
0
-1
-1

Note that it is not always possible to move between any two rooms using only corridors.