A - セグメント木

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

配点 : 3 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 i x:A_i を x に変更する。
  • 2 l r:\min(A_l,A_{l+1},\ldots,A_r) を出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 種類目のクエリについて、1 \leq i \leq N, 0 \leq x \leq 10^9
  • 2 種類目のクエリについて、1 \leq l \leq r \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

3
2
8

1 番目のクエリでは、A_2,A_3,A_4 の最小値である 3 を出力します。

2 番目のクエリでは、A が (5,3,2,9,1) に変更されます。

3 番目のクエリでは、A_2,A_3,A_4 の最小値である 2 を出力します。


入力例 2

3 4
0 1000000000 7
2 1 1
2 2 2
1 1 1000000000
2 1 3

出力例 2

0
1000000000
7

Score: 3 points

Problem Statement

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

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

  • 1 i x: Change A_i to x.
  • 2 l r: Print \min(A_l,A_{l+1},\ldots,A_r).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • For type 1 queries, 1 \leq i \leq N, 0 \leq x \leq 10^9
  • For type 2 queries, 1 \leq l \leq r \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

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

Sample Output 1

3
2
8

For query 1, print 3, the minimum of A_2,A_3,A_4.

Query 2 changes A to (5,3,2,9,1).

For query 3, print 2, the minimum of A_2,A_3,A_4.


Sample Input 2

3 4
0 1000000000 7
2 1 1
2 2 2
1 1 1000000000
2 1 3

Sample Output 2

0
1000000000
7
B - 遅延セグメント木

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

配点 : 3 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 l r x:i=l,l+1,\ldots,r のそれぞれについて、A_i に x を加える。
  • 2 l r:\min(A_l,A_{l+1},\ldots,A_r) を出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 種類目のクエリについて、1 \leq l \leq r \leq N, 0 \leq x \leq 10^9
  • 2 種類目のクエリについて、1 \leq l \leq r \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 l r x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

3
8
11
7

1 番目のクエリでは、A_2,A_3,A_4 の最小値である 3 を出力します。

2 番目のクエリでは、A は (5,8,12,14,1) に変更されます。

3 番目のクエリでは、A_2,A_3,A_4 の最小値である 8 を出力します。


入力例 2

1 5
1000000000
2 1 1
1 1 1 1000000000
2 1 1
1 1 1 1000000000
2 1 1

出力例 2

1000000000
2000000000
3000000000

Score: 3 points

Problem Statement

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

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

  • 1 l r x: For each i=l,l+1,\ldots,r, add x to A_i.
  • 2 l r: Print \min(A_l,A_{l+1},\ldots,A_r).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • For type 1 queries, 1 \leq l \leq r \leq N, 0 \leq x \leq 10^9
  • For type 2 queries, 1 \leq l \leq r \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 l r x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

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

Sample Output 1

3
8
11
7

For query 1, print 3, the minimum of A_2,A_3,A_4.

Query 2 changes A to (5,8,12,14,1).

For query 3, print 8, the minimum of A_2,A_3,A_4.


Sample Input 2

1 5
1000000000
2 1 1
1 1 1 1000000000
2 1 1
1 1 1 1000000000
2 1 1

Sample Output 2

1000000000
2000000000
3000000000
C - セグメント木上の二分探索

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

配点 : 3 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 i x:A_i を x に変更する。
  • 2 l t:\displaystyle \sum_{i=l}^{r} A_i \geq t を満たす l 以上 N 以下の整数 r のうち、最小のものを出力する。そのような整数 r が存在しない場合は、N+1 を出力する。
  • 3 r t:\displaystyle \sum_{i=l}^{r} A_i \geq t を満たす 1 以上 r 以下の整数 l のうち、最大のものを出力する。そのような整数 l が存在しない場合は、0 を出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 種類目のクエリについて、1 \leq i \leq N, 0 \leq x \leq 10^9
  • 2 種類目のクエリについて、1 \leq l \leq N, 1 \leq t \leq 10^{15}
  • 3 種類目のクエリについて、1 \leq r \leq N, 1 \leq t \leq 10^{15}
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l t
3 r t

出力

2 種類目または 3 種類目のクエリを合わせた個数を q として、q 行出力せよ。j 行目には、2 種類目または 3 種類目のクエリのうち j 番目のものに対する答えを出力せよ。


入力例 1

5 8
2 1 3 0 4
2 2 4
3 5 5
1 3 1
2 2 4
3 5 5
2 4 5
3 2 4
2 1 2

出力例 1

3
3
5
3
6
0
1

1 番目のクエリでは、A_2=1 \lt 4 である一方、A_2+A_3=4 であるため、3 を出力します。

2 番目のクエリでは、A_3+A_4+A_5=7 である一方、A_4+A_5=4 であるため、3 を出力します。

3 番目のクエリでは、A は (2,1,1,0,4) に変更されます。

4 番目のクエリでは、A_2+A_3+A_4=2 \lt 4 である一方、A_2+A_3+A_4+A_5=6 であるため、5 を出力します。

5 番目のクエリでは、A_4+A_5=4 \lt 5 である一方、A_3+A_4+A_5=5 であるため、3 を出力します。

6 番目のクエリでは、A_4+A_5=4 \lt 5 であり、条件を満たす r は存在しないため、6 を出力します。

7 番目のクエリでは、A_1+A_2=3 \lt 4 であり、条件を満たす l は存在しないため、0 を出力します。

8 番目のクエリでは、A_1=2 であるため、1 を出力します。


入力例 2

1 6
0
2 1 1
3 1 1
1 1 1000000000
2 1 1000000000
3 1 1000000000
2 1 1000000000000000

出力例 2

2
0
1
1
2

Score: 3 points

Problem Statement

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

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

  • 1 i x: Change A_i to x.
  • 2 l t: Print the smallest integer r between l and N, inclusive, that satisfies \displaystyle \sum_{i=l}^{r} A_i \geq t. If no such integer r exists, print N+1.
  • 3 r t: Print the largest integer l between 1 and r, inclusive, that satisfies \displaystyle \sum_{i=l}^{r} A_i \geq t. If no such integer l exists, print 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • For type 1 queries, 1 \leq i \leq N, 0 \leq x \leq 10^9
  • For type 2 queries, 1 \leq l \leq N, 1 \leq t \leq 10^{15}
  • For type 3 queries, 1 \leq r \leq N, 1 \leq t \leq 10^{15}
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l t
3 r t

Output

Let q be the total number of queries of types 2 and 3. Print q lines. The j-th line should contain the answer to the j-th query among those of types 2 and 3, in the order they are given.


Sample Input 1

5 8
2 1 3 0 4
2 2 4
3 5 5
1 3 1
2 2 4
3 5 5
2 4 5
3 2 4
2 1 2

Sample Output 1

3
3
5
3
6
0
1

For query 1, A_2=1 \lt 4 while A_2+A_3=4, so print 3.

For query 2, A_3+A_4+A_5=7 while A_4+A_5=4, so print 3.

Query 3 changes A to (2,1,1,0,4).

For query 4, A_2+A_3+A_4=2 \lt 4 while A_2+A_3+A_4+A_5=6, so print 5.

For query 5, A_4+A_5=4 \lt 5 while A_3+A_4+A_5=5, so print 3.

For query 6, A_4+A_5=4 \lt 5, and no r satisfies the condition, so print 6.

For query 7, A_1+A_2=3 \lt 4, and no l satisfies the condition, so print 0.

For query 8, A_1=2, so print 1.


Sample Input 2

1 6
0
2 1 1
3 1 1
1 1 1000000000
2 1 1000000000
3 1 1000000000
2 1 1000000000000000

Sample Output 2

2
0
1
1
2
D - 転倒数

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

配点 : 3 点

問題文

0,1,2 からなる長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 i x:A_i を x に変更する。
  • 2 l r:数列 (A_l,A_{l+1},\ldots,A_r) の転倒数を出力する。

ここで、長さ k の数列 B=(B_1,B_2,\ldots,B_k) の 転倒数 とは、 1 \leq i \lt j \leq k かつ B_i \gt B_j を満たす整数の組 (i,j) の個数です。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 2
  • 1 種類目のクエリについて、1 \leq i \leq N, 0 \leq x \leq 2
  • 2 種類目のクエリについて、1 \leq l \leq r \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

5 5
2 0 1 2 0
2 1 5
2 2 4
1 2 2
2 1 3
2 2 5

出力例 1

5
0
2
4

はじめ、A=(2,0,1,2,0) です。

3 番目のクエリを処理した後、A=(2,2,1,2,0) となります。


入力例 2

6 6
0 2 1 0 2 1
2 1 6
1 1 2
2 1 4
1 4 2
2 3 6
2 4 4

出力例 2

5
5
2
0

Score: 3 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N consisting of 0,1,2.

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

  • 1 i x: Change A_i to x.
  • 2 l r: Print the inversion count of the sequence (A_l,A_{l+1},\ldots,A_r).

Here, the inversion count of a sequence B=(B_1,B_2,\ldots,B_k) of length k is the number of pairs of integers (i,j) satisfying 1 \leq i \lt j \leq k and B_i \gt B_j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 2
  • For type 1 queries, 1 \leq i \leq N, 0 \leq x \leq 2
  • For type 2 queries, 1 \leq l \leq r \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

5 5
2 0 1 2 0
2 1 5
2 2 4
1 2 2
2 1 3
2 2 5

Sample Output 1

5
0
2
4

Initially, A=(2,0,1,2,0).

After processing query 3, A=(2,2,1,2,0).


Sample Input 2

6 6
0 2 1 0 2 1
2 1 6
1 1 2
2 1 4
1 4 2
2 3 6
2 4 4

Sample Output 2

5
5
2
0
E - RGB

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

配点 : 3 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) と、R, G, B からなる長さ N の文字列 S が与えられます。S の i 文字目を S_i とします。

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

  • 1 i x:A_i を x に変更する。
  • 2 l r:l\leq i\lt j\lt k\leq r かつ S_i,S_j,S_k がこの順に R, G, B である整数の組 (i,j,k) すべてに対する A_iA_jA_k の総和を 998244353 で割った余りを出力する。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq Q\leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • S は R, G, B からなる長さ N の文字列
  • 1 種類目のクエリについて、1\leq i\leq N, 0\leq x\leq 10^9
  • 2 種類目のクエリについて、1\leq l\leq r\leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

5 5
1 2 3 4 5
RGBGB
2 1 5
1 2 10
2 1 5
2 2 4
2 1 3

出力例 1

36
100
0
30

最初のクエリについて、条件を満たす組は (1,2,3),(1,2,5),(1,4,5) の 3 つです。これらに対する積の総和は 1\times2\times3+1\times2\times5+1\times4\times5=36 です。


入力例 2

3 5
1000000000 1000000000 1000000000
RGB
2 1 3
1 2 998244353
2 1 3
1 2 998244354
2 1 3

出力例 2

747068572
0
716070898

Score: 3 points

Problem Statement

You are given a sequence of nonnegative integers A=(A_1,A_2,\ldots,A_N) of length N and a string S of length N consisting of R, G, and B. Let S_i denote the i-th character of S.

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

  • 1 i x: Change A_i to x.
  • 2 l r: Print the sum of A_iA_jA_k over all triples of integers (i,j,k) such that l\leq i\lt j\lt k\leq r and S_i,S_j,S_k are R, G, B in this order, modulo 998244353.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq Q\leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • S is a string of length N consisting of R, G, and B.
  • For type 1 queries, 1\leq i\leq N, 0\leq x\leq 10^9
  • For type 2 queries, 1\leq l\leq r\leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

5 5
1 2 3 4 5
RGBGB
2 1 5
1 2 10
2 1 5
2 2 4
2 1 3

Sample Output 1

36
100
0
30

For the first query, the 3 triples satisfying the conditions are (1,2,3),(1,2,5),(1,4,5). The sum of their products is 1\times2\times3+1\times2\times5+1\times4\times5=36.


Sample Input 2

3 5
1000000000 1000000000 1000000000
RGB
2 1 3
1 2 998244353
2 1 3
1 2 998244354
2 1 3

Sample Output 2

747068572
0
716070898
F - 連

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

配点 : 3 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 i x:A_i を x に変更する。
  • 2 l r:正の整数 k であって、次の条件を満たすものの最大値を出力する。
    • ある整数 p が存在し、l \leq p \leq p+k-1 \leq r かつ A_p=A_{p+1}=\cdots=A_{p+k-1} である。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 種類目のクエリについて、1 \leq i \leq N, 0 \leq x \leq 10^9
  • 2 種類目のクエリについて、1 \leq l \leq r \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

8 7
1 1 2 2 2 3 2 2
2 1 8
2 4 8
1 6 2
2 3 8
1 4 7
2 1 8
2 4 4

出力例 1

3
2
6
4
1

はじめの 2 つのクエリに対する答えは、それぞれ 3,2 です。

3 番目のクエリを処理した後、A=(1,1,2,2,2,2,2,2) となるため、続くクエリに対する答えは 6 です。


入力例 2

6 7
0 1000000000 1000000000 0 0 0
2 1 6
2 2 5
1 3 0
2 2 6
1 2 0
2 1 6
1 6 1000000000

出力例 2

3
2
4
6

Score: 3 points

Problem Statement

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

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

  • 1 i x: Change A_i to x.
  • 2 l r: Print the largest positive integer k satisfying the following condition:
    • There exists an integer p such that l \leq p \leq p+k-1 \leq r and A_p=A_{p+1}=\cdots=A_{p+k-1}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • For type 1 queries, 1 \leq i \leq N, 0 \leq x \leq 10^9
  • For type 2 queries, 1 \leq l \leq r \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

8 7
1 1 2 2 2 3 2 2
2 1 8
2 4 8
1 6 2
2 3 8
1 4 7
2 1 8
2 4 4

Sample Output 1

3
2
6
4
1

The answers to the first 2 queries are 3,2, respectively.

After processing query 3, A=(1,1,2,2,2,2,2,2), so the answer to the next query is 6.


Sample Input 2

6 7
0 1000000000 1000000000 0 0 0
2 1 6
2 2 5
1 3 0
2 2 6
1 2 0
2 1 6
1 6 1000000000

Sample Output 2

3
2
4
6
G - k 乗和

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

配点 : 3 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 l r x:i=l,l+1,\ldots,r のそれぞれについて、A_i に x を加える。
  • 2 l r k:\displaystyle \sum_{i=l}^{r} A_i^k を 998244353 で割った余りを出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 種類目のクエリについて、1 \leq l \leq r \leq N, 0 \leq x \leq 10^9
  • 2 種類目のクエリについて、1 \leq l \leq r \leq N, 1 \leq k \leq 4
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 l r x
2 l r k

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

10
29
856
6753

1 番目のクエリでは、1+2+3+4=10 を出力します。

3 番目のクエリによって A=(1,7,8,4) となります。したがって、4 番目のクエリでは 1^3+7^3+8^3=856 を出力します。


入力例 2

3 5
998244352 0 2
2 1 3 2
1 1 2 1
2 1 3 4
1 2 3 1000000000
2 1 3 1

出力例 2

5
17
3511297

Score: 3 points

Problem Statement

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

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

  • 1 l r x: For each i=l,l+1,\ldots,r, add x to A_i.
  • 2 l r k: Print \displaystyle \sum_{i=l}^{r} A_i^k modulo 998244353.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • For type 1 queries, 1 \leq l \leq r \leq N, 0 \leq x \leq 10^9
  • For type 2 queries, 1 \leq l \leq r \leq N, 1 \leq k \leq 4
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 l r x
2 l r k

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

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

Sample Output 1

10
29
856
6753

For query 1, print 1+2+3+4=10.

After query 3, A=(1,7,8,4). Therefore, for query 4, print 1^3+7^3+8^3=856.


Sample Input 2

3 5
998244352 0 2
2 1 3 2
1 1 2 1
2 1 3 4
1 2 3 1000000000
2 1 3 1

Sample Output 2

5
17
3511297
H - 加算と modulo 演算

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

配点 : 4 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 i x:A_i を x に変更する。
  • 2 l r x:与えられた x に対し、i=l,l+1,\ldots,r の順に以下の操作を行い、最終的な x の値を出力する。
    • A_i=0 のとき、x を x \bmod{998244353} に変更する。
    • A_i\gt 0 のとき、x を x+A_i に変更する。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq Q\leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • 1 種類目のクエリについて、1\leq i\leq N, 0\leq x\leq 10^9
  • 2 種類目のクエリについて、1\leq l\leq r\leq N, 0\leq x\leq 10^9
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r x

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

4 5
8 0 11 5
2 1 4 998244350
1 2 6
2 2 4 10
1 4 0
2 3 4 1000000000

出力例 1

21
32
1755658

最初のクエリでは、x は 998244350,998244358,5,16,21 と変化するため、21 を出力します。

2 番目のクエリによって、A=(8,6,11,5) となります。

3 番目のクエリでは、x は 10,16,27,32 と変化するため、32 を出力します。


入力例 2

6 7
650000000 0 910000007 123456789 0 870000013
2 3 4 750000021
2 2 6 987654321
1 5 420000009
2 3 6 314159265
1 1 0
2 1 3 271828182
2 6 6 1000000000

出力例 2

1783456817
894622424
2637616083
1181828189
1870000013

Score: 4 points

Problem Statement

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

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

  • 1 i x: Change A_i to x.
  • 2 l r x: Starting with the given x, perform the following operation for i=l,l+1,\ldots,r in this order, and print the final value of x.
    • If A_i=0, change x to x \bmod{998244353}.
    • If A_i\gt 0, change x to x+A_i.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq Q\leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • For type 1 queries, 1\leq i\leq N, 0\leq x\leq 10^9
  • For type 2 queries, 1\leq l\leq r\leq N, 0\leq x\leq 10^9
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r x

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

4 5
8 0 11 5
2 1 4 998244350
1 2 6
2 2 4 10
1 4 0
2 3 4 1000000000

Sample Output 1

21
32
1755658

For the first query, x changes as 998244350,998244358,5,16,21, so print 21.

After query 2, A=(8,6,11,5).

For query 3, x changes as 10,16,27,32, so print 32.


Sample Input 2

6 7
650000000 0 910000007 123456789 0 870000013
2 3 4 750000021
2 2 6 987654321
1 5 420000009
2 3 6 314159265
1 1 0
2 1 3 271828182
2 6 6 1000000000

Sample Output 2

1783456817
894622424
2637616083
1181828189
1870000013
I - 連続部分列

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

配点 : 4 点

問題文

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

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

  • 1 i x:A_i を x に変更する。
  • 2 l r:数列 (A_l,A_{l+1},\ldots,A_r) から、要素を共有しない非空の連続部分列を 0 個以上 2 個以下選ぶ。選んだ連続部分列に含まれる要素の総和としてあり得る最大値を出力する。

ここで、数列 (A_l,A_{l+1},\ldots,A_r) の非空の連続部分列とは、整数 s,t を l\leq s\leq t\leq r を満たすように選んだときの (A_s,A_{s+1},\ldots,A_t) のことです。

また、2 個の連続部分列を選ぶ場合、それらに含まれる位置が重複してはいけません。

制約

  • 1\leq N\leq 10^5
  • 1\leq Q\leq 10^5
  • -10^9\leq A_i\leq 10^9
  • 1 種類目のクエリについて、1\leq i\leq N, -10^9\leq x\leq 10^9
  • 2 種類目のクエリについて、1\leq l\leq r\leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

7 7
4 -5 3 -1 2 -6 5
2 1 7
2 2 6
1 4 10
2 1 7
1 6 1
2 1 7
2 2 2

出力例 1

9
5
20
25
0

はじめの 2 種類目のクエリでは、(A_3,A_4,A_5)=(3,-1,2) と (A_7)=(5) を選ぶと、要素の総和は 9 になります。これが最大です。

4 番目のクエリでは、A は (4,-5,3,10,2,-6,5) です。このとき、(A_3,A_4,A_5) と (A_7) を選ぶと、要素の総和は 20 になり、これが最大です。

7 番目のクエリでは、どの連続部分列も選ばないと要素の総和は 0 になります。これが最大です。


入力例 2

4 5
-8 -3 -5 -2
2 1 4
1 2 7
2 1 4
1 4 6
2 1 4

出力例 2

0
7
13

Score: 4 points

Problem Statement

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

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

  • 1 i x: Change A_i to x.
  • 2 l r: Choose between 0 and 2 nonempty contiguous subsequences of the sequence (A_l,A_{l+1},\ldots,A_r) that do not share any elements. Print the maximum possible sum of the elements in the chosen contiguous subsequences.

Here, a nonempty contiguous subsequence of (A_l,A_{l+1},\ldots,A_r) is a sequence (A_s,A_{s+1},\ldots,A_t) obtained by choosing integers s,t satisfying l\leq s\leq t\leq r.

Also, when choosing 2 contiguous subsequences, the positions they contain must not overlap.

Constraints

  • 1\leq N\leq 10^5
  • 1\leq Q\leq 10^5
  • -10^9\leq A_i\leq 10^9
  • For type 1 queries, 1\leq i\leq N, -10^9\leq x\leq 10^9
  • For type 2 queries, 1\leq l\leq r\leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

7 7
4 -5 3 -1 2 -6 5
2 1 7
2 2 6
1 4 10
2 1 7
1 6 1
2 1 7
2 2 2

Sample Output 1

9
5
20
25
0

For the first type 2 query, choosing (A_3,A_4,A_5)=(3,-1,2) and (A_7)=(5) gives a sum of 9. This is the maximum.

For query 4, A is (4,-5,3,10,2,-6,5). In this case, choosing (A_3,A_4,A_5) and (A_7) gives a sum of 20, which is the maximum.

For query 7, choosing no contiguous subsequences gives a sum of 0. This is the maximum.


Sample Input 2

4 5
-8 -3 -5 -2
2 1 4
1 2 7
2 1 4
1 4 6
2 1 4

Sample Output 2

0
7
13
J - 長い 01 列

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

配点 : 4 点

問題文

長さ 10^{18} の 0, 1 からなる文字列 S があります。S の i 文字目を S_i と表します。

はじめ、S のすべての文字は 0 です。

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

  • 1 l r:S_l,S_{l+1},\ldots,S_r を反転する。すなわち、各文字について 0 を 1 に、1 を 0 に変更する。
  • 2 l r:S_l,S_{l+1},\ldots,S_r に含まれる 1 の個数を出力する。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq l \leq r \leq 10^{18}
  • 入力される数値はすべて整数

入力

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

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

各クエリは以下のいずれかの形式で与えられる。

1 l r
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

0
5
3
7
7

はじめ、S のすべての文字は 0 です。

2 番目のクエリを処理した後、1 である文字は S_3,S_4,S_5,S_6,S_7 です。

4 番目のクエリを処理した後、1 である文字は S_3,S_4,S_8,S_9,S_{10},S_{11},S_{12} です。


入力例 2

6
1 999999999999999998 1000000000000000000
2 999999999999999997 1000000000000000000
1 1 1000000000000000000
2 1 1000000000000000000
2 999999999999999997 1000000000000000000
2 1 1

出力例 2

3
999999999999999997
1
1

Score: 4 points

Problem Statement

There is a string S of length 10^{18} consisting of 0 and 1. Let S_i denote the i-th character of S.

Initially, all characters of S are 0.

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

  • 1 l r: Flip S_l,S_{l+1},\ldots,S_r. That is, change each 0 to 1 and each 1 to 0.
  • 2 l r: Print the number of 1s among S_l,S_{l+1},\ldots,S_r.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq l \leq r \leq 10^{18}
  • All numerical values in the input are integers.

Input

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

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

Each query is given in one of the following formats:

1 l r
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

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

Sample Output 1

0
5
3
7
7

Initially, all characters of S are 0.

After processing query 2, the characters that are 1 are S_3,S_4,S_5,S_6,S_7.

After processing query 4, the characters that are 1 are S_3,S_4,S_8,S_9,S_{10},S_{11},S_{12}.


Sample Input 2

6
1 999999999999999998 1000000000000000000
2 999999999999999997 1000000000000000000
1 1 1000000000000000000
2 1 1000000000000000000
2 999999999999999997 1000000000000000000
2 1 1

Sample Output 2

3
999999999999999997
1
1
K - 多重集合

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

配点 : 4 点

問題文

整数の多重集合 S があります。はじめ、S は N 個の要素 A_1,A_2,\ldots,A_N をもっています。

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

  • 1 x:S に x を 1 個追加する。
  • 2 x:S から x を 1 個削除する。このクエリを処理する直前の S には、x が 1 個以上含まれることが保証される。
  • 3:S の要素を重複を含めて昇順に並べた列を x_1,x_2,\ldots,x_k とする。このとき、\displaystyle \sum_{i=1}^{k} i x_i を 998244353 で割った余りを出力する。

S が空であるときは k=0 であり、上記の和を 0 とします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 種類目および 2 種類目のクエリについて、0 \leq x \leq 10^9
  • 2 種類目のクエリを処理する直前の S には、x が 1 個以上含まれる
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 x
2 x
3

出力

3 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 3 種類目のクエリに対する答えを出力せよ。


入力例 1

4 8
3 1 3 2
3
1 0
3
2 3
3
2 3
3
1 2

出力例 1

26
35
20
8

はじめの S の要素を昇順に並べると (1,2,3,3) なので、最初のクエリでは 1 \times 1+2 \times 2+3 \times 3+4 \times 3=26 を出力します。


入力例 2

1 5
998244353
3
2 998244353
3
1 1000000000
3

出力例 2

0
0
1755647

3 番目のクエリでは、その時点では S は空なので、0 を出力します。

Score: 4 points

Problem Statement

There is a multiset S of integers. Initially, S has N elements A_1,A_2,\ldots,A_N.

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

  • 1 x: Add 1 occurrence of x to S.
  • 2 x: Remove 1 occurrence of x from S. It is guaranteed that immediately before this query is processed, S contains at least 1 occurrence of x.
  • 3: Let x_1,x_2,\ldots,x_k be the sequence obtained by sorting all elements of S in ascending order, including duplicates. Print \displaystyle \sum_{i=1}^{k} i x_i modulo 998244353.

When S is empty, k=0, and the sum above is defined to be 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • For queries of types 1 and 2, 0 \leq x \leq 10^9
  • Immediately before each type 2 query, S contains at least 1 occurrence of x.
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 x
2 x
3

Output

Let q be the number of type 3 queries. Print q lines. The j-th line should contain the answer to the j-th type 3 query.


Sample Input 1

4 8
3 1 3 2
3
1 0
3
2 3
3
2 3
3
1 2

Sample Output 1

26
35
20
8

Sorting the initial elements of S in ascending order gives (1,2,3,3), so for the first query, print 1 \times 1+2 \times 2+3 \times 3+4 \times 3=26.


Sample Input 2

1 5
998244353
3
2 998244353
3
1 1000000000
3

Sample Output 2

0
0
1755647

For query 3, S is empty at that time, so print 0.

L - 数列間の swap

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

配点 : 4 点

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N)、B=(B_1,B_2,\ldots,B_N)、C=(C_1,C_2,\ldots,C_N) が与えられます。

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

  • 1 l r:l\leq k\leq r を満たすすべての整数 k について、A_k と B_k を入れ替える。
  • 2 l r:l\leq k\leq r を満たすすべての整数 k について、B_k と C_k を入れ替える。
  • 3 l r:l\leq k\leq r を満たすすべての整数 k について、C_k と A_k を入れ替える。
  • 4 i:i\leq j\leq N かつ \displaystyle\sum_{k=i}^{j}A_k\gt0 を満たす整数 j のうち、最小のものを出力する。そのような j が存在しない場合は N+1 を出力する。

制約

  • 1\leq N\leq 10^5
  • 1\leq Q\leq 10^5
  • -10^9\leq A_i,B_i,C_i\leq 10^9
  • 1,2,3 種類目のクエリについて、1\leq l\leq r\leq N
  • 4 種類目のクエリについて、1\leq i\leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 l r
2 l r
3 l r
4 i

出力

4 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 4 種類目のクエリに対する答えを出力せよ。


入力例 1

5 7
3 -4 5 -2 1
-1 6 -3 4 -5
2 -7 1 8 -6
4 2
1 2 4
4 1
2 3 5
4 3
3 1 3
4 2

出力例 1

3
1
4
4

1 番目のクエリでは、A_2=-4\leq 0、A_2+A_3=1>0 なので、3 を出力します。

2 番目のクエリを処理した後、A=(3,6,-3,4,1) となります。

3 番目のクエリでは、A_1=3\gt0 なので、1 を出力します。


入力例 2

6 7
0 -3 3 -5 7 -2
-4 2 -1 0 5 -8
6 -2 0 4 -3 1
4 1
4 6
2 2 6
1 3 5
4 3
3 6 6
4 6

出力例 2

5
7
4
7

Score: 4 points

Problem Statement

You are given sequences of integers A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N), and C=(C_1,C_2,\ldots,C_N), each of length N.

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

  • 1 l r: For every integer k satisfying l\leq k\leq r, swap A_k and B_k.
  • 2 l r: For every integer k satisfying l\leq k\leq r, swap B_k and C_k.
  • 3 l r: For every integer k satisfying l\leq k\leq r, swap C_k and A_k.
  • 4 i: Print the smallest integer j satisfying i\leq j\leq N and \displaystyle\sum_{k=i}^{j}A_k\gt0. If no such j exists, print N+1.

Constraints

  • 1\leq N\leq 10^5
  • 1\leq Q\leq 10^5
  • -10^9\leq A_i,B_i,C_i\leq 10^9
  • For queries of types 1,2,3, 1\leq l\leq r\leq N
  • For type 4 queries, 1\leq i\leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 l r
2 l r
3 l r
4 i

Output

Let q be the number of type 4 queries. Print q lines. The j-th line should contain the answer to the j-th type 4 query.


Sample Input 1

5 7
3 -4 5 -2 1
-1 6 -3 4 -5
2 -7 1 8 -6
4 2
1 2 4
4 1
2 3 5
4 3
3 1 3
4 2

Sample Output 1

3
1
4
4

For query 1, A_2=-4\leq 0 and A_2+A_3=1>0, so print 3.

After processing query 2, A=(3,6,-3,4,1).

For query 3, A_1=3\gt0, so print 1.


Sample Input 2

6 7
0 -3 3 -5 7 -2
-4 2 -1 0 5 -8
6 -2 0 4 -3 1
4 1
4 6
2 2 6
1 3 5
4 3
3 6 6
4 6

Sample Output 2

5
7
4
7
M - NAND

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

配点 : 4 点

問題文

0 以上 2^{30} 未満の整数 a,b に対して、a と b の NAND を次のように定めます。

  • a,b をそれぞれ、必要に応じて先頭に 0 を補って 2 進法 30 桁で表します。各桁について、両方の数のその桁が 1 ならば 0、そうでなければ 1 とすると、 2 進法 30 桁で表された整数が得られます。それを a と b の NAND とします。

0 以上 2^{30} 未満の整数からなる長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 p x:A_p を 0 以上 2^{30} 未満の整数 x に変更する。
  • 2 l r:B_l=A_l とし、i=l+1,l+2,\ldots,r の順に B_i を B_{i-1} と A_i の NAND として定める。B_r を出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \lt 2^{30}
  • 1 種類目のクエリについて、1 \leq p \leq N, 0 \leq x \lt 2^{30}
  • 2 種類目のクエリについて、1 \leq l \leq r \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 p x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

4 3
5 3 12 10
2 1 4
2 2 3
2 3 3

出力例 1

1073741821
1073741823
12

1 番目のクエリでは、以下のように答えを計算できます。

  1. B_1=A_1=5 です。
  2. B_1 (=5) と A_2 (=3) の NAND は 1073741822 なので、 B_2=1073741822 です。
  3. B_2 (=1073741822) と A_3 (=12) の NAND は 1073741811 なので、 B_3=1073741811 です。
  4. B_3 (=1073741811) と A_4 (=10) の NAND は 1073741821 なので、 B_4=1073741821 です。この値を出力します。

入力例 2

5 6
7 10 12 3 5
2 2 4
1 3 6
2 1 5
2 3 3
1 1 1073741823
2 1 2

出力例 2

1073741820
1073741819
6
1073741813

入力例 3

3 5
1073741823 0 536870912
2 1 2
2 1 3
1 2 1073741823
2 1 3
2 2 2

出力例 3

1073741823
536870911
1073741823
1073741823

Score: 4 points

Problem Statement

For integers a,b that are at least 0 and less than 2^{30}, define the NAND of a and b as follows:

  • Represent each of a,b using 30 digits in base 2, adding leading 0s if necessary. For each digit, set the resulting digit to 0 if the corresponding digits of both numbers are 1, and to 1 otherwise. This gives an integer represented by 30 digits in base 2. This integer is the NAND of a and b.

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N consisting of integers that are at least 0 and less than 2^{30}.

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

  • 1 p x: Change A_p to the integer x, which is at least 0 and less than 2^{30}.
  • 2 l r: Set B_l=A_l, and then, for i=l+1,l+2,\ldots,r in this order, define B_i as the NAND of B_{i-1} and A_i. Print B_r.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \lt 2^{30}
  • For type 1 queries, 1 \leq p \leq N, 0 \leq x \lt 2^{30}
  • For type 2 queries, 1 \leq l \leq r \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 p x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

4 3
5 3 12 10
2 1 4
2 2 3
2 3 3

Sample Output 1

1073741821
1073741823
12

For query 1, the answer can be calculated as follows:

  1. B_1=A_1=5.
  2. The NAND of B_1 (=5) and A_2 (=3) is 1073741822, so B_2=1073741822.
  3. The NAND of B_2 (=1073741822) and A_3 (=12) is 1073741811, so B_3=1073741811.
  4. The NAND of B_3 (=1073741811) and A_4 (=10) is 1073741821, so B_4=1073741821. Print this value.

Sample Input 2

5 6
7 10 12 3 5
2 2 4
1 3 6
2 1 5
2 3 3
1 1 1073741823
2 1 2

Sample Output 2

1073741820
1073741819
6
1073741813

Sample Input 3

3 5
1073741823 0 536870912
2 1 2
2 1 3
1 2 1073741823
2 1 3
2 2 2

Sample Output 3

1073741823
536870911
1073741823
1073741823
N - 九九

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

配点 : 4 点

問題文

縦 N マス、横 N マスのグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。

はじめ、マス (i,j) には整数 i \times j が書かれています。

次の操作を Q 回行います。k 回目の操作では、a_k \leq i \leq c_k かつ b_k \leq j \leq d_k を満たすすべてのマス (i,j) に書かれている整数をそれぞれ 0 で上書きします。

すべての操作を終えた後にグリッドの各マスに書かれている整数の総和を 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq a_k \leq c_k \leq N
  • 1 \leq b_k \leq d_k \leq N
  • 入力される数値はすべて整数

入力

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

N Q
a_1 b_1 c_1 d_1
a_2 b_2 c_2 d_2
\vdots
a_Q b_Q c_Q d_Q

出力

すべての操作を終えた後にグリッドの各マスに書かれている整数の総和を 998244353 で割った余りを出力せよ。


入力例 1

3 1
2 2 3 3

出力例 1

11

はじめ、グリッドの各マスに書かれている整数は次のようになっています。

1 2 3
2 4 6
3 6 9

すべての操作を終えた後の各マスに書かれている整数は次のようになっています。

1 2 3
2 0 0
3 0 0

入力例 2

3 2
1 2 2 3
2 1 3 2

出力例 2

10

入力例 3

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

出力例 3

0

Score: 4 points

Problem Statement

There is a grid with N rows and N columns. Let cell (i,j) denote the cell in the i-th row from the top and the j-th column from the left.

Initially, cell (i,j) contains the integer i \times j.

Perform the following operation Q times. In operation k, overwrite the integer in every cell (i,j) satisfying a_k \leq i \leq c_k and b_k \leq j \leq d_k with 0.

Find the sum of the integers in all cells of the grid after all operations, modulo 998244353.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq a_k \leq c_k \leq N
  • 1 \leq b_k \leq d_k \leq N
  • All numerical values in the input are integers.

Input

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

N Q
a_1 b_1 c_1 d_1
a_2 b_2 c_2 d_2
\vdots
a_Q b_Q c_Q d_Q

Output

Print the sum of the integers in all cells of the grid after all operations, modulo 998244353.


Sample Input 1

3 1
2 2 3 3

Sample Output 1

11

Initially, the integers in the cells of the grid are as follows:

1 2 3
2 4 6
3 6 9

After all operations, the integers in the cells are as follows:

1 2 3
2 0 0
3 0 0

Sample Input 2

3 2
1 2 2 3
2 1 3 2

Sample Output 2

10

Sample Input 3

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

Sample Output 3

0
O - 最小値

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

配点 : 4 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

クエリを Q 個処理してください。それぞれのクエリでは整数 l,r が与えられるので、

\[ \sum_{l \leq s \leq t \leq r} \min(A_s,A_{s+1},\ldots,A_t) \]

を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^6
  • 1 \leq l \leq r \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下の形式で与えられる。

l r

出力

Q 行出力せよ。i 行目には、i 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

3
5
11
17

1 番目のクエリでは、s=t=1 の場合だけを考えればよいため、答えは 3 です。

2 番目のクエリでは、(s,t)=(1,1),(1,2),(2,2) の場合の最小値はそれぞれ 3,1,1 であるため、答えは 5 です。


入力例 2

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

出力例 2

14
20
15

入力例 3

6 4
0 1000000 2 999999 3 4
1 6
2 5
2 2
4 6

出力例 3

2000031
2000017
1000000
1000015

Score: 4 points

Problem Statement

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

Process Q queries. In each query, you are given integers l,r. Find

\[ \sum_{l \leq s \leq t \leq r} \min(A_s,A_{s+1},\ldots,A_t) \]

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^6
  • 1 \leq l \leq r \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format:

l r

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

3
5
11
17

For query 1, only the case s=t=1 needs to be considered, so the answer is 3.

For query 2, the minimum values for (s,t)=(1,1),(1,2),(2,2) are 3,1,1, respectively, so the answer is 5.


Sample Input 2

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

Sample Output 2

14
20
15

Sample Input 3

6 4
0 1000000 2 999999 3 4
1 6
2 5
2 2
4 6

Sample Output 3

2000031
2000017
1000000
1000015
P - 線引きゲーム

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

配点 : 4 点

問題文

円周上に 2N 個の頂点が、時計回りに頂点 1, 頂点 2, \ldots, 頂点 2N の順に並んでいます。

長さ N の整数対の列 ((A_1,B_1),(A_2,B_2),\ldots,(A_N,B_N)) が与えられます。2N 個の値 A_1,\ldots,A_N,B_1,\ldots,B_N はすべて異なります。

クエリを Q 個処理してください。j 番目のクエリでは整数 l_j,r_j が与えられます。このクエリに対して、以下の条件をすべて満たすように曲線を引けるか判定してください。

  • i=l_j,l_j+1,\ldots,r_j のそれぞれについて、頂点 A_i と頂点 B_i を結ぶ曲線を 1 本引く。
  • すべての曲線は円の内部または円周上にある。
  • 異なる 2 本の曲線は交わらない。

各クエリは独立です。あるクエリで引いた曲線は他のクエリには影響しません。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq 2N
  • (A_1,\ldots,A_N,B_1,\ldots,B_N) は (1,2,\ldots,2N) の順列
  • 1 \leq l_j \leq r_j \leq N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 B_1
A_2 B_2
\vdots
A_N B_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

出力

Q 行出力せよ。

j 行目には、j 番目のクエリについて条件を満たすように曲線を引けるなら Yes を、引けないなら No を出力せよ。


入力例 1

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

出力例 1

Yes
Yes
No
No
Yes
Yes

1 番目のクエリでは、頂点 1 と 10、頂点 2 と 5、頂点 3 と 4 を結ぶ曲線を互いに交わらないように引けます。

3 番目のクエリでは、頂点 6 と 8 を結ぶ曲線と、頂点 7 と 9 を結ぶ曲線を交わらないように引くことはできません。


入力例 2

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

出力例 2

No
Yes
No
Yes

Score: 4 points

Problem Statement

There are 2N vertices on a circle, arranged clockwise in the order vertex 1, vertex 2, \ldots, vertex 2N.

You are given a sequence of N pairs of integers ((A_1,B_1),(A_2,B_2),\ldots,(A_N,B_N)). The 2N values A_1,\ldots,A_N,B_1,\ldots,B_N are all distinct.

Process Q queries. In the j-th query, you are given integers l_j,r_j. Determine whether curves can be drawn satisfying all of the following conditions:

  • For each i=l_j,l_j+1,\ldots,r_j, draw 1 curve connecting vertex A_i and vertex B_i.
  • Every curve lies inside the circle or on its circumference.
  • No 2 distinct curves intersect.

Each query is independent. Curves drawn in one query do not affect other queries.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq 2N
  • (A_1,\ldots,A_N,B_1,\ldots,B_N) is a permutation of (1,2,\ldots,2N).
  • 1 \leq l_j \leq r_j \leq N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 B_1
A_2 B_2
\vdots
A_N B_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

Output

Print Q lines.

On the j-th line, print Yes if curves satisfying the conditions can be drawn for the j-th query, and No otherwise.


Sample Input 1

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

Sample Output 1

Yes
Yes
No
No
Yes
Yes

For query 1, curves connecting vertices 1 and 10, vertices 2 and 5, and vertices 3 and 4 can be drawn without intersecting each other.

For query 3, a curve connecting vertices 6 and 8 and a curve connecting vertices 7 and 9 cannot be drawn without intersecting each other.


Sample Input 2

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

Sample Output 2

No
Yes
No
Yes
Q - 移動回数

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

配点 : 5 点

問題文

正整数 N,M,Q と、0 と 1 からなる長さ N の文字列 S と、長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

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

  • 1 i:S の i 文字目 S_i を、0 ならば 1 に、1 ならば 0 に変更する。
  • 2 x l r:次の手順で得られる金額を求める。
    • あなたは数直線上の座標 x にいて、所持金は 0 円である。
    • i=l,l+1,\ldots,r の順に、以下を行う。
      • S_i が 0 ならば負の向きを、1 ならば正の向きを向く。
      • 次の操作を A_i 回繰り返す。
        • 向いている方向へ 1 進んだ先の座標が 0 以上 M 以下ならば、そこへ進んで 1 円を得る。そうでなければ、位置と所持金を変化させない。

2 種類目の各クエリでは、ほかのクエリでの移動や所持金を引き継がず、指定された座標 x と所持金 0 円から手順を開始します。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le M \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • S は 0 と 1 からなる長さ N の文字列
  • 1 \le A_i \le 10^9
  • 1 種類目のクエリについて、1 \le i \le N
  • 2 種類目のクエリについて、0 \le x \le M, 1 \le l \le r \le N
  • 入力される数値はすべて整数

入力

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

N M Q
S
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i
2 x l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えとして、手順を終えた時点での所持金を出力せよ。


入力例 1

5 10 8
10100
4 8 3 6 2
2 5 1 5
1 2
2 5 1 5
2 0 2 4
1 5
2 10 4 5
2 3 3 3
1 1

出力例 1

19
13
16
8
3

1 番目のクエリでは、座標 5 から順に正、負、正、負、負の向きへ進もうとします。各段階を終えた時点の座標は順に 9,1,4,0,0 であり、19 円を得ます。

3 番目のクエリでは、座標 5 から順に正、正、正、負、負の向きへ進もうとします。各段階を終えた時点の座標は順に 9,10,10,4,2 であり、得られる金額は 13 円になります。


入力例 2

3 4 5
010
10 1 10
2 0 1 3
2 4 1 3
1 1
2 2 1 2
2 2 2 3

出力例 2

2
6
2
4

Score: 5 points

Problem Statement

You are given positive integers N,M,Q, a string S of length N consisting of 0 and 1, and a sequence of positive integers A=(A_1,A_2,\ldots,A_N) of length N.

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

  • 1 i: Change the i-th character S_i of S from 0 to 1, or from 1 to 0.
  • 2 x l r: Find the amount of money obtained by the following procedure:
    • You start at coordinate x on a number line with 0 yen.
    • For i=l,l+1,\ldots,r in this order, do the following:
      • Face in the negative direction if S_i is 0, and in the positive direction if it is 1.
      • Repeat the following operation A_i times:
        • If the coordinate 1 unit ahead in the direction you are facing is between 0 and M, inclusive, move there and receive 1 yen. Otherwise, keep your position and the amount of money unchanged.

For each type 2 query, start the procedure at the specified coordinate x with 0 yen, without carrying over your movement or money from other queries.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 1 \le M \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1 \le A_i \le 10^9
  • For type 1 queries, 1 \le i \le N
  • For type 2 queries, 0 \le x \le M, 1 \le l \le r \le N
  • All numerical values in the input are integers.

Input

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

N M Q
S
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i
2 x l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the amount of money you have at the end of the procedure for the j-th type 2 query.


Sample Input 1

5 10 8
10100
4 8 3 6 2
2 5 1 5
1 2
2 5 1 5
2 0 2 4
1 5
2 10 4 5
2 3 3 3
1 1

Sample Output 1

19
13
16
8
3

For query 1, starting at coordinate 5, you attempt to move in the positive, negative, positive, negative, and negative directions in this order. Your coordinates after the respective stages are 9,1,4,0,0, and you receive 19 yen.

For query 3, starting at coordinate 5, you attempt to move in the positive, positive, positive, negative, and negative directions in this order. Your coordinates after the respective stages are 9,10,10,4,2, and the amount of money obtained is 13 yen.


Sample Input 2

3 4 5
010
10 1 10
2 0 1 3
2 4 1 3
1 1
2 2 1 2
2 2 2 3

Sample Output 2

2
6
2
4
R - 連結性

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

配点 : 5 点

問題文

縦 N マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。

グリッドの各マスには数字が 1 つ書かれています。はじめ、マス (i,j) には文字列 S_i の j 文字目が書かれています。

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

  • 1 i T:j=1,2,\ldots,W について、マス (i,j) に書かれている数字を T の j 文字目に変更する。
  • 2 a b c d:マス (a,b) からマス (c,d) へ、辺で隣接するマスのうち現在のマスと同じ数字が書かれたマスへの移動を繰り返すことで到達できるか判定する。

制約

  • 2 \le N \le 10^5
  • 2 \le W \le 10
  • 1 \le Q \le 5 \times 10^4
  • S_i は長さ W の数字からなる文字列
  • 1 種類目のクエリについて、1 \le i \le N
  • 1 種類目のクエリについて、T は長さ W の数字からなる文字列
  • 2 種類目のクエリについて、1 \le a,c \le N, 1 \le b,d \le W
  • 2 種類目のクエリについて、(a,b) \ne (c,d)
  • 2 種類目のクエリが与えられる時点で、マス (a,b) とマス (c,d) には同じ数字が書かれている
  • 入力される数値はすべて整数

入力

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

N W Q
S_1
S_2
\vdots
S_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i T
2 a b c d

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを、到達できるならば Yes、到達できないならば No として出力せよ。


入力例 1

4 5 5
01010
00000
99999
01010
2 1 1 2 5
2 1 1 4 1
1 3 00000
2 1 1 4 1
2 1 2 4 2

出力例 1

Yes
No
Yes
No

はじめ、マス (1,1) からマス (2,5) へは同じ数字 0 が書かれたマスだけを通って到達できます。一方、マス (1,1) からマス (4,1) へは到達できません。

3 行目を 00000 に変更した後は、マス (1,1) からマス (4,1) へ到達できるようになります。


入力例 2

4 7 1
1112111
1212121
1211121
1222222
2 4 1 3 7

出力例 2

Yes

マス (4,1) からマス (3,7) へ、例えば次の順に移動することで到達できます。

\[ (4,1),(3,1),(2,1),(1,1),(1,2),(1,3),(2,3),(3,3),(3,4),(3,5),(2,5),(1,5),(1,6),(1,7),(2,7),(3,7) \]

Score: 5 points

Problem Statement

There is a grid with N rows and W columns. Let cell (i,j) denote the cell in the i-th row from the top and the j-th column from the left.

Each cell of the grid contains 1 digit. Initially, cell (i,j) contains the j-th character of the string S_i.

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

  • 1 i T: For j=1,2,\ldots,W, change the digit in cell (i,j) to the j-th character of T.
  • 2 a b c d: Determine whether you can reach cell (c,d) from cell (a,b) by repeatedly moving to a cell that shares an edge with the current cell and contains the same digit as the current cell.

Constraints

  • 2 \le N \le 10^5
  • 2 \le W \le 10
  • 1 \le Q \le 5 \times 10^4
  • S_i is a string of length W consisting of digits.
  • For type 1 queries, 1 \le i \le N
  • For type 1 queries, T is a string of length W consisting of digits.
  • For type 2 queries, 1 \le a,c \le N, 1 \le b,d \le W
  • For type 2 queries, (a,b) \ne (c,d)
  • When a type 2 query is given, cells (a,b) and (c,d) contain the same digit.
  • All numerical values in the input are integers.

Input

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

N W Q
S_1
S_2
\vdots
S_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i T
2 a b c d

Output

Let q be the number of type 2 queries. Print q lines. On the j-th line, print Yes if the destination can be reached in the j-th type 2 query, and No otherwise.


Sample Input 1

4 5 5
01010
00000
99999
01010
2 1 1 2 5
2 1 1 4 1
1 3 00000
2 1 1 4 1
2 1 2 4 2

Sample Output 1

Yes
No
Yes
No

Initially, you can reach cell (2,5) from cell (1,1) by passing only through cells containing the digit 0. However, you cannot reach cell (4,1) from cell (1,1).

After changing row 3 to 00000, you can reach cell (4,1) from cell (1,1).


Sample Input 2

4 7 1
1112111
1212121
1211121
1222222
2 4 1 3 7

Sample Output 2

Yes

You can reach cell (3,7) from cell (4,1) by moving in the following order, for example:

\[ (4,1),(3,1),(2,1),(1,1),(1,2),(1,3),(2,3),(3,3),(3,4),(3,5),(2,5),(1,5),(1,6),(1,7),(2,7),(3,7) \]

S - 絶対値

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

配点 : 5 点

問題文

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

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

  • 1 i x:A_i を x に変更する。
  • 2 l r:整数 m と整数列 (i_1,i_2,\ldots,i_m) を 3 \leq m かつ l \leq i_1 \lt i_2 \lt \cdots \lt i_m \leq r を満たすように選べるとき、次の値の最大値を出力する。

\[ \sum_{k=1}^{m-2} \left|A_{i_k}+2A_{i_{k+1}}+3A_{i_{k+2}}\right| \]

制約

  • 1 \leq N \leq 5 \times 10^4
  • 1 \leq Q \leq 5 \times 10^4
  • -10^9 \leq A_i \leq 10^9
  • 1 種類目のクエリについて、1 \leq i \leq N, -10^9 \leq x \leq 10^9
  • 2 種類目のクエリについて、1 \leq l \lt r \leq N, r-l \geq 2
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを出力せよ。


入力例 1

5 5
1 -2 3 -1 2
2 1 3
2 1 5
1 2 4
2 1 3
2 2 5

出力例 1

6
16
18
16

1 番目のクエリでは m=3,(i_1,i_2,i_3)=(1,2,3) とする以外に選び方はなく、求める値は 6 です。

2 番目のクエリでは m=4,(i_1,i_2,i_3,i_4)=(1,2,3,5) と選ぶと、求める値は 16 になります。これが最大値です。

3 番目のクエリによって A_2 は 4 に変更されます。


入力例 2

6 4
-3 1 -4 1 -5 9
2 1 6
1 4 -2
2 2 5
2 3 6

出力例 2

52
36
38

入力例 3

3 3
-1000000000 -1000000000 -1000000000
2 1 3
1 2 1000000000
2 1 3

出力例 3

6000000000
2000000000

Score: 5 points

Problem Statement

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

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

  • 1 i x: Change A_i to x.
  • 2 l r: Over all choices of an integer m and a sequence of integers (i_1,i_2,\ldots,i_m) satisfying 3 \leq m and l \leq i_1 \lt i_2 \lt \cdots \lt i_m \leq r, print the maximum value of the following expression:

\[ \sum_{k=1}^{m-2} \left|A_{i_k}+2A_{i_{k+1}}+3A_{i_{k+2}}\right| \]

Constraints

  • 1 \leq N \leq 5 \times 10^4
  • 1 \leq Q \leq 5 \times 10^4
  • -10^9 \leq A_i \leq 10^9
  • For type 1 queries, 1 \leq i \leq N, -10^9 \leq x \leq 10^9
  • For type 2 queries, 1 \leq l \lt r \leq N, r-l \geq 2
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. The j-th line should contain the answer to the j-th type 2 query.


Sample Input 1

5 5
1 -2 3 -1 2
2 1 3
2 1 5
1 2 4
2 1 3
2 2 5

Sample Output 1

6
16
18
16

For query 1, the only choice is m=3,(i_1,i_2,i_3)=(1,2,3), and the value sought is 6.

For query 2, choosing m=4,(i_1,i_2,i_3,i_4)=(1,2,3,5) gives a value of 16. This is the maximum.

Query 3 changes A_2 to 4.


Sample Input 2

6 4
-3 1 -4 1 -5 9
2 1 6
1 4 -2
2 2 5
2 3 6

Sample Output 2

52
36
38

Sample Input 3

3 3
-1000000000 -1000000000 -1000000000
2 1 3
1 2 1000000000
2 1 3

Sample Output 3

6000000000
2000000000
T - K-転倒数

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

配点 : 5 点

問題文

正整数 K と、長さ L の整数列 C=(C_1,C_2,\ldots,C_L) に対し、C の K-転倒数 を、以下の条件をともに満たす整数の組 (i,j) の個数として定義します。

  • 1 \le i \lt j \le L
  • C_j \lt C_i \le C_j+K

正整数 N,Q,K と、長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

クエリを Q 個処理してください。i 番目のクエリでは、正整数 m_i,x_i が与えられます。

t を 0 以上 m_i 以下の整数から選び、数列 (A_1,A_2,\ldots,A_{m_i}) の先頭から t 個の要素の直後に x_i を挿入します。すなわち、挿入後の数列は

\[ (A_1,\ldots,A_t,x_i,A_{t+1},\ldots,A_{m_i}) \]

となります。ただし、t=0 のときは先頭に、t=m_i のときは末尾に x_i を挿入するものとします。

挿入後の数列の K-転倒数としてあり得る最小値を求めてください。なお、それぞれのクエリは独立です。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • 1 \le K \le N
  • 1 \le A_i \le N
  • 1 \le m_i \le N
  • 1 \le x_i \le N
  • 入力される数値はすべて整数

入力

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

N Q K
A_1 A_2 \ldots A_N
m_1 x_1
m_2 x_2
\vdots
m_Q x_Q

出力

Q 行出力せよ。i 行目には、i 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

4
1
0

1 番目のクエリについて、t=0 として 2 を挿入すると、数列は (2,3,1,4,2) となり、その 2-転倒数は 4 です。2-転倒数を 3 以下にする挿入位置は存在しないため、答えは 4 です。


入力例 2

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

出力例 2

3
2
0
1
0

Score: 5 points

Problem Statement

For a positive integer K and a sequence of integers C=(C_1,C_2,\ldots,C_L) of length L, define the K-inversion count of C as the number of pairs of integers (i,j) satisfying both of the following conditions:

  • 1 \le i \lt j \le L
  • C_j \lt C_i \le C_j+K

You are given positive integers N,Q,K and a sequence of nonnegative integers A=(A_1,A_2,\ldots,A_N) of length N.

Process Q queries. In the i-th query, you are given positive integers m_i,x_i.

Choose an integer t between 0 and m_i, inclusive, and insert x_i immediately after the first t elements of the sequence (A_1,A_2,\ldots,A_{m_i}). That is, the resulting sequence is

\[ (A_1,\ldots,A_t,x_i,A_{t+1},\ldots,A_{m_i}) \]

Here, x_i is inserted at the beginning if t=0, and at the end if t=m_i.

Find the minimum possible K-inversion count of the resulting sequence. Each query is independent.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • 1 \le K \le N
  • 1 \le A_i \le N
  • 1 \le m_i \le N
  • 1 \le x_i \le N
  • All numerical values in the input are integers.

Input

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

N Q K
A_1 A_2 \ldots A_N
m_1 x_1
m_2 x_2
\vdots
m_Q x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

4
1
0

For query 1, inserting 2 with t=0 gives the sequence (2,3,1,4,2), whose 2-inversion count is 4. There is no insertion position that makes the 2-inversion count at most 3, so the answer is 4.


Sample Input 2

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

Sample Output 2

3
2
0
1
0
U - 2 番目

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

配点 : 5 点

問題文

長さ M の数列 B=(B_1,B_2,\ldots,B_M) を昇順に並べ替えて得られる数列を C=(C_1,C_2,\ldots,C_M) とします。M\geq 4 であるとき、B のスコアを C_{M-1}-C_2 と定めます。

長さ N (4\leq N)の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。A を、それぞれの長さが 4 以上である 1 個以上の連続部分列に分割します。

このとき、分割して得られたすべての連続部分列のスコアの総和としてあり得る最大値を求めてください。

1 つの入力ファイルにつき、T 個のテストケースを解いてください。

制約

  • 1 \leq T \leq 10^5
  • 4 \leq N \leq 4 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 つの入力ファイルに含まれる N の総和は 4 \times 10^5 以下
  • 入力される数値はすべて整数

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N
A_1 A_2 \ldots A_N

出力

T 行出力せよ。i 行目には、i 番目のテストケースに対する答えを出力せよ。


入力例 1

3
4
1 2 10 20
8
0 0 1 1 0 0 1 1
5
5 5 5 5 5

出力例 1

8
2
0

1 番目のテストケースでは、数列全体を 1 個の連続部分列とします。 これを昇順に並べ替えると (1,2,10,20) となるため、スコアは 10-2=8 です。これが最大値です。

2 番目のテストケースでは、A を (0,0,1,1) と (0,0,1,1) に分割すると、 スコアの総和は 2 となります。これが最大値です。


入力例 2

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

出力例 2

10

入力例 3

2
9
1000000000 0 7 6 5 4 3 2 1
11
4 9 1 8 2 7 3 6 0 5 10

出力例 3

6
10

Score: 5 points

Problem Statement

For a sequence B=(B_1,B_2,\ldots,B_M) of length M, let C=(C_1,C_2,\ldots,C_M) be the sequence obtained by sorting it in ascending order. When M\geq 4, define the score of B as C_{M-1}-C_2.

You are given a sequence of nonnegative integers A=(A_1,A_2,\ldots,A_N) of length N (4\leq N). Partition A into 1 or more contiguous subsequences, each of length at least 4.

Find the maximum possible sum of the scores of all contiguous subsequences obtained by the partition.

Each input file contains T test cases. Solve all of them.

Constraints

  • 1 \leq T \leq 10^5
  • 4 \leq N \leq 4 \times 10^5
  • 0 \leq A_i \leq 10^9
  • The sum of N over all test cases in a single input file is at most 4 \times 10^5.
  • All numerical values in the input are integers.

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Print T lines. The i-th line should contain the answer to the i-th test case.


Sample Input 1

3
4
1 2 10 20
8
0 0 1 1 0 0 1 1
5
5 5 5 5 5

Sample Output 1

8
2
0

For test case 1, take the entire sequence as 1 contiguous subsequence. Sorting it in ascending order gives (1,2,10,20), so its score is 10-2=8. This is the maximum.

For test case 2, partitioning A into (0,0,1,1) and (0,0,1,1) gives a total score of 2. This is the maximum.


Sample Input 2

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

Sample Output 2

10

Sample Input 3

2
9
1000000000 0 7 6 5 4 3 2 1
11
4 9 1 8 2 7 3 6 0 5 10

Sample Output 3

6
10
V - 連結性 2

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

配点 : 6 点

問題文

頂点に 1 から N の番号が付いた N 頂点の木があります。 i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。

整数の区間 [a,b] が good であるとは、頂点 a,a+1,\ldots,b からなる誘導部分グラフが 連結であることをいいます。

クエリを Q 個処理してください。i 番目のクエリでは整数 L_i,R_i が与えられるので、 次の条件をともに満たす整数の組 (a,b) の個数を求めてください。

  • L_i \leq a \leq b \leq R_i
  • 区間 [a,b] は good である

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq u_i \lt v_i \leq N
  • 与えられるグラフは木
  • 1 \leq L_i \leq R_i \leq N
  • 入力される数値はすべて整数

入力

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

N Q
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

出力

Q 行出力せよ。i 行目には、i 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

9
7
3
1

good な区間は以下の 9 個です。

[1,1], [1,5], [2,2], [2,5], [3,3], [3,5], [4,4], [4,5], [5,5]


入力例 2

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

出力例 2

17
3
9

Score: 6 points

Problem Statement

There is a tree with N vertices numbered from 1 to N. Edge i connects vertices u_i and v_i.

An integer interval [a,b] is good if the subgraph induced by vertices a,a+1,\ldots,b is connected.

Process Q queries. In the i-th query, you are given integers L_i,R_i. Find the number of pairs of integers (a,b) satisfying both of the following conditions:

  • L_i \leq a \leq b \leq R_i
  • The interval [a,b] is good.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq u_i \lt v_i \leq N
  • The given graph is a tree.
  • 1 \leq L_i \leq R_i \leq N
  • All numerical values in the input are integers.

Input

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

N Q
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

9
7
3
1

The following 9 intervals are good:

[1,1], [1,5], [2,2], [2,5], [3,3], [3,5], [4,4], [4,5], [5,5]


Sample Input 2

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

Sample Output 2

17
3
9
W - 隣接加算

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

配点 : 6 点

問題文

長さ k の整数列 B=(B_1,B_2,\ldots,B_k) に対して、次の操作を 0 回以上行うことを考えます。

  • 1 \le i \le k-1 を満たす整数 i と、整数 x を選ぶ(x は負でもよい)。
  • B_i に x を、B_{i+1} に 2x をそれぞれ加える。

操作によって B のすべての要素を等しくできるとき、B を よい数列 と呼びます。

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。 クエリを Q 個処理してください。クエリは以下の 2 種類のいずれかです。

  • 1 i x:A_i を x に変更する。
  • 2 l r:数列 (A_l,A_{l+1},\ldots,A_r) がよい数列であるか判定する。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 種類目のクエリについて、1 \le i \le N, 0 \le x \le 10^9
  • 2 種類目のクエリについて、1 \le l \le r \le N
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 i x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。j 行目には、j 番目の 2 種類目のクエリに対する答えを、数列 (A_l,A_{l+1},\ldots,A_r) がよい数列ならば Yes、そうでなければ No として出力せよ。


入力例 1

5 7
1 2 5 4 0
2 1 1
2 1 2
2 1 3
1 3 3
2 1 3
2 2 5
2 1 5

出力例 1

Yes
Yes
No
Yes
No
No

最初の 2 種類目のクエリで得られる数列は (1) です。操作を行わなくてもすべての要素が等しいため、これはよい数列です。

2 番目の 2 種類目のクエリで得られる数列は (1,2) です。i=1,x=-1 として操作を行うと (0,0) になるため、これはよい数列です。


入力例 2

6 6
1000000000 0 999999999 1 500000000 42
2 1 6
2 2 4
1 4 1000000000
2 2 4
2 4 6
2 3 3

出力例 2

No
No
No
Yes
Yes

Score: 6 points

Problem Statement

For a sequence of integers B=(B_1,B_2,\ldots,B_k) of length k, consider performing the following operation 0 or more times:

  • Choose an integer i satisfying 1 \le i \le k-1 and an integer x (x may be negative).
  • Add x to B_i and 2x to B_{i+1}.

We call B a good sequence if all elements of B can be made equal by these operations.

You are given a sequence of nonnegative integers A=(A_1,A_2,\ldots,A_N) of length N. Process Q queries. Each query is of one of the following 2 types:

  • 1 i x: Change A_i to x.
  • 2 l r: Determine whether the sequence (A_l,A_{l+1},\ldots,A_r) is a good sequence.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • For type 1 queries, 1 \le i \le N, 0 \le x \le 10^9
  • For type 2 queries, 1 \le l \le r \le N
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 i x
2 l r

Output

Let q be the number of type 2 queries. Print q lines. On the j-th line, print Yes if the sequence (A_l,A_{l+1},\ldots,A_r) in the j-th type 2 query is a good sequence, and No otherwise.


Sample Input 1

5 7
1 2 5 4 0
2 1 1
2 1 2
2 1 3
1 3 3
2 1 3
2 2 5
2 1 5

Sample Output 1

Yes
Yes
No
Yes
No
No

The sequence obtained in the first type 2 query is (1). All its elements are already equal without any operations, so it is a good sequence.

The sequence obtained in the 2-nd type 2 query is (1,2). Performing an operation with i=1,x=-1 gives (0,0), so it is a good sequence.


Sample Input 2

6 6
1000000000 0 999999999 1 500000000 42
2 1 6
2 2 4
1 4 1000000000
2 2 4
2 4 6
2 3 3

Sample Output 2

No
No
No
Yes
Yes
X - 不揃いさ

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

配点 : 6 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N)、g=(g_1,g_2,\ldots,g_N) と、長さ N-1 の整数列 d=(d_1,d_2,\ldots,d_{N-1}) が与えられます。ここで g_j は 1 \leq g_j \leq 2 を満たす点に注意してください。

数列 g,d を用いて、長さ N の整数列 B=(B_1,B_2,\ldots,B_N) に対する f(B) を次のように定義します。

長さ N の整数列 C=(C_1,C_2,\ldots,C_N) を自由に選ぶことができます。このとき、B と C のコストを

\[ \sum_{j=1}^{N} g_j |B_j-C_j|+\sum_{j=1}^{N-1} d_j |C_j-C_{j+1}| \]

とします。f(B) は、C としてあり得るすべての整数列を考えたときの、コストの最小値です。

次の Q 個のクエリを順に処理してください。

k 番目のクエリでは整数 i_k,x_k が与えられるので、A_{i_k} を x_k に変更して、f(A) を計算してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_j \leq 10^9
  • 1 \leq g_j \leq 2
  • 1 \leq d_j \leq 10^9
  • 1 \leq i_k \leq N
  • 0 \leq x_k \leq 10^9
  • 入力される数値はすべて整数

入力

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

N Q
A_1 A_2 \ldots A_N
g_1 g_2 \ldots g_N
d_1 d_2 \ldots d_{N-1}
i_1 x_1
i_2 x_2
\vdots
i_Q x_Q

出力

Q 行出力せよ。k 行目には、k 番目のクエリで計算した f(A) を出力せよ。


入力例 1

3 3
1 5 2
1 2 1
3 1
2 3
1 6
3 6

出力例 1

3
4
6

1 番目のクエリの後、A=(1,3,2) です。例えば C=(2,2,2) とするとコストは 3 になります。コストを 3 より小さくすることはできないため、f(A)=3 です。


入力例 2

2 4
0 10
2 2
1
1 10
2 0
1 0
2 10

出力例 2

0
10
0
10

A のすべての要素が等しいとき、C=A とすることでコストを 0 にできます。


入力例 3

5 5
8 1 7 3 9
2 1 2 1 2
2 1000000000 1 4
3 0
5 2
1 1
4 9
2 6

出力例 3

30
18
4
10
15

Score: 6 points

Problem Statement

You are given sequences of nonnegative integers A=(A_1,A_2,\ldots,A_N) and g=(g_1,g_2,\ldots,g_N) of length N, and a sequence of integers d=(d_1,d_2,\ldots,d_{N-1}) of length N-1. Note that g_j satisfies 1 \leq g_j \leq 2.

Using the sequences g,d, define f(B) for a sequence of integers B=(B_1,B_2,\ldots,B_N) of length N as follows.

You may freely choose a sequence of integers C=(C_1,C_2,\ldots,C_N) of length N. Define the cost of B and C as

\[ \sum_{j=1}^{N} g_j |B_j-C_j|+\sum_{j=1}^{N-1} d_j |C_j-C_{j+1}| \]

Then, f(B) is the minimum cost over all possible choices of the integer sequence C.

Process the following Q queries in order.

In query k, you are given integers i_k,x_k. Change A_{i_k} to x_k and calculate f(A).

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq A_j \leq 10^9
  • 1 \leq g_j \leq 2
  • 1 \leq d_j \leq 10^9
  • 1 \leq i_k \leq N
  • 0 \leq x_k \leq 10^9
  • All numerical values in the input are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
g_1 g_2 \ldots g_N
d_1 d_2 \ldots d_{N-1}
i_1 x_1
i_2 x_2
\vdots
i_Q x_Q

Output

Print Q lines. The k-th line should contain f(A) calculated in query k.


Sample Input 1

3 3
1 5 2
1 2 1
3 1
2 3
1 6
3 6

Sample Output 1

3
4
6

After query 1, A=(1,3,2). For example, choosing C=(2,2,2) gives a cost of 3. The cost cannot be made less than 3, so f(A)=3.


Sample Input 2

2 4
0 10
2 2
1
1 10
2 0
1 0
2 10

Sample Output 2

0
10
0
10

When all elements of A are equal, choosing C=A gives a cost of 0.


Sample Input 3

5 5
8 1 7 3 9
2 1 2 1 2
2 1000000000 1 4
3 0
5 2
1 1
4 9
2 6

Sample Output 3

30
18
4
10
15