A - Complement Interval Graph

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

整数 l, r に対し、l 以上 r 以下の整数全体からなる集合を [l, r] で表します。 すなわち、[l, r] = \lbrace l, l+1, l+2, \ldots, r-1, r\rbrace です。

N 組の整数のペア (L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N) が与えられます。 これに対して、下記で定義される無向グラフ G を考えます。

  • 1, 2, \ldots, N と番号付けられた N 個の頂点を持つ。
  • すべての i, j \in [1, N] について次が成り立つ:[L_i, R_i][L_j, R_j] の共通部分が空であるとき、かつ、そのときに限り、頂点 i と頂点 j の間に無向辺が張られている。

また、i = 1, 2, \ldots, N について、頂点 i の重みを W_i で定めます。

G に関する Q 個のクエリが与えられるので、それらを与えられる順に処理してください。 i = 1, 2, \ldots, Q について、i 番目のクエリは下記の通りです。

s_i \neq t_i を満たす 1 以上 N 以下の整数 s_i, t_i が与えられる。 G において、頂点 s_i から頂点 t_i へのパスが存在するかを判定せよ。 存在する場合は、そのようなパスの重みの最小値を出力せよ。

ここで、頂点 s から頂点 t へのパスの重みは、パス上の頂点(両端点である頂点 s と頂点 t を含む)の重みの総和で定義されます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq 2N
  • 1 \leq s_i, t_i \leq N
  • s_i \neq t_i
  • 入力はすべて整数

入力

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

N
W_1 W_2 \cdots W_N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
s_1 t_1
s_2 t_2
\vdots
s_Q t_Q

出力

Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には、 頂点 s_i から頂点 t_i へのパスが存在する場合はそのようなパスの重みの最小値を出力し、存在しない場合は -1 を出力せよ。


入力例 1

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

出力例 1

11
6
-1

G\lbrace 1, 3\rbrace, \lbrace 2, 3\rbrace, \lbrace 2, 4\rbrace, \lbrace 3, 4\rbrace4 本の無向辺を持つグラフです。

  • 1 番目のクエリに関して、頂点 1 から頂点 4 への 1 \to 3 \to 4 というパスが存在します。そのパスの重みは W_1 + W_3 + W_4 = 5 + 4 + 2 = 11 であり、これが考えられる最小値です。
  • 2 番目のクエリに関して、頂点 4 から頂点 3 への 4 \to 3 というパスが存在します。そのパスの重みは W_4 + W_3 = 2 + 4 = 6 であり、これが考えられる最小値です。
  • 3 番目のクエリに関して、頂点 5 から頂点 2 へのパスは存在しません。したがって、-1 を出力します。

入力例 2

8
44 75 49 4 78 79 12 32
5 13
10 16
6 8
6 15
12 15
5 7
1 15
1 2
5
5 6
3 2
7 5
4 5
5 4

出力例 2

157
124
-1
114
114

Score : 700 points

Problem Statement

For integers l, r, let [l, r] denote the set of all integers from l through r. That is, [l, r] = \lbrace l, l+1, l+2, \ldots, r-1, r\rbrace.

You are given N pairs of integers (L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N). Based on these pairs, consider an undirected graph G defined as follows:

  • It has N vertices numbered 1, 2, \ldots, N.
  • For all i, j \in [1, N], there is an undirected edge between vertices i and j if and only if the intersection of [L_i, R_i] and [L_j, R_j] is empty.

In addition, for each i = 1, 2, \ldots, N, define the weight of vertex i to be W_i.

You are given Q queries about G. Process these queries in the order they are given. For each i = 1, 2, \ldots, Q, the i-th query is the following:

You are given integers s_i and t_i (both between 1 and N, inclusive) such that s_i \neq t_i. Determine whether there exists a path from vertex s_i to vertex t_i in G. If it exists, print the minimum possible weight of such a path.

Here, the weight of a path from vertex s to vertex t is defined as the sum of the weights of the vertices on that path (including both endpoints s and t).

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq 2N
  • 1 \leq s_i, t_i \leq N
  • s_i \neq t_i
  • All input values are integers.

Input

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

N
W_1 W_2 \cdots W_N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
s_1 t_1
s_2 t_2
\vdots
s_Q t_Q

Output

Print Q lines. For each i = 1, 2, \ldots, Q, on the i-th line, if there exists a path from vertex s_i to vertex t_i, print the minimum possible weight of such a path, and print -1 otherwise.


Sample Input 1

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

Sample Output 1

11
6
-1

G is a graph with four undirected edges: \lbrace 1, 3\rbrace, \lbrace 2, 3\rbrace, \lbrace 2, 4\rbrace, \lbrace 3, 4\rbrace.

  • For the first query, there is a path from vertex 1 to vertex 4 given by 1 \to 3 \to 4. The weight of this path is W_1 + W_3 + W_4 = 5 + 4 + 2 = 11, and this is the minimum possible.
  • For the second query, there is a path from vertex 4 to vertex 3 given by 4 \to 3. The weight of this path is W_4 + W_3 = 2 + 4 = 6, and this is the minimum possible.
  • For the third query, there is no path from vertex 5 to vertex 2. Hence, print -1.

Sample Input 2

8
44 75 49 4 78 79 12 32
5 13
10 16
6 8
6 15
12 15
5 7
1 15
1 2
5
5 6
3 2
7 5
4 5
5 4

Sample Output 2

157
124
-1
114
114
B - Broken Wheel

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 800

問題文

正整数 N と、01 のみからなる長さ N の文字列 s_0s_1\ldots s_{N-1} が与えられます。

0, 1, 2, \ldots, N と番号づけられた (N+1) 個の頂点と、下記の辺からなる単純無向グラフ G を考えます。

  • i = 0, 1, \ldots, N-1 について、頂点 i と頂点 (i+1)\bmod N を結ぶ無向辺がある。
  • i = 0, 1, \ldots, N-1 について、s_i = 1 のとき、かつ、そのときに限り、頂点 i と頂点 N を結ぶ無向辺がある。
  • 上記の他に辺はない。

さらに、G の各辺を任意に向きづけして有向グラフ G' を作ります。すなわち、G の各無向辺 \lbrace u, v \rbrace を、 u から v への有向辺 (u, v)v から u への有向辺 (v, u) のどちらか一方で置き換えます。

i = 0, 1, \ldots, N について、G' における頂点 i の入次数を d_i とおくとき、 得られる数列 (d_0, d_1, \ldots, d_N) としてありえるものの個数を 998244353 で割った余りを出力してください。

制約

  • 3 \leq N \leq 10^6
  • N は整数
  • s_i0 または 1

入力

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

N
s_0s_1\ldots s_{N-1}

出力

答えを出力せよ。


入力例 1

3
010

出力例 1

14

G\lbrace 0, 1 \rbrace, \lbrace 0, 2 \rbrace, \lbrace 1, 2 \rbrace, \lbrace 1, 3 \rbrace4 本の無向辺を持ちます。 例えば、それぞれの辺を 0 \to 12 \to 02 \to 11 \to 3 と向きづけた場合、(d_0, d_1, d_2, d_3) = (1, 2, 0, 1) が得られます。

(d_0, d_1, d_2, d_3) として得られるものは、 (0, 1, 2, 1), (0, 2, 1, 1), (0, 2, 2, 0), (0, 3, 1, 0), (1, 0, 2, 1), (1, 1, 1, 1), (1, 1, 2, 0), (1, 2, 0, 1), (1, 2, 1, 0), (1, 3, 0, 0), (2, 0, 1, 1), (2, 1, 0, 1), (2, 1, 1, 0), (2, 2, 0, 0)14 個です。


入力例 2

20
00001100111010100101

出力例 2

261339902

Score : 800 points

Problem Statement

You are given a positive integer N and a length-N string s_0s_1\ldots s_{N-1} consisting only of 0 and 1.

Consider a simple undirected graph G with (N+1) vertices numbered 0, 1, 2, \ldots, N, and the following edges:

  • For each i = 0, 1, \ldots, N-1, there is an undirected edge between vertices i and (i+1)\bmod N.
  • For each i = 0, 1, \ldots, N-1, there is an undirected edge between vertices i and N if and only if s_i = 1.
  • There are no other edges.

Furthermore, create a directed graph G' by assigning a direction to each edge of G. That is, for each undirected edge \lbrace u, v \rbrace in G, replace it with either a directed edge (u, v) from u to v or a directed edge (v, u) from v to u.

For each i = 0, 1, \ldots, N, let d_i be the in-degree of vertex i in G'. Print the number, modulo 998244353, of distinct sequences (d_0, d_1, \ldots, d_N) that can be obtained.

Constraints

  • 3 \leq N \leq 10^6
  • N is an integer.
  • Each s_i is 0 or 1.

Input

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

N
s_0s_1\ldots s_{N-1}

Output

Print the answer.


Sample Input 1

3
010

Sample Output 1

14

G has four undirected edges: \lbrace 0, 1 \rbrace, \lbrace 0, 2 \rbrace, \lbrace 1, 2 \rbrace, \lbrace 1, 3 \rbrace. For example, if we assign directions to each edge as 0 \to 1, 2 \to 0, 2 \to 1, 1 \to 3, then (d_0, d_1, d_2, d_3) = (1, 2, 0, 1) is obtained.

The possible sequences (d_0, d_1, d_2, d_3) are (0, 1, 2, 1), (0, 2, 1, 1), (0, 2, 2, 0), (0, 3, 1, 0), (1, 0, 2, 1), (1, 1, 1, 1), (1, 1, 2, 0), (1, 2, 0, 1), (1, 2, 1, 0), (1, 3, 0, 0), (2, 0, 1, 1), (2, 1, 0, 1), (2, 1, 1, 0), (2, 2, 0, 0), for a total of 14.


Sample Input 2

20
00001100111010100101

Sample Output 2

261339902
C - Grid Coloring 3

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

HW 列のグリッドがあり、はじめすべてのマスは無色です。

このグリッドに対して、下記の手順を好きな回数だけ繰り返します。

  • まず、1 以上 C 以下の整数 i と、マス 1 個を選ぶ。
  • その後、選んだマスおよび、選んだマスと同じ行にあるすべてのマス、選んだマスと同じ列にあるすべてのマスの、合計 (H+W-1) マスを色 i で塗る(すでに色が塗られている場合は、その色を色 i で上書きする)。

上記の手順を繰り返して得られる、すべてのマスに色が塗られている グリッドとしてありえるものの個数を 998244353 で割った余りを出力してください。

制約

  • 1 \leq H, W \leq 400
  • 1 \leq C \leq 10^9
  • 入力はすべて整数

入力

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

H W C

出力

答えを出力せよ。


入力例 1

2 3 2

出力例 1

26

以下、上から i 行目、左から j 行目のマスを (i, j) で表します。 また、無色のマス、色 1 で塗られたマス、色 2 で塗られたマスをそれぞれ .12 で表します。 入力例1に対応する初期状態のグリッドから、まず色 2 とマス (2, 2) を選択して操作を行うと、グリッドの状態は

.2.
222

となります。 続けて、色 1 とマス (1, 1) を選択して操作を行うと、グリッドの状態は

111
122

となります。このグリッドはすべてのマスに色が塗られているため、 問題文中の「上記の手順を繰り返して得られる、すべてのマスに色が塗られているグリッド」に該当します。 さらに、色 1 とマス (1, 3) を選択して操作を行うと、グリッドの状態は

111
121

となります。このグリッドも問題文中の「上記の手順を繰り返して得られる、すべてのマスに色が塗られているグリッド」に該当します。


入力例 2

3 2 1

出力例 2

1

入力例 3

229 327 763027379

出力例 3

547014653

Score : 1000 points

Problem Statement

There is a grid of H rows and W columns. Initially, all cells are uncolored.

You can repeat the following procedure any number of times:

  • Choose an integer i between 1 and C, inclusive, and choose exactly one cell in the grid.
  • Then, color that chosen cell, as well as all cells in the same row as the chosen cell and all cells in the same column as the chosen cell (a total of (H+W-1) cells), with color i. (If any cell is already colored, its color is overwritten with color i.)

Print the number, modulo 998244353, of different grids in which every cell is colored that can be obtained by repeating the procedure above.

Constraints

  • 1 \leq H, W \leq 400
  • 1 \leq C \leq 10^9
  • All input values are integers.

Input

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

H W C

Output

Print the answer.


Sample Input 1

2 3 2

Sample Output 1

26

Below, let (i, j) denote the cell at the i-th row from the top and the j-th column from the left. Also, let ., 1, 2 denote an uncolored cell, a cell colored with color 1, a cell colored with color 2, respectively.

From the initial grid in Sample Input 1, if you first choose color 2 and cell (2, 2), the grid becomes:

.2.
222

Then, if you choose color 1 and cell (1, 1), the grid becomes:

111
122

All cells are colored in this grid, so it satisfies the condition in the problem statement. Furthermore, if you then choose color 1 and cell (1, 3), the grid becomes:

111
121

All cells are again colored in this grid, satisfying the condition in the problem statement.


Sample Input 2

3 2 1

Sample Output 2

1

Sample Input 3

229 327 763027379

Sample Output 3

547014653
D - Magnets

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

01 のみからなる 2 つの長さ N の文字列 A = A_1A_2 \ldots A_NB = B_1B_2 \ldots B_N が与えられます。

左右に一列に並んだ N 個のマスがあります。 i = 1, 2, \ldots, N について、左から i 番目のマスはマス i と呼ばれ、 はじめマス i には、A_i = 1 ならばコマが 1 個置かれており、A_i = 0 ならばコマは置かれていません。

下記の操作を好きな回数( 0 回でも良い)だけ繰り返します。

  • まず、1 以上 N 以下の整数 i を選ぶ。
  • そして、すべてのコマを同時に、マス i に近づく方向に 1 マス動かす。すなわち、各コマを次が成り立つように動かす: そのコマの移動前の位置をマス j 、移動後の位置をマス j' とすると、
    • i \lt j ならば j' = j-1
    • i \gt j ならば j' = j+1
    • i = j ならば j' = j

上記の操作の繰り返しによって、下記の条件を満たす状態にすることが可能かを判定し、可能な場合はそれまでに操作を行う回数としてありえる最小値を求めてください。

すべての i = 1, 2, \ldots, N について次が成り立つ: B_i = 1 であるとき、かつ、そのときに限り、マス i にコマが 1 個以上存在する。

T 個の独立なテストケースが与えられるので、それぞれについて答えを出力してください。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 10^6
  • T, N は整数
  • A, B はそれぞれ 01 のみからなる長さ N の文字列
  • A_i = 1 である i が存在する
  • B_i = 1 である i が存在する
  • すべてのテストケースにわたる N の総和は 10^6 以下

入力

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

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

ここで、i = 1, 2, \ldots, T について、\mathrm{case}_ii 番目のテストケースを表す。

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

N
A
B

出力

T 行出力せよ。 i = 1, 2, \ldots, T について、i 行目には i 番目のテストケースに対する答えとして、 問題文中の条件を満たす状態にすることが不可能な場合は -1 を、 可能な場合はそれまでに操作を行う回数としてありえる最小値を出力せよ。


入力例 1

3
8
01001101
00001011
3
010
111
20
10100011011110101011
00010001111101100000

出力例 1

3
-1
5

入力は 3 個の独立なテストケースからなります。

1 番目のテストケースでは、 初期状態において、コマの配置、すなわち各マスに置かれているコマの個数を並べた列は (0, 1, 0, 0, 1, 1, 0, 1) です。 下記の通りに 3 回の操作を行うことで、問題文中の条件を満たす状態にすることができます。

  • 1 回目の操作として、i = 5 として操作を行う。その結果、コマの配置は (0, 0, 1, 0, 2, 0, 1, 0) となる。
  • 2 回目の操作として、i = 8 として操作を行う。その結果、コマの配置は (0, 0, 0, 1, 0, 2, 0, 1) となる。
  • 3 回目の操作として、i = 8 として操作を行う。その結果、コマの配置は (0, 0, 0, 0, 1, 0, 2, 1) となる。

一方、3 回未満の操作で問題文中の条件を満たす状態にすることはできないため、1 番目のテストケースに対する答えとして、3 を出力します。

2 番目のテストケースでは、 どのような手順で操作を行っても、問題文中の条件を満たすことはできません。 よって、2 番目のテストケースに対する答えとして、-1 を出力します。

Score : 1000 points

Problem Statement

You are given two length-N strings A = A_1A_2 \ldots A_N and B = B_1B_2 \ldots B_N, each consisting of 0 and 1.

There are N squares aligned in a row from left to right. For i = 1, 2, \ldots, N, the i-th square from the left is called square i. Initially, square i contains a piece if A_i = 1, and no piece if A_i = 0.

You may repeat the following operation any number of times (possibly zero):

  • Choose an integer i between 1 and N, inclusive.
  • Move all pieces simultaneously one square closer to square i. That is, for each piece, let square j be its current position and square j' be its new position, and the following holds:
    • if i < j, then j' = j-1;
    • if i > j, then j' = j+1;
    • if i = j, then j' = j.

Determine whether it is possible to reach a configuration satisfying the following condition, and if it is possible, find the minimum number of operations needed to do so:

For every i = 1, 2, \ldots, N, there is at least one piece in square i if and only if B_i = 1.

You are given T independent test cases. Print the answer for each of them.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 10^6
  • T and N are integers.
  • A and B are strings of length N, each consisting of 0 and 1.
  • There exists i such that A_i = 1.
  • There exists i such that B_i = 1.
  • The sum of N over all test cases is at most 10^6.

Input

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

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

Here, \mathrm{case}_i (i=1,2,\ldots,T) denotes the i-th test case.

Each test case is given in the following format:

N
A
B

Output

Print T lines. For each i = 1, 2, \ldots, T, on the i-th line, print -1 if it is impossible to reach a configuration satisfying the condition for the i-th test case. Otherwise, print the minimum number of operations needed.


Sample Input 1

3
8
01001101
00001011
3
010
111
20
10100011011110101011
00010001111101100000

Sample Output 1

3
-1
5

The input has three independent test cases.

In the first test case, initially, the sequence of the numbers of pieces in the squares is (0, 1, 0, 0, 1, 1, 0, 1). By performing the operation three times as follows, you can satisfy the condition:

  • Choose i = 5. After the operation, the configuration is (0, 0, 1, 0, 2, 0, 1, 0).
  • Choose i = 8. After the operation, the configuration is (0, 0, 0, 1, 0, 2, 0, 1).
  • Choose i = 8. After the operation, the configuration is (0, 0, 0, 0, 1, 0, 2, 1).

It is impossible to satisfy the condition in fewer than three operations, so the answer is 3.

In the second test case, no matter how you perform the operations, you cannot satisfy the condition, so the answer is -1.