Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は花壇の管理をしています。今日の仕事は、水道から汲んできた水をそれぞれの植木鉢に注ぐことです。
花壇には N 個の植木鉢が一列に並んでおり、i 番目の植木鉢には最大 C_i リットルの水が入ります。現在、i 番目の植木鉢にはすでに S_i リットルの水が入っています。
高橋君は水道から新たに M リットルの水を汲み、バケツに入れて持ち歩きます。植木鉢 1, 2, \ldots, N の順に、次の操作を行います。
- 植木鉢 i について、その植木鉢の空き容量 C_i - S_i リットルと、現在バケツに残っている水の量のうち、小さい方 に等しい量の水を植木鉢 i に注ぐ。注いだ分だけバケツの水は減り、植木鉢 i に入っている水の量はその分だけ増える。
すべての植木鉢について操作を終えた後、バケツに水が残っていればその水は捨てます。
すべての操作が終わった後の、各植木鉢に入っている水の量を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 10^{18}
- 1 \leq C_i \leq 10^9
- 0 \leq S_i \leq C_i
- 入力はすべて整数である。
入力
N M C_1 S_1 C_2 S_2 \vdots C_N S_N
- 1 行目には、植木鉢の個数を表す整数 N と、新たに用意した水の量を表す整数 M が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各植木鉢の情報が与えられる。
- 1 + i 行目には、i 番目の植木鉢の最大容量 C_i と、現在入っている水の量 S_i が、スペース区切りで与えられる。
出力
N 行出力してください。i 行目には、すべての操作が終わった後に i 番目の植木鉢に入っている水の量を出力してください。
入力例 1
3 8 10 3 8 5 6 2
出力例 1
10 6 2
入力例 2
5 100 10 5 20 20 15 3 8 0 12 7
出力例 2
10 20 15 8 12
入力例 3
8 1000000000000 1000000000 0 500000000 200000000 1000000000 1000000000 300000000 100000000 700000000 0 400000000 400000000 600000000 150000000 200000000 50000000
出力例 3
1000000000 500000000 1000000000 300000000 700000000 400000000 600000000 200000000
Score : 266 pts
Problem Statement
Takahashi is managing a flower bed. Today's task is to pour water fetched from the faucet into each flower pot.
There are N flower pots arranged in a row in the flower bed, and the i-th flower pot can hold a maximum of C_i liters of water. Currently, the i-th flower pot already contains S_i liters of water.
Takahashi fetches M liters of water from the faucet and carries it in a bucket. He visits the flower pots in the order 1, 2, \ldots, N, performing the following operation for each:
- For flower pot i, pour into flower pot i an amount of water equal to the smaller of the remaining capacity of the pot C_i - S_i liters and the amount of water currently remaining in the bucket. The water in the bucket decreases by the amount poured, and the amount of water in flower pot i increases by that same amount.
After finishing the operation for all flower pots, if there is any water remaining in the bucket, it is discarded.
Determine the amount of water in each flower pot after all operations are completed.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 10^{18}
- 1 \leq C_i \leq 10^9
- 0 \leq S_i \leq C_i
- All input values are integers.
Input
N M C_1 S_1 C_2 S_2 \vdots C_N S_N
- The first line contains an integer N representing the number of flower pots and an integer M representing the amount of newly prepared water, separated by a space.
- From the 2nd line to the (N + 1)-th line, information about each flower pot is given.
- The (1 + i)-th line contains the maximum capacity C_i of the i-th flower pot and the current amount of water S_i, separated by a space.
Output
Print N lines. On the i-th line, print the amount of water in the i-th flower pot after all operations are completed.
Sample Input 1
3 8 10 3 8 5 6 2
Sample Output 1
10 6 2
Sample Input 2
5 100 10 5 20 20 15 3 8 0 12 7
Sample Output 2
10 20 15 8 12
Sample Input 3
8 1000000000000 1000000000 0 500000000 200000000 1000000000 1000000000 300000000 100000000 700000000 0 400000000 400000000 600000000 150000000 200000000 50000000
Sample Output 3
1000000000 500000000 1000000000 300000000 700000000 400000000 600000000 200000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は図書館の司書として働いています。この図書館には N 個の本棚が一列に並んでおり、各本棚には 1 から N までの番号が付けられています。i 番目の本棚には A_i 冊の本が収められています。
青木君は図書館の蔵書調査を行うことになりました。青木君は Q 回の調査を行います。j 回目の調査では、本棚の番号の範囲として左端 L_j と右端 R_j を指定し、高橋君に L_j 番目から R_j 番目までのすべての本棚に収められている本の合計冊数を尋ねます。
各調査に対して、高橋君が報告すべき本の合計冊数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
- 入力はすべて整数である
入力
N Q A_1 A_2 \ldots A_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- 1 行目には、本棚の個数を表す整数 N と、調査の回数を表す整数 Q が、スペース区切りで与えられる。
- 2 行目には、各本棚に収められている本の冊数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 続く Q 行にわたって、各調査の範囲が与えられる。
- そのうち j 行目(入力全体では 2 + j 行目、1 \leq j \leq Q)には、j 回目の調査で指定される範囲の左端 L_j と右端 R_j が、スペース区切りで与えられる。
出力
Q 行にわたって出力せよ。
- j 行目 (1 \leq j \leq Q) には、j 回目の調査に対する答え、すなわち L_j 番目から R_j 番目までの本棚に収められている本の合計冊数を整数で出力せよ。
入力例 1
5 3 3 1 4 1 5 1 3 2 4 1 5
出力例 1
8 6 14
入力例 2
8 5 10 20 30 40 50 60 70 80 1 1 3 6 1 8 4 7 2 5
出力例 2
10 180 360 220 140
入力例 3
10 8 1000000000 999999999 123456789 987654321 500000000 250000000 750000000 100000000 999999999 1 1 10 1 1 10 10 3 7 5 5 1 5 6 10 2 9
出力例 3
5711111109 1000000000 1 2611111110 500000000 3611111109 2100000000 4711111108
Score : 333 pts
Problem Statement
Takahashi works as a librarian at a library. The library has N bookshelves arranged in a row, numbered from 1 to N. The i-th bookshelf contains A_i books.
Aoki is going to conduct a collection survey of the library. Aoki will perform Q surveys. In the j-th survey, he specifies a range of bookshelf numbers with left endpoint L_j and right endpoint R_j, and asks Takahashi for the total number of books stored in all bookshelves from the L_j-th to the R_j-th.
For each survey, find the total number of books that Takahashi should report.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
- All input values are integers
Input
N Q A_1 A_2 \ldots A_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- The first line contains an integer N representing the number of bookshelves and an integer Q representing the number of surveys, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the number of books stored in each bookshelf, separated by spaces.
- The following Q lines give the range for each survey.
- The j-th of these lines (the (2 + j)-th line of the entire input, 1 \leq j \leq Q) contains the left endpoint L_j and right endpoint R_j of the range specified in the j-th survey, separated by a space.
Output
Output Q lines.
- The j-th line (1 \leq j \leq Q) should contain the answer to the j-th survey, namely the total number of books stored in the bookshelves from the L_j-th to the R_j-th, as an integer.
Sample Input 1
5 3 3 1 4 1 5 1 3 2 4 1 5
Sample Output 1
8 6 14
Sample Input 2
8 5 10 20 30 40 50 60 70 80 1 1 3 6 1 8 4 7 2 5
Sample Output 2
10 180 360 220 140
Sample Input 3
10 8 1000000000 999999999 123456789 987654321 500000000 250000000 750000000 100000000 999999999 1 1 10 1 1 10 10 3 7 5 5 1 5 6 10 2 9
Sample Output 3
5711111109 1000000000 1 2611111110 500000000 3611111109 2100000000 4711111108
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は N 個の整数からなる数列を管理しています。数列の要素には左から順に 1 から N までの番号が付けられており、最初はすべての要素の値が 0 です。
高橋君はこの数列に対して M 回の操作を行います。i 回目の操作では、番号が L_i 以上 R_i 以下であるすべての要素の値に 1 を加算します。
すべての操作を行った後、数列の各要素の値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
- 入力はすべて整数
入力
N M L_1 R_1 L_2 R_2 \vdots L_M R_M
- 1 行目には、数列の要素数 N と操作の回数 M が、スペース区切りで与えられる。
- 1 + i 行目 (1 \leq i \leq M) には、i 回目の操作における区間の左端 L_i と右端 R_i が、スペース区切りで与えられる。
出力
N 個の整数をスペース区切りで 1 行に出力せよ。i 番目 (1 \leq i \leq N) の整数は、すべての操作を行った後の数列の i 番目の要素の値を表す。
入力例 1
5 3 1 3 2 5 3 4
出力例 1
1 2 3 2 1
入力例 2
10 5 1 5 3 8 6 10 1 10 4 6
出力例 2
2 2 3 4 4 4 3 3 2 2
入力例 3
20 10 1 20 3 15 5 10 7 8 1 1 20 20 2 19 10 15 12 18 6 14
出力例 3
2 2 3 3 4 5 6 6 5 6 5 6 6 6 5 3 3 3 2 2
Score : 366 pts
Problem Statement
Takahashi manages a sequence of N integers. The elements of the sequence are numbered from 1 to N from left to right, and initially all elements have a value of 0.
Takahashi performs M operations on this sequence. In the i-th operation, he adds 1 to the values of all elements whose indices are between L_i and R_i, inclusive.
After all operations have been performed, find the value of each element in the sequence.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
- All inputs are integers
Input
N M L_1 R_1 L_2 R_2 \vdots L_M R_M
- The first line contains the number of elements in the sequence N and the number of operations M, separated by a space.
- The (1 + i)-th line (1 \leq i \leq M) contains the left endpoint L_i and right endpoint R_i of the interval for the i-th operation, separated by a space.
Output
Print N integers separated by spaces on a single line. The i-th integer (1 \leq i \leq N) represents the value of the i-th element of the sequence after all operations have been performed.
Sample Input 1
5 3 1 3 2 5 3 4
Sample Output 1
1 2 3 2 1
Sample Input 2
10 5 1 5 3 8 6 10 1 10 4 6
Sample Output 2
2 2 3 4 4 4 3 3 2 2
Sample Input 3
20 10 1 20 3 15 5 10 7 8 1 1 20 20 2 19 10 15 12 18 6 14
Sample Output 3
2 2 3 3 4 5 6 6 5 6 5 6 6 6 5 3 3 3 2 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は配送会社のドライバーとして働いています。今日は倉庫がある地点 1 から、届け先である地点 N まで荷物を届けなければなりません。
地点は 1 から N まで番号が付けられており、地点間を結ぶ M 本の道路があります。各道路にも 1 から M までの番号が付けられています。道路 i (1 \leq i \leq M) は地点 U_i と地点 V_i を双方向に結んでおり、通常の所要時間は W_i 分です。同じ地点の組を結ぶ道路が複数存在することもあります。
しかし、本日は道路工事の影響で K 本の道路が速度規制を受けています。速度規制の対象となっている道路の番号は C_1, C_2, \ldots, C_K として与えられます。速度規制の対象となっている道路は通行可能ですが、所要時間が通常の 2 倍、すなわち 2W_i 分になります。速度規制の対象でない道路の所要時間は通常どおり W_i 分です。
高橋君が地点 1 から地点 N まで移動するのにかかる最短時間(分)を求めてください。地点 1 から地点 N へ到達できない場合は -1 を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq M
- 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
- U_i \neq V_i (1 \leq i \leq M)
- 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
- 1 \leq C_j \leq M (1 \leq j \leq K)
- C_1, C_2, \ldots, C_K はすべて異なる
- 入力はすべて整数である
入力
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M C_1 C_2 \ldots C_K
- 第 1 行には、地点の数を表す整数 N 、道路の数を表す整数 M 、速度規制の対象となっている道路の数を表す整数 K が、スペース区切りで与えられる。
- 第 (1 + i) 行 (1 \leq i \leq M) には、道路 i が結ぶ地点 U_i , V_i と通常の所要時間 W_i が、スペース区切りで与えられる。
- K \geq 1 のとき、第 (M + 2) 行には、速度規制の対象となっている道路の番号 C_1, C_2, \ldots, C_K が、スペース区切りで与えられる。 K = 0 のとき、この行は与えられない(入力は第 (M+1) 行で終了する)。
出力
高橋君が地点 1 から地点 N まで移動するのにかかる最短時間(分)を 1 行で出力せよ。到達できない場合は -1 を出力せよ。
入力例 1
4 5 1 1 2 3 1 3 2 2 4 5 3 4 4 2 3 7 3
出力例 1
6
入力例 2
3 1 0 1 2 5
出力例 2
-1
入力例 3
8 12 3 1 2 4 1 3 7 2 3 2 2 4 5 3 5 3 4 5 1 4 6 8 5 6 6 5 7 9 6 8 3 7 8 2 3 7 10 1 6 11
出力例 3
19
入力例 4
15 20 5 1 2 10 1 3 15 2 4 12 3 4 8 3 5 7 4 6 6 5 6 9 5 7 3 6 8 14 7 8 5 7 9 11 8 10 4 9 10 2 9 11 13 10 12 7 11 12 6 11 13 8 12 14 3 13 14 10 14 15 5 2 5 7 9 19
出力例 4
71
入力例 5
2 1 1 1 2 1000000000 1
出力例 5
2000000000
入力例 6
4 5 1 1 2 3 1 3 2 2 4 5 3 4 4 2 3 7 3
出力例 6
6
入力例 7
3 1 0 1 2 5
出力例 7
-1
入力例 8
8 12 3 1 2 4 1 3 7 2 3 2 2 4 5 3 5 3 4 5 1 4 6 8 5 6 6 5 7 9 6 8 3 7 8 2 3 7 10 1 6 11
出力例 8
19
入力例 9
15 20 5 1 2 10 1 3 15 2 4 12 3 4 8 3 5 7 4 6 6 5 6 9 5 7 3 6 8 14 7 8 5 7 9 11 8 10 4 9 10 2 9 11 13 10 12 7 11 12 6 11 13 8 12 14 3 13 14 10 14 15 5 2 5 7 9 19
出力例 9
71
入力例 10
2 1 1 1 2 1000000000 1
出力例 10
2000000000
Score : 400 pts
Problem Statement
Takahashi works as a driver for a delivery company. Today, he must deliver a package from point 1, where the warehouse is located, to point N, the delivery destination.
The points are numbered from 1 to N, and there are M roads connecting the points. Each road is also numbered from 1 to M. Road i (1 \leq i \leq M) bidirectionally connects point U_i and point V_i, with a normal travel time of W_i minutes. There may be multiple roads connecting the same pair of points.
However, due to road construction today, K roads are subject to speed restrictions. The numbers of the roads under speed restrictions are given as C_1, C_2, \ldots, C_K. Roads under speed restrictions are still passable, but their travel time becomes 2 times the normal time, that is, 2W_i minutes. Roads not under speed restrictions have their normal travel time of W_i minutes.
Find the shortest time (in minutes) for Takahashi to travel from point 1 to point N. If it is impossible to reach point N from point 1, output -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq M
- 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
- U_i \neq V_i (1 \leq i \leq M)
- 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
- 1 \leq C_j \leq M (1 \leq j \leq K)
- C_1, C_2, \ldots, C_K are all distinct
- All input values are integers
Input
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M C_1 C_2 \ldots C_K
- The first line contains the integer N representing the number of points, the integer M representing the number of roads, and the integer K representing the number of roads under speed restrictions, separated by spaces.
- The (1 + i)-th line (1 \leq i \leq M) contains the points U_i, V_i connected by road i and the normal travel time W_i, separated by spaces.
- When K \geq 1, the (M + 2)-th line contains the numbers of the roads under speed restrictions C_1, C_2, \ldots, C_K, separated by spaces. When K = 0, this line is not given (the input ends at the (M+1)-th line).
Output
Output in one line the shortest time (in minutes) for Takahashi to travel from point 1 to point N. If it is impossible to reach point N, output -1.
Sample Input 1
4 5 1 1 2 3 1 3 2 2 4 5 3 4 4 2 3 7 3
Sample Output 1
6
Sample Input 2
3 1 0 1 2 5
Sample Output 2
-1
Sample Input 3
8 12 3 1 2 4 1 3 7 2 3 2 2 4 5 3 5 3 4 5 1 4 6 8 5 6 6 5 7 9 6 8 3 7 8 2 3 7 10 1 6 11
Sample Output 3
19
Sample Input 4
15 20 5 1 2 10 1 3 15 2 4 12 3 4 8 3 5 7 4 6 6 5 6 9 5 7 3 6 8 14 7 8 5 7 9 11 8 10 4 9 10 2 9 11 13 10 12 7 11 12 6 11 13 8 12 14 3 13 14 10 14 15 5 2 5 7 9 19
Sample Output 4
71
Sample Input 5
2 1 1 1 2 1000000000 1
Sample Output 5
2000000000
Sample Input 6
4 5 1 1 2 3 1 3 2 2 4 5 3 4 4 2 3 7 3
Sample Output 6
6
Sample Input 7
3 1 0 1 2 5
Sample Output 7
-1
Sample Input 8
8 12 3 1 2 4 1 3 7 2 3 2 2 4 5 3 5 3 4 5 1 4 6 8 5 6 6 5 7 9 6 8 3 7 8 2 3 7 10 1 6 11
Sample Output 8
19
Sample Input 9
15 20 5 1 2 10 1 3 15 2 4 12 3 4 8 3 5 7 4 6 6 5 6 9 5 7 3 6 8 14 7 8 5 7 9 11 8 10 4 9 10 2 9 11 13 10 12 7 11 12 6 11 13 8 12 14 3 13 14 10 14 15 5 2 5 7 9 19
Sample Output 9
71
Sample Input 10
2 1 1 1 2 1000000000 1
Sample Output 10
2000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は 1 から N までの番号が付いた N 個のボールを、ボール 1, ボール 2, \dots, ボール N の順に、1 から M までの番号が付いた M 個の箱のいずれかに入れていく。1 つの箱には何個でもボールを入れることができる。ボール i を入れた箱の番号を B_i とする(1 \leq B_i \leq M)。
青木君は、箱番号列 B_1, B_2, \dots, B_N を次の手続きで「圧縮番号列」 C_1, C_2, \dots, C_N に変換して記録した。この手続きは、箱番号の出現順に 1, 2, 3, \dots と新しい番号を割り当てるものである。
> はじめ、どの箱番号にも圧縮番号は割り当てられていない。
>
> i = 1, 2, \dots, N の順に以下を行う。
> - 箱番号 B_i にまだ圧縮番号が割り当てられていない場合(すなわち B_i が B_1, B_2, \dots, B_{i-1} のいずれとも異なる場合)、箱番号 B_i に対して、まだどの箱番号にも割り当てられていない最小の正整数を圧縮番号として割り当てる。C_i をその圧縮番号とする。
> - 箱番号 B_i にすでに圧縮番号が割り当てられている場合(すなわち B_1, B_2, \dots, B_{i-1} の中に B_i と等しいものが存在する場合)、C_i を、箱番号 B_i に割り当て済みの圧縮番号とする。
例えば、箱番号列が (5, 5, 2, 7, 2) のとき、箱番号 5 に圧縮番号 1、箱番号 2 に圧縮番号 2、箱番号 7 に圧縮番号 3 が割り当てられ、圧縮番号列は (1, 1, 2, 3, 2) となる。
ところが、記録の一部が汚れて読めなくなってしまった。
長さ N の列 D_1, D_2, \dots, D_N が与えられる。D_i = 0 のとき C_i の値は不明であり、D_i \neq 0 のとき C_i = D_i でなければならない。
箱番号列 B_1, B_2, \dots, B_N(各 B_i は 1 以上 M 以下の整数)であって、上の手続きで得られる圧縮番号列 C_1, C_2, \dots, C_N が D の条件を全て満たすものの個数を、998244353 で割った余りを求めよ。ここで、2 つの箱番号列は、ある i が存在して B_i の値が異なるとき区別する。条件を満たす箱番号列が存在しない場合は 0 を出力せよ。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- N \times \min(N, M) \leq 2.5 \times 10^7
- 0 \leq D_i \leq N
- 入力はすべて整数である
入力
N M D_1 D_2 \dots D_N
- 1 行目には、ボールの個数 N と、箱の番号の最大値 M が、スペース区切りで与えられる。
- 2 行目には、一部が不明になった圧縮番号列を表す D_1, D_2, \dots, D_N が、スペース区切りで与えられる。
- D_i = 0 のとき、i 番目の圧縮番号は不明であることを表す。
- D_i \geq 1 のとき、i 番目の圧縮番号が D_i であることを表す。
出力
条件を満たす箱番号列の個数を 998244353 で割った余りを 1 行で出力せよ。
入力例 1
5 4 1 1 2 0 2
出力例 1
48
入力例 2
4 2 1 2 3 0
出力例 2
0
入力例 3
10 6 1 0 2 0 2 3 0 1 4 0
出力例 3
51840
入力例 4
24 15 1 0 2 0 3 0 1 4 0 2 5 0 0 3 6 0 4 0 6 7 0 5 0 7
出力例 4
280519446
入力例 5
1 1 0
出力例 5
1
Score : 466 pts
Problem Statement
Takahashi places N balls numbered from 1 to N into some of M boxes numbered from 1 to M, in the order of ball 1, ball 2, \dots, ball N. Each box can hold any number of balls. Let B_i denote the box number in which ball i is placed (1 \leq B_i \leq M).
Aoki recorded the box number sequence B_1, B_2, \dots, B_N by converting it into a "compressed number sequence" C_1, C_2, \dots, C_N using the following procedure. This procedure assigns new numbers 1, 2, 3, \dots in the order of first appearance of each box number.
> Initially, no box number has a compressed number assigned to it.
>
> For i = 1, 2, \dots, N in order, do the following:
> - If box number B_i has not yet been assigned a compressed number (i.e., B_i is different from all of B_1, B_2, \dots, B_{i-1}), assign to box number B_i the smallest positive integer not yet assigned to any box number as its compressed number. Set C_i to that compressed number.
> - If box number B_i has already been assigned a compressed number (i.e., there exists some element among B_1, B_2, \dots, B_{i-1} equal to B_i), set C_i to the compressed number already assigned to box number B_i.
For example, when the box number sequence is (5, 5, 2, 7, 2), box number 5 is assigned compressed number 1, box number 2 is assigned compressed number 2, and box number 7 is assigned compressed number 3, resulting in the compressed number sequence (1, 1, 2, 3, 2).
However, part of the record became dirty and unreadable.
You are given a sequence D_1, D_2, \dots, D_N of length N. When D_i = 0, the value of C_i is unknown. When D_i \neq 0, it must hold that C_i = D_i.
Find the number of box number sequences B_1, B_2, \dots, B_N (where each B_i is an integer between 1 and M inclusive) such that the compressed number sequence C_1, C_2, \dots, C_N obtained by the above procedure satisfies all the conditions of D, modulo 998244353. Here, two box number sequences are considered distinct if there exists some i such that their values of B_i differ. If no valid box number sequence exists, output 0.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- N \times \min(N, M) \leq 2.5 \times 10^7
- 0 \leq D_i \leq N
- All input values are integers.
Input
N M D_1 D_2 \dots D_N
- The first line contains the number of balls N and the maximum box number M, separated by a space.
- The second line contains D_1, D_2, \dots, D_N, representing the partially unknown compressed number sequence, separated by spaces.
- When D_i = 0, it means the i-th compressed number is unknown.
- When D_i \geq 1, it means the i-th compressed number is D_i.
Output
Output the number of valid box number sequences modulo 998244353 in a single line.
Sample Input 1
5 4 1 1 2 0 2
Sample Output 1
48
Sample Input 2
4 2 1 2 3 0
Sample Output 2
0
Sample Input 3
10 6 1 0 2 0 2 3 0 1 4 0
Sample Output 3
51840
Sample Input 4
24 15 1 0 2 0 3 0 1 4 0 2 5 0 0 3 6 0 4 0 6 7 0 5 0 7
Sample Output 4
280519446
Sample Input 5
1 1 0
Sample Output 5
1