E - Rotation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

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

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

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

制約

  • 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

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

t x

出力

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


入力例 1

3 3
abc
2 2
1 1
2 2

出力例 1

b
a

1 個目のクエリのとき、S は abc なので 2 文字目の b を出力します。 2 個目のクエリのとき、S は abc から cab に変わります。 3 個目のクエリのとき、S は cab なので 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
F - Operate 1

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

この問題は F 問題 (Operate K) の部分問題であり、 K=1 です。
F 問題に正解するコードをこの問題に提出することで、この問題に正解できます。

文字列 S に対して以下の操作を 0 回以上 K 回以下行って、文字列 T と一致させられるか判定してください。

  • 次の 3 種類の操作のうちひとつを選択し、実行する。
    • S 中の (先頭や末尾を含む) 任意の位置に、任意の文字を 1 つ挿入する。
    • S 中の文字を 1 つ選び、削除する。
    • S 中の文字を 1 つ選び、別の 1 つの文字に変更する。

制約

  • S,T は英小文字からなる長さ 1 以上 500000 以下の文字列
  • \color{red}{K=1}

入力

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

K
S
T

出力

K 回以下の操作で S を T に一致させられる時 Yes 、そうでない時 No と出力せよ。


入力例 1

1
abc
agc

出力例 1

Yes

abc の 2 文字目の b を g に置き換えることで、 abc を 1 回の操作で agc に変換できます。


入力例 2

1
abc
awtf

出力例 2

No

1 回の操作では abc を awtf に変換できません。


入力例 3

1
abc
ac

出力例 3

Yes

abc の 2 文字目の b を削除することで、 abc を 1 回の操作で ac に変換できます。


入力例 4

1
back
black

出力例 4

Yes

back の 1 文字目と 2 文字目の間に l を挿入することで、 back を 1 回の操作で black に変換できます。


入力例 5

1
same
same

出力例 5

Yes

初めから S=T である場合もあります。


入力例 6

1
leap
read

出力例 6

No

Score : 350 points

Problem Statement

This problem is a sub-problem of Problem F (Operate K), with K=1.
You can solve this problem by submitting a correct solution for Problem F to this problem.

Determine whether it is possible to perform the following operation on string S between 0 and K times, inclusive, to make it identical to string T.

  • Choose one of the following three operations and execute it.
    • Insert any one character at any position in S (possibly the beginning or end).
    • Delete one character from S.
    • Choose one character in S and replace it with another character.

Constraints

  • Each of S and T is a string of length between 1 and 500000, inclusive, consisting of lowercase English letters.
  • \color{red}{K=1}

Input

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

K
S
T

Output

If S can be made identical to T with at most K operations, print Yes; otherwise, print No.


Sample Input 1

1
abc
agc

Sample Output 1

Yes

Replacing the second character b of abc with g converts abc to agc in one operation.


Sample Input 2

1
abc
awtf

Sample Output 2

No

abc cannot be converted to awtf in one operation.


Sample Input 3

1
abc
ac

Sample Output 3

Yes

Deleting the second character b of abc converts abc to ac in one operation.


Sample Input 4

1
back
black

Sample Output 4

Yes

Inserting l between the first and second characters of back converts back to black in one operation.


Sample Input 5

1
same
same

Sample Output 5

Yes

It is also possible that S = T from the beginning.


Sample Input 6

1
leap
read

Sample Output 6

No
G - Merge Slimes

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

最初、N 種類のサイズのスライムがいます。
具体的には、1\leq i\leq N について、サイズ S_i のスライムが C_i 匹います。

高橋君はスライムの合成を好きな順番で好きなだけ(0 回でも良い)繰り返すことができます。
スライムの合成では、次のことを行います。

  • 同じ サイズの 2 匹のスライムを選ぶ。選ばれたスライムのサイズが X であったとき、新しくサイズ 2X のスライムが出現する。合成後、選ばれた元のスライムは 2 匹とも消滅する。

高橋君はスライムの匹数を最小にしたいと考えています。 高橋君がうまく合成を繰り返した時、最小で何匹にすることができるでしょうか?

制約

  • 1\leq N\leq 10^5
  • 1\leq S_i\leq 10^9
  • 1\leq C_i\leq 10^9
  • S_1,S_2,\ldots,S_N はすべて異なる。
  • 入力はすべて整数

入力

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

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

出力

高橋君が合成を繰り返した後のスライムの匹数としてあり得る最小の数を出力せよ。


入力例 1

3
3 3
5 1
6 1

出力例 1

3

最初、サイズ 3 のスライムが 3 匹、サイズ 5 のスライムが 1 匹、サイズ 6 のスライムが 1 匹います。
高橋君は次のように合成を 2 回行うことができます。

  • まず、サイズ 3 のスライム 2 匹を選んで合成を行います。サイズ 3 のスライムが 1 匹、サイズ 5 のスライムが 1 匹、サイズ 6 のスライムが 2 匹となります。
  • 次に、サイズ 6 のスライム 2 匹を選んで合成を行います。サイズ 3 のスライムが 1 匹、サイズ 5 のスライムが 1 匹、サイズ 12 のスライムが 1 匹となります。

高橋君は最初の状態からどのように合成を繰り返してもスライムを 2 匹以下にすることはできないため、3 を出力します。


入力例 2

3
1 1
2 1
3 1

出力例 2

3

高橋君は合成を行うことができません。


入力例 3

1
1000000000 1000000000

出力例 3

13

Score : 425 points

Problem Statement

Initially, there are N sizes of slimes.
Specifically, for each 1\leq i\leq N, there are C_i slimes of size S_i.

Takahashi can repeat slime synthesis any number of times (possibly zero) in any order.
Slime synthesis is performed as follows.

  • Choose two slimes of the same size. Let this size be X, and a new slime of size 2X appears. Then, the two original slimes disappear.

Takahashi wants to minimize the number of slimes. What is the minimum number of slimes he can end up with by an optimal sequence of syntheses?

Constraints

  • 1\leq N\leq 10^5
  • 1\leq S_i\leq 10^9
  • 1\leq C_i\leq 10^9
  • S_1,S_2,\ldots,S_N are all different.
  • All input values are integers.

Input

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

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

Output

Print the minimum possible number of slimes after Takahashi has repeated the synthesis.


Sample Input 1

3
3 3
5 1
6 1

Sample Output 1

3

Initially, there are three slimes of size 3, one of size 5, and one of size 6.
Takahashi can perform the synthesis twice as follows:

  • First, perform the synthesis by choosing two slimes of size 3. There will be one slime of size 3, one of size 5, and two of size 6.
  • Next, perform the synthesis by choosing two slimes of size 6. There will be one slime of size 3, one of size 5, and one of size 12.

No matter how he repeats the synthesis from the initial state, he cannot reduce the number of slimes to 2 or less, so you should print 3.


Sample Input 2

3
1 1
2 1
3 1

Sample Output 2

3

He cannot perform the synthesis.


Sample Input 3

1
1000000000 1000000000

Sample Output 3

13
H - Edge Deletion

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 頂点 M 辺の単純連結無向グラフが与えられます。
辺 i は頂点 A_i と頂点 B_i を結ぶ長さ C_i の辺です。

以下の条件を満たすようにいくつかの辺を削除します。削除する辺の数の最大値を求めてください。

  • 辺を削除した後のグラフも連結である。
  • 全ての頂点対 (s,t) について、頂点 s と頂点 t の間の距離が削除前と削除後で変化しない。

注釈

単純連結無向グラフとは、単純かつ連結で辺に向きの無いグラフのことをいいます。
グラフが単純であるとは、グラフが自己ループや多重辺を含まないことをいいます。
グラフが連結であるとは、グラフ上の任意の 2 頂点 s, t について s から t へ辺をたどって行けることをいいます。
頂点 s と頂点 t の間の距離とは、頂点 s と頂点 t の間の最短路の長さのことをいいます。

制約

  • 2 \leq N \leq 300
  • N-1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i \lt B_i \leq N
  • 1 \leq C_i \leq 10^9
  • i \neq j ならば (A_i, B_i) \neq (A_j, B_j) である。
  • 与えられるグラフは連結である。
  • 入力はすべて整数である。

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

答えを出力せよ。


入力例 1

3 3
1 2 2
2 3 3
1 3 6

出力例 1

1

辺を削除する前の全ての頂点対の距離は次の通りです。

  • 頂点 1 と頂点 2 の距離は 2
  • 頂点 1 と頂点 3 の距離は 5
  • 頂点 2 と頂点 3 の距離は 3

辺 3 を削除しても全ての頂点間の距離は変化しません。また、問題文の条件を満たすように 2 本以上の辺を削除することはできないので、答えは 1 本になります。


入力例 2

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

出力例 2

0

どの辺も削除することができません。


入力例 3

5 10
1 2 71
1 3 9
1 4 82
1 5 64
2 3 22
2 4 99
2 5 1
3 4 24
3 5 18
4 5 10

出力例 3

5

Score : 500 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges.
Edge i connects Vertex A_i and Vertex B_i, and has a length of C_i.

Let us delete some number of edges while satisfying the conditions below. Find the maximum number of edges that can be deleted.

  • The graph is still connected after the deletion.
  • For every pair of vertices (s, t), the distance between s and t remains the same before and after the deletion.

Notes

A simple connected undirected graph is a graph that is simple and connected and has undirected edges.
A graph is simple when it has no self-loops and no multi-edges.
A graph is connected when, for every pair of two vertices s and t, t is reachable from s by traversing edges.
The distance between Vertex s and Vertex t is the length of a shortest path between s and t.

Constraints

  • 2 \leq N \leq 300
  • N-1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i \lt B_i \leq N
  • 1 \leq C_i \leq 10^9
  • (A_i, B_i) \neq (A_j, B_j) if i \neq j.
  • The given graph is connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

Output

Print the answer.


Sample Input 1

3 3
1 2 2
2 3 3
1 3 6

Sample Output 1

1

The distance between each pair of vertices before the deletion is as follows.

  • The distance between Vertex 1 and Vertex 2 is 2.
  • The distance between Vertex 1 and Vertex 3 is 5.
  • The distance between Vertex 2 and Vertex 3 is 3.

Deleting Edge 3 does not affect the distance between any pair of vertices. It is impossible to delete two or more edges while satisfying the condition, so the answer is 1.


Sample Input 2

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

Sample Output 2

0

No edge can be deleted.


Sample Input 3

5 10
1 2 71
1 3 9
1 4 82
1 5 64
2 3 22
2 4 99
2 5 1
3 4 24
3 5 18
4 5 10

Sample Output 3

5
I - Operations on a Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

縦 N 行、横 M 列の行列があり、はじめ全ての成分は 0 です。

以下のいずれかの形式で表されるクエリを Q 個処理してください。

  • 1 l r x : l 列目、l+1 列目、\ldots、r 列目の成分全てに x を足す。
  • 2 i x : i 行目の成分全てを x で置き換える。
  • 3 i j : (i, j) 成分を出力する。

制約

  • 1 \leq N, M, Q \leq 2 \times 10^5
  • 1 l r x の形式のクエリについて、1 \leq l \leq r \leq M かつ 1 \leq x \leq 10^9
  • 2 i x の形式のクエリについて、1 \leq i \leq N かつ 1 \leq x \leq 10^9
  • 3 i j の形式にクエリについて、1 \leq i \leq N かつ 1 \leq j \leq M
  • 3 i j の形式のクエリが一個以上与えられる
  • 入力は全て整数

入力

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

N M Q
\mathrm{Query}_1
\vdots
\mathrm{Query}_Q

i 番目に与えられるクエリを表す \mathrm{Query}_i は以下のいずれかの形式である。

1 l r x
2 i x
3 i j

出力

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


入力例 1

3 3 9
1 1 2 1
3 2 2
2 3 2
3 3 3
3 3 1
1 2 3 3
3 3 2
3 2 3
3 1 2

出力例 1

1
2
2
5
3
4

行列は次のように変化します。

\begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 1 & 0 \\ 1 & 1 & 0 \\ 1 & 1 & 0 \\ \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 1 & 0 \\ 1 & 1 & 0 \\ 2 & 2 & 2 \\ \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 4 & 3 \\ 1 & 4 & 3 \\ 2 & 5 & 5 \\ \end{pmatrix}


入力例 2

1 1 10
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
3 1 1

出力例 2

9000000000

入力例 3

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

出力例 3

6
5
5
13
10
0

Score : 500 points

Problem Statement

We have an N \times M matrix, whose elements are initially all 0.

Process Q given queries. Each query is in one of the following formats.

  • 1 l r x : Add x to every element in the l-th, (l+1)-th, \ldots, and r-th column.
  • 2 i x: Replace every element in the i-th row with x.
  • 3 i j: Print the (i, j)-th element.

Constraints

  • 1 \leq N, M, Q \leq 2 \times 10^5
  • Every query is in one of the formats listed in the Problem Statement.
  • For each query in the format 1 l r x, 1 \leq l \leq r \leq M and 1 \leq x \leq 10^9.
  • For each query in the format 2 i x, 1 \leq i \leq N and 1 \leq x \leq 10^9.
  • For each query in the format 3 i j, 1 \leq i \leq N and 1 \leq j \leq M.
  • At least one query in the format 3 i j is given.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
\mathrm{Query}_1
\vdots
\mathrm{Query}_Q

\mathrm{Query}_i, which denotes the i-th query, is in one of the following formats:

1 l r x
2 i x
3 i j

Output

For each query in the format 3 i j, print a single line containing the answer.


Sample Input 1

3 3 9
1 1 2 1
3 2 2
2 3 2
3 3 3
3 3 1
1 2 3 3
3 3 2
3 2 3
3 1 2

Sample Output 1

1
2
2
5
3
4

The matrix transitions as follows.

\begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 1 & 0 \\ 1 & 1 & 0 \\ 1 & 1 & 0 \\ \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 1 & 0 \\ 1 & 1 & 0 \\ 2 & 2 & 2 \\ \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 4 & 3 \\ 1 & 4 & 3 \\ 2 & 5 & 5 \\ \end{pmatrix}


Sample Input 2

1 1 10
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
1 1 1 1000000000
3 1 1

Sample Output 2

9000000000

Sample Input 3

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

Sample Output 3

6
5
5
13
10
0