A - <Inversion>

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

配点 : 300

問題文

<, > からなる長さ N-1 の文字列 S が与えられます.

長さ N の数列 x=(x_1,x_2,\cdots,x_N) が以下の条件を満たすとき,それをよい数列と呼ぶことにします.

  • i (1 \leq i \leq N-1) について,Si 文字目が < なら x_i\lt x_{i+1} で,> なら x_i \gt x_{i+1}

よい数列の転倒数としてあり得る最小値を求めてください.

数列の転倒数とは

長さ n の数列 x=(x_1,x_2,\cdots,x_n) の転倒数とは,整数の組 (i,j) (1 \leq i < j \leq n) であって,x_i>x_j を満たすものの個数です.

制約

  • 2 \leq N \leq 250000
  • S<, > からなる長さ N-1 の文字列.
  • 入力される値はすべて整数.

入力

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

N
S

出力

答えを出力せよ.


入力例 1

4
<><

出力例 1

1

x=(1,2,1,2) とすると,これはよい数列です. また,x の転倒数は 1 です. 転倒数が 0 のよい数列は存在しないので,1 が答えになります.


入力例 2

2
<

出力例 2

0

入力例 3

10
>>>>>>>>>

出力例 3

45

入力例 4

30
<<><>>><><>><><><<>><<<><><<>

出力例 4

19

Score : 300 points

Problem Statement

You are given a string S of length N-1 consisting of < and >.

A sequence x=(x_1,x_2,\cdots,x_N) of length N is called a good sequence if and only if it satisfies the following condition:

  • For each i (1 \leq i \leq N-1), if the i-th character of S is <, then x_i\lt x_{i+1}; if it is >, then x_i \gt x_{i+1}.

Find the minimum possible inversion number of a good sequence.

What is the inversion number of a sequence?

The inversion number of a sequence x=(x_1,x_2,\cdots,x_n) of length n is the number of pairs of integers (i,j) (1 \leq i < j \leq n) such that x_i>x_j.

Constraints

  • 2 \leq N \leq 250000
  • S is a string of length N-1 consisting of < and >.
  • All input values are integers.

Input

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

N
S

Output

Print the answer.


Sample Input 1

4
<><

Sample Output 1

1

x=(1,2,1,2) is a good sequence, and its inversion number is 1. There is no good sequence whose inversion number is 0, so the answer is 1.


Sample Input 2

2
<

Sample Output 2

0

Sample Input 3

10
>>>>>>>>>

Sample Output 3

45

Sample Input 4

30
<<><>>><><>><><><<>><<<><><<>

Sample Output 4

19
B - Arbitrary Nim

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

配点 : 400

問題文

石の山が N 個あり,i 番目 (1 \leq i \leq N) の山には A_i 個の石が積まれています.

あなたは今から正整数 k を選びます. その後,Alice と Bob がこの山を使って以下のようなゲームを行います.

  • Alice から始めて両者は交互に手番をプレイする.
  • 各手番では,プレイヤーは空でない山を一つ選び,その山から 1 個以上 k 個以下の好きな個数の石を取り除く.

自分の手番で操作が行えなくなった人が負けで,負けなかった人が勝ちです.

あなたは正整数 k を選ぶとき,Alice に必勝法が存在するようにしたいです. そのような k が存在するか判定してください. 存在する場合は,そのような k の最大値が存在するか判定してください. 最大値が存在する場合は,その値を答えてください.

制約

  • 1 \leq N \leq 250000
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数.

入力

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

N
A_1 A_2 \cdots A_N

出力

Alice に必勝法が存在するような k が存在しない場合,0 を出力せよ.

Alice に必勝法が存在するような k が存在するが,その最大値が存在しない場合,-1 を出力せよ.

Alice に必勝法が存在するような k が存在し,その最大値も存在する場合,その値を出力せよ.


入力例 1

3
1 2 3

出力例 1

2

例えば,k=2 とすると Alice に必勝法が存在します. 3 \leq k を選んだ場合は Alice に必勝法が存在しないので,k=2 が答えです.


入力例 2

4
1 2 3 4

出力例 2

-1

例えば,すべての k=100,200,300,\cdots に対して Alice に必勝法が存在します. よって k の最大値は存在しないため,-1 を出力します.


入力例 3

2
100 100

出力例 3

0

どのような k を選んでも Alice に必勝法は存在しません. よって 0 を出力します.

Score : 400 points

Problem Statement

There are N piles of stones, and the i-th pile (1 \leq i \leq N) has A_i stones.

You will choose a positive integer k. Then, Alice and Bob will play a game with these piles as follows.

  • Starting with Alice, the players take turns to play.
  • In each turn, the player must choose a non-empty pile and remove from it some number of stones between 1 and k, inclusive.

The person who cannot make a move on their turn loses, and the other person wins.

You want to choose a positive integer k for which there is a winning strategy for Alice. Determine if such a k exists. If it exists, determine if there is a maximum value for such k. If the maximum value exists, provide that value.

Constraints

  • 1 \leq N \leq 250000
  • 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 \cdots A_N

Output

If there is no k for which Alice has a winning strategy, print 0.

If there is a k for which Alice has a winning strategy, and there is no maximum value for such k, print -1.

If there is a k for which Alice has a winning strategy, and there is a maximum value for such k, print that value.


Sample Input 1

3
1 2 3

Sample Output 1

2

For example, if k=2, Alice has a winning strategy. If k \geq 3, Alice has no winning strategy, so the answer is k=2.


Sample Input 2

4
1 2 3 4

Sample Output 2

-1

For example, Alice has winning strategies for all k=100,200,300,\cdots. Thus, there is no maximum value for k, so print -1.


Sample Input 3

2
100 100

Sample Output 3

0

No matter what k is chosen, Alice has no winning strategy. Thus, print 0.

C - Swap Characters

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

配点 : 600

問題文

A, B, C からなる長さ N の文字列 S が与えられます.

以下の操作を 0 回以上 K 回以下行うことを考えます.

  • S 内の 2 文字を自由に選び,入れ替える.

操作後の S としてあり得る文字列が何通りあるかを 998244353 で割ったあまりを求めてください.

制約

  • 1 \leq N \leq 250000
  • 1 \leq K \leq 100
  • SA, B, C からなる長さ N の文字列.
  • 入力される値はすべて整数.

入力

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

N K
S

出力

答えを出力せよ.


入力例 1

3 1
ABC

出力例 1

4

以下のように 4 通りの文字列が得られます.

  • S=ABC : 0 回の操作を行えばよい.
  • S=BAC : 1,2 文字目を入れ替える操作を行えばよい.
  • S=CBA : 1,3 文字目を入れ替える操作を行えばよい.
  • S=ACB : 2,3 文字目を入れ替える操作を行えばよい.

入力例 2

3 2
ABC

出力例 2

6

入力例 3

4 5
AAAA

出力例 3

1

入力例 4

30 10
CACCABAABBABABBCBBCAAACAAACCCA

出力例 4

42981885

Score : 600 points

Problem Statement

You are given a string S of length N consisting of the characters A, B, and C.

Consider performing the following operation between 0 and K times, inclusive:

  • Freely choose two characters in S and swap them.

Find the number of strings that S can be after the operations, modulo 998244353.

Constraints

  • 1 \leq N \leq 250000
  • 1 \leq K \leq 100
  • S is a string of length N consisting of the characters A, B, and C.
  • All input values are integers.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

3 1
ABC

Sample Output 1

4

The following four strings can be obtained:

  • S=ABC: No operation is needed.
  • S=BAC: Swap the 1-st and 2-nd characters.
  • S=CBA: Swap the 1-st and 3-rd characters.
  • S=ACB: Swap the 2-nd and 3-rd characters.

Sample Input 2

3 2
ABC

Sample Output 2

6

Sample Input 3

4 5
AAAA

Sample Output 3

1

Sample Input 4

30 10
CACCABAABBABABBCBBCAAACAAACCCA

Sample Output 4

42981885
D - Maximize Update

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

配点 : 700

問題文

N 個のマスが横一列に並んでおり,左から順に 1 から N の番号がついています. 最初,すべてのマスは白色です.

あなたは以下の M 種類の操作を好きな順序で好きな回数行うことができます.

  • i 種類目の操作: マス L_i からマス R_i までを黒色で塗る.

マス目の状態を変化させるような操作の回数の最大値を求めてください. なお,操作を行った結果色が変化したマスが 1 つでもあれば,その操作はマス目の状態を変化させたとみなします.

制約

  • 1 \leq N \leq 500
  • 1 \leq M \leq N(N+1)/2
  • 1 \leq L_i \leq R_i \leq N
  • (L_i,R_i) \neq (L_j,R_j) (i \neq j)
  • 入力される値はすべて整数.

入力

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

出力

答えを出力せよ.


入力例 1

3 3
1 3
1 1
3 3

出力例 1

3

以下のように操作すると,マス目の状態を変化させる操作が 3 回行われます.

  • 2 種類目の操作を行う.新たにマス 1 が黒色で塗られる.
  • 3 種類目の操作を行う.新たにマス 3 が黒色で塗られる.
  • 1 種類目の操作を行う.新たにマス 2 が黒色で塗られる.

入力例 2

4 3
1 2
3 4
1 4

出力例 2

2

入力例 3

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

出力例 3

4

入力例 4

20 15
2 4
16 19
7 13
1 15
3 18
10 11
1 10
1 7
14 16
1 16
2 17
1 17
12 14
3 17
4 10

出力例 4

11

Score : 700 points

Problem Statement

There are N squares arranged in a row, numbered 1 to N from left to right. Initially, all squares are white.

You can perform the following M types of operations any number of times in any order:

  • Type-i operation: Paint square L_i through square R_i in black.

Find the maximum number of times the operations can change the state of the squares. If an operation changes the color of at least one square, that operation is considered to change the state of the squares.

Constraints

  • 1 \leq N \leq 500
  • 1 \leq M \leq N(N+1)/2
  • 1 \leq L_i \leq R_i \leq N
  • (L_i,R_i) \neq (L_j,R_j) for all i \neq j
  • All input values are integers.

Input

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the answer.


Sample Input 1

3 3
1 3
1 1
3 3

Sample Output 1

3

If you perform the operations as follows, they will change the state of the squares three times.

  • Perform type-2 operation. Square 1 is newly painted black.
  • Perform type-3 operation. Square 3 is newly painted black.
  • Perform type-1 operation. Square 2 is newly painted black.

Sample Input 2

4 3
1 2
3 4
1 4

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4

Sample Input 4

20 15
2 4
16 19
7 13
1 15
3 18
10 11
1 10
1 7
14 16
1 16
2 17
1 17
12 14
3 17
4 10

Sample Output 4

11
E - Subsegments with Large Sums

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

配点 : 800

問題文

長さ N の正整数列 A=(A_1,A_2,\cdots,A_N) が与えられます.

この数列を K 個の非空な連続部分列に分割することを考えます. この K 個の連続部分列のうち,要素の総和が S 以上であるものの個数をスコアと呼ぶことにします. スコアの最大値を求めてください.

制約

  • 1 \leq K \leq N \leq 250000
  • 1 \leq A_i \leq 10^9
  • 1 \leq S \leq 10^{15}
  • 入力される値はすべて整数.

入力

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

N K S
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

4 3 6
1 4 2 8

出力例 1

2

数列を (1),(4,2),(8) と分割すると,スコアが 2 になります. これより大きいスコアは達成できないため,答えは 2 です.


入力例 2

10 5 2
1 1 1 1 1 1 1 1 1 1

出力例 2

5

入力例 3

10 5 3
1 1 1 1 1 1 1 1 1 1

出力例 3

2

入力例 4

20 6 946667802
786820955 250480341 710671229 946667801 19271059 404902145 251317818 22712439 520643153 344670307 274195604 561032101 140039457 543856068 521915711 857077284 499774361 419370025 744280520 249168130

出力例 4

6

Score : 800 points

Problem Statement

You are given a sequence of positive integers A=(A_1,A_2,\cdots,A_N) of length N.

Consider dividing this sequence into K non-empty contiguous subsequences. Among these K contiguous subsequences, the number of ones whose sum of elements is at least S will be called the score. Find the maximum value of the score.

Constraints

  • 1 \leq K \leq N \leq 250000
  • 1 \leq A_i \leq 10^9
  • 1 \leq S \leq 10^{15}
  • All input values are integers.

Input

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

N K S
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4 3 6
1 4 2 8

Sample Output 1

2

If the sequence is divided into (1),(4,2),(8), the score will be 2. No higher score can be achieved, so the answer is 2.


Sample Input 2

10 5 2
1 1 1 1 1 1 1 1 1 1

Sample Output 2

5

Sample Input 3

10 5 3
1 1 1 1 1 1 1 1 1 1

Sample Output 3

2

Sample Input 4

20 6 946667802
786820955 250480341 710671229 946667801 19271059 404902145 251317818 22712439 520643153 344670307 274195604 561032101 140039457 543856068 521915711 857077284 499774361 419370025 744280520 249168130

Sample Output 4

6
F - Up-Down Queries

実行時間制限: 5 sec / メモリ制限: 1024 MB

配点 : 1100

問題文

各要素が 0 以上 M 以下で長さが N の整数列 x=(x_1,x_2,\cdots,x_N) に対し,f(x) を次のように定義します.

  • 長さ M の整数列 y=(y_1,y_2,\cdots,y_M) を用意する. 最初,y の要素はすべて 0 にする. その後,各 i=1,2,\cdots,N に対しこの順に以下の操作を行う.
    • 各整数 j (1 \leq j \leq x_i) に対し,y_j の値を \max(y_j-1,0) で置き換える.
    • 各整数 j (x_i < j \leq M) に対し,y_j の値を y_j+1 で置き換える.
  • すべての操作が終わった時点での y の要素の総和を f(x) の値とする.

各要素が 0 以上 M 以下で長さが N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます. Q 個のクエリを処理してください.

  • i 番目のクエリ: 整数 X_i,Y_i が与えられるので,A_{X_i} の値を Y_i で置き換える. その後,f(A) の値を出力する.

制約

  • 1 \leq N,M,Q \leq 250000
  • 0 \leq A_i \leq M
  • 1 \leq X_i \leq N
  • 0 \leq Y_i \leq M
  • 入力される値はすべて整数.

入力

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

N M Q
A_1 A_2 \cdots A_N
X_1 Y_1
X_2 Y_2
\vdots
X_Q Y_Q

出力

答えを出力せよ.


入力例 1

3 4 2
1 2 3
1 4
3 0

出力例 1

2
6

まず 1 番目のクエリを考えます. A_1 の値を 4 で置き換え,A=(4,2,3) になります. そして,f(A) の値は次のように求まります.

  • y=(0,0,0,0) を用意する.
  • A_1=4 について操作を行い,y=(0,0,0,0) になる.
  • A_2=2 について操作を行い,y=(0,0,1,1) になる.
  • A_3=3 について操作を行い,y=(0,0,0,2) になる.
  • y の要素の総和 =2f(A) の値となる.

次に 2 番目のクエリを考えます. A_3 の値を 0 で置き換え,A=(4,2,0) になります. そして,f(A) の値は次のように求まります.

  • y=(0,0,0,0) を用意する.
  • A_1=4 について操作を行い,y=(0,0,0,0) になる.
  • A_2=2 について操作を行い,y=(0,0,1,1) になる.
  • A_3=0 について操作を行い,y=(1,1,2,2) になる.
  • y の要素の総和 =6f(A) の値となる.

入力例 2

7 2 9
2 0 2 2 0 1 0
1 1
3 0
4 0
4 1
6 1
3 2
2 0
3 2
2 0

出力例 2

4
7
11
9
9
6
6
6
6

入力例 3

20 200000 10
39664 143179 193565 153887 16141 91985 51452 155409 116777 190060 87620 64458 106481 51272 9108 100995 139248 18243 181424 6182
4 196305
13 59753
8 96194
6 57037
19 125781
16 142779
15 13967
10 17772
16 84763
12 17283

出力例 3

1145670
1234421
1352851
1352851
1464137
1380569
1380569
1608611
1724643
1736769

Score : 1100 points

Problem Statement

For an integer sequence x=(x_1,x_2,\cdots,x_N) of length N with each element being between 0 and M, inclusive, we define f(x) as follows:

  • Prepare an integer sequence y=(y_1,y_2,\cdots,y_M) of length M. Initially, set all elements of y to 0. Then, for each i=1,2,\cdots,N, in order, perform the following operations:
    • For each integer j (1 \leq j \leq x_i), replace the value of y_j with \max(y_j-1,0).
    • For each integer j (x_i < j \leq M), replace the value of y_j with y_j+1.
  • The sum of the elements of y after all operations is the value of f(x).

You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N with each element being between 0 and M, inclusive. Process Q queries:

  • The i-th query: Given integers X_i and Y_i, replace the value of A_{X_i} with Y_i. Then, print the value of f(A).

Constraints

  • 1 \leq N,M,Q \leq 250000
  • 0 \leq A_i \leq M
  • 1 \leq X_i \leq N
  • 0 \leq Y_i \leq M
  • All input values are integers.

Input

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

N M Q
A_1 A_2 \cdots A_N
X_1 Y_1
X_2 Y_2
\vdots
X_Q Y_Q

Output

Print the answer.


Sample Input 1

3 4 2
1 2 3
1 4
3 0

Sample Output 1

2
6

Consider the first query. We replace the value of A_1 with 4, making A=(4,2,3). Then, the value of f(A) is calculated as follows:

  • Prepare y=(0,0,0,0).
  • Perform the operation for A_1=4, resulting in y=(0,0,0,0).
  • Perform the operation for A_2=2, resulting in y=(0,0,1,1).
  • Perform the operation for A_3=3, resulting in y=(0,0,0,2).
  • The sum of the elements of y, which equals 2, is the value of f(A).

Next, consider the second query. We replace the value of A_3 with 0, making A=(4,2,0). Then, the value of f(A) is calculated as follows:

  • Prepare y=(0,0,0,0).
  • Perform the operation for A_1=4, resulting in y=(0,0,0,0).
  • Perform the operation for A_2=2, resulting in y=(0,0,1,1).
  • Perform the operation for A_3=0, resulting in y=(1,1,2,2).
  • The sum of the elements of y, which equals 6, is the value of f(A).

Sample Input 2

7 2 9
2 0 2 2 0 1 0
1 1
3 0
4 0
4 1
6 1
3 2
2 0
3 2
2 0

Sample Output 2

4
7
11
9
9
6
6
6
6

Sample Input 3

20 200000 10
39664 143179 193565 153887 16141 91985 51452 155409 116777 190060 87620 64458 106481 51272 9108 100995 139248 18243 181424 6182
4 196305
13 59753
8 96194
6 57037
19 125781
16 142779
15 13967
10 17772
16 84763
12 17283

Sample Output 3

1145670
1234421
1352851
1352851
1464137
1380569
1380569
1608611
1724643
1736769