Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
正整数 N,K 及び長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A に含まれる要素のうち、K の倍数であるもののみを全て取り出し、それらを K で割って出力してください。
制約
- 1\leq N,K\leq 100
- 1\leq A_1 < A_2 < \ldots < A_N \leq 100
- A には K の倍数が 1 個以上含まれる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
A に含まれる要素のうち、K の倍数であるもの全てを K で割った値を、空白区切りで昇順に出力せよ。
入力例 1
5 2 2 5 6 7 10
出力例 1
1 3 5
A に含まれる要素のうち、2 の倍数は 2,6,10 です。それらを 2 で割って得られる 1,3,5 を空白区切りで昇順に出力してください。
入力例 2
3 1 3 4 7
出力例 2
3 4 7
入力例 3
5 10 50 51 54 60 65
出力例 3
5 6
Score: 100 points
Problem Statement
You are given positive integers N and K, and a sequence of length N, A=(A_1,A_2,\ldots,A_N).
Extract all elements of A that are multiples of K, divide them by K, and print the quotients.
Constraints
- 1\leq N,K\leq 100
- 1\leq A_1 < A_2 < \ldots < A_N \leq 100
- A has at least one multiple of K.
- All given numbers are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Divide all elements of A that are multiples of K and print the quotients in ascending order with spaces in between.
Sample Input 1
5 2 2 5 6 7 10
Sample Output 1
1 3 5
The multiples of 2 among the elements in A are 2, 6, and 10. Divide them by 2 to get 1, 3, and 5, and print them in ascending order with spaces in between.
Sample Input 2
3 1 3 4 7
Sample Output 2
3 4 7
Sample Input 3
5 10 50 51 54 60 65
Sample Output 3
5 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
縦 H 行、横 W 列のマス目があり、このうち上から i 個目、左から j 個目のマスを (i,j) と呼びます。
このとき、マス (R,C) に辺で隣接するマスの個数を求めてください。
ただし、ある 2 つのマス (a,b),(c,d) が辺で隣接するとは、 |a-c|+|b-d|=1 (|x| を x の絶対値とする) であることを言います。
制約
- 入力は全て整数
- 1 \le R \le H \le 10
- 1 \le C \le W \le 10
入力
入力は以下の形式で標準入力から与えられる。
H W R C
出力
答えを整数として出力せよ。
入力例 1
3 4 2 2
出力例 1
4
入出力例 1,2,3 に対する説明は、出力例 3 の下にまとめて示します。
入力例 2
3 4 1 3
出力例 2
3
入力例 3
3 4 3 4
出力例 3
2
H=3,W=4 のとき、マス目は以下のようになります。
- 入力例 1 について、マス (2,2) に隣接するマスは 4 つです。
- 入力例 2 について、マス (1,3) に隣接するマスは 3 つです。
- 入力例 3 について、マス (3,4) に隣接するマスは 2 つです。

入力例 4
1 10 1 5
出力例 4
2
入力例 5
8 1 8 1
出力例 5
1
入力例 6
1 1 1 1
出力例 6
0
Score : 100 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square (R, C).
Here, two squares (a,b) and (c,d) are said to share a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).
Constraints
- All values in input are integers.
- 1 \le R \le H \le 10
- 1 \le C \le W \le 10
Input
Input is given from Standard Input in the following format:
H W R C
Output
Print the answer as an integer.
Sample Input 1
3 4 2 2
Sample Output 1
4
We will describe Sample Inputs/Outputs 1,2, and 3 at once below Sample Output 3.
Sample Input 2
3 4 1 3
Sample Output 2
3
Sample Input 3
3 4 3 4
Sample Output 3
2
When H=3 and W=4, the grid looks as follows.
- For Sample Input 1, there are 4 squares adjacent to Square (2,2).
- For Sample Input 2, there are 3 squares adjacent to Square (1,3).
- For Sample Input 3, there are 2 squares adjacent to Square (3,4).

Sample Input 4
1 10 1 5
Sample Output 4
2
Sample Input 5
8 1 8 1
Sample Output 5
1
Sample Input 6
1 1 1 1
Sample Output 6
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
文字列 S が与えられます。S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力してください。
なお、相異なる 2 つの文字列 s = s_1 s_2 \ldots s_n と t = t_1 t_2 \ldots t_m について、それらが以下の条件のいずれかを満たすとき、辞書順で s \lt t であるとします。
- ある整数 i\ (1 \leq i \leq \min(n,m)) が存在し、s_i \lt t_i かつすべての整数 j\ (1 \leq j \lt i) について s_j=t_j
- すべての整数 i\ (1 \leq i \leq \min(n,m)) について s_i = t_i かつ、n \lt m
制約
- S は英小文字のみからなる長さ 1 以上 2 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力せよ。
入力例 1
aba
出力例 1
aab
S= aba を並び替えて得られる文字列は以下の 3 つが考えられます。
abaaabbaa
この中で辞書順で最小のものは、aab です。
入力例 2
zzzz
出力例 2
zzzz
Score : 200 points
Problem Statement
You are given a string S. Find the lexicographically smallest string S' obtained by permuting the characters of S.
Here, for different two strings s = s_1 s_2 \ldots s_n and t = t_1 t_2 \ldots t_m, s \lt t holds lexicographically when one of the conditions below is satisfied.
- There is an integer i\ (1 \leq i \leq \min(n,m)) such that s_i \lt t_i and s_j=t_j for all integers j\ (1 \leq j \lt i).
- s_i = t_i for all integers i\ (1 \leq i \leq \min(n,m)), and n \lt m.
Constraints
- S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
Print the lexicographically smallest string S' obtained by permuting the characters in S.
Sample Input 1
aba
Sample Output 1
aab
Three strings can be obtained by permuting aba:
abaaabbaa
The lexicographically smallest among them is aab.
Sample Input 2
zzzz
Sample Output 2
zzzz
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
H 行 W 列のブロックからなる長方形状のチョコレートがあります。
Q 個のクエリが与えられるので、順に処理したときの各クエリの答えを求めてください。各クエリは、以下のいずれかの形式です。
-
タイプ 1 : 整数 R が与えられる。下 R 行のチョコレートのブロックの個数を求め、それらを食べる。
-
タイプ 2 : 整数 C が与えられる。右 C 列のチョコレートのブロックの個数を求め、それらを食べる。
なお、クエリを順に処理したとき、各クエリを処理した後もチョコレートは長方形状であり、タイプ 1 のクエリを処理する直前の時点でチョコレートは R + 1 行以上存在し、タイプ 2 のクエリを処理する直前の時点でチョコレートは C + 1 列以上存在します。
制約
- 2 \leq H, W \leq 100
- 1 \leq Q \leq 100
- タイプ 1 のクエリについて、1 \leq R
- クエリを順に処理したとき、タイプ 1 のクエリを処理する直前にチョコレートは R + 1 行以上存在する
- タイプ 2 のクエリについて、1 \leq C
- クエリを順に処理したとき、タイプ 2 のクエリを処理する直前にチョコレートは C + 1 列以上存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。
1 R
2 C
出力
Q 行出力せよ。 i (1 \leq i \leq Q) 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 9 5 2 4 1 3 2 1 2 1 1 3
出力例 1
28 15 4 4 9
はじめ、チョコレートは 7 行 9 列の長方形状です。
1 番目のクエリでは、右 4 列のチョコレートの個数は 28 個であるため 28 を出力します。チョコレートは 7 行 5 列となります。
2 番目のクエリでは、下 3 行のチョコレートの個数は 15 個であるため 15 を出力します。チョコレートは 4 行 5 列となります。
3 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 4 列となります。
4 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 3 列となります。
5 番目のクエリでは、下 3 行のチョコレートの個数は 9 個であるため 9 を出力します。チョコレートは 1 行 3 列となります。
Score : 200 points
Problem Statement
There is a rectangular chocolate consisting of H rows and W columns of blocks.
You are given Q queries; process them in order and find the answer to each query. Each query is in one of the following formats:
-
Type 1: An integer R is given. Find the number of chocolate blocks in the bottom R rows, then eat them.
-
Type 2: An integer C is given. Find the number of chocolate blocks in the rightmost C columns, then eat them.
When the queries are processed in order, the chocolate remains rectangular after each query is processed, and it has at least R + 1 rows immediately before processing a type 1 query and has at least C + 1 columns immediately before processing a type 2 query.
Constraints
- 2 \leq H, W \leq 100
- 1 \leq Q \leq 100
- For type 1 queries, 1 \leq R.
- When the queries are processed in order, the chocolate has at least R + 1 rows immediately before processing a type 1 query.
- For type 2 queries, 1 \leq C.
- When the queries are processed in order, the chocolate has at least C + 1 columns immediately before processing a type 2 query.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i is the i-th query, given in one of the following formats:
1 R
2 C
Output
Output Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.
Sample Input 1
7 9 5 2 4 1 3 2 1 2 1 1 3
Sample Output 1
28 15 4 4 9
Initially, the chocolate is a rectangle with 7 rows and 9 columns.
For the first query, the number of chocolate blocks in the rightmost 4 columns is 28, so output 28. The chocolate becomes 7 rows and 5 columns.
For the second query, the number of chocolate blocks in the bottom 3 rows is 15, so output 15. The chocolate becomes 4 rows and 5 columns.
For the third query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 4 columns.
For the fourth query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 3 columns.
For the fifth query, the number of chocolate blocks in the bottom 3 rows is 9, so output 9. The chocolate becomes 1 row and 3 columns.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さは |i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。
残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。
制約
- 1\leq K\leq N \leq 2\times 10^5
- 1\leq A_1 < A_2 < \dots < A_K \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_K
出力
奇妙さの総和の最小値を整数として出力せよ。
入力例 1
4 2 1 3
出力例 1
2
以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。
色 1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4) の 3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。
入力例 2
5 1 2
出力例 2
0
(1,1),(3,3),(4,4),(5,5) の 4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。
入力例 3
8 5 1 2 4 7 8
出力例 3
2
Score : 350 points
Problem Statement
Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.
Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.
Constraints
- 1\leq K\leq N \leq 2\times 10^5
- 1\leq A_1 < A_2 < \dots < A_K \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_K
Output
Print the minimum total weirdness as an integer.
Sample Input 1
4 2 1 3
Sample Output 1
2
Below, let (i,j) denote a pair of a sock of color i and a sock of color j.
There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.
Sample Input 2
5 1 2
Sample Output 2
0
The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).
Sample Input 3
8 5 1 2 4 7 8
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 次元グリッド上に 2 つの図形 S と T があります。グリッドは正方形のマスからなります。
S は N 行 N 列のグリッド内にあり、S_{i,j} が # であるようなマス全体からなります。
T も N 行 N 列のグリッド内にあり、T_{i,j} が # であるようなマス全体からなります。
S と T を 90 度回転及び平行移動の繰り返しによって一致させることができるか判定してください。
制約
- 1 \leq N \leq 200
- S,T は
#と.のみからなる - S,T は 1 つ以上
#を含む
入力
入力は以下の形式で標準入力から与えられる。
N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}
出力
S と T を90 度回転及び平行移動の繰り返しによって一致させることができるとき Yes を、そうでないとき No を出力せよ。
入力例 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
出力例 1
Yes
S を左回りに 90 度回転させ、平行移動することで T に一致させることができます。
入力例 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
出力例 2
No
90 度回転と平行移動の繰り返しによって一致させることはできません。
入力例 3
4 #... ..#. ..#. .... #... #... ..#. ....
出力例 3
Yes
S 及び T は連結とは限りません。
入力例 4
4 #... .##. ..#. .... ##.. #... ..#. ....
出力例 4
No
回転や移動の操作は連結成分ごとにできるわけではなく、S,T 全体に対して行うことに注意してください。
Score : 300 points
Problem Statement
We have two figures S and T on a two-dimensional grid with square cells.
S lies within a grid with N rows and N columns, and consists of the cells where S_{i,j} is #.
T lies within the same grid with N rows and N columns, and consists of the cells where T_{i,j} is #.
Determine whether it is possible to exactly match S and T by 90-degree rotations and translations.
Constraints
- 1 \leq N \leq 200
- Each of S and T consists of
#and.. - Each of S and T contains at least one
#.
Input
Input is given from Standard Input in the following format:
N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}
Output
Print Yes if it is possible to exactly match S and T by 90-degree rotations and translations, and No otherwise.
Sample Input 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
Sample Output 1
Yes
We can match S to T by rotating it 90-degrees counter-clockwise and translating it.
Sample Input 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
Sample Output 2
No
It is impossible to match them by 90-degree rotations and translations.
Sample Input 3
4 #... ..#. ..#. .... #... #... ..#. ....
Sample Output 3
Yes
Each of S and T may not be connected.
Sample Input 4
4 #... .##. ..#. .... ##.. #... ..#. ....
Sample Output 4
No
Note that it is not allowed to rotate or translate just a part of a figure; it is only allowed to rotate or translate a whole figure.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
N+2 個のマスが横一列に並んでいます。左から i 番目のマスをマス i と表します。
マス 1 からマス N には石が 1 個ずつ置かれています。
各 1\leq i \leq N について、S_i が W のときマス i に置かれている石の色は白であり、S_i が B のときマス i に置かれている石の色は黒です。
マス N+1,N+2 には何も置かれていません。
あなたは以下の操作を好きな回数(0 回でもよい)行うことができます。
- 石が 2 個並んでいる箇所を選び、その 2 個の石を順序を保って空きマスに移す。
より正確には次の通り。1 以上 N+1 以下の整数 x であって、マス x,x+1 の両方に石が置かれているものを選ぶ。石の置かれていないマスを k,k+1 とする。マス x,x+1 にある石をそれぞれマス k,k+1 に移動する。
以下の状態にすることが可能か判定し、可能なら操作回数の最小値を求めてください。
- マス 1 からマス N には石が 1 個ずつ置かれており、各 1\leq i \leq N について、T_i が
Wのときマス i に置かれている石の色は白、T_i がBのときマス i に置かれている石の色は黒である。
制約
- 2 \leq N \leq 14
- N は整数である
- S,T は
BおよびWのみからなる長さ N の文字列である
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
目的の状態にすることが可能なら操作回数の最小値を出力せよ。不可能ならかわりに -1 を出力せよ。
入力例 1
6 BWBWBW WWWBBB
出力例 1
4
石が置かれていないマスを . と表します。以下のようにして 4 回の操作で目的の状態にすることができ、これが最小回数です。
BWBWBW..BW..BWBWBWWBB..W..WBBBWWWWWBBB..
入力例 2
6 BBBBBB WWWWWW
出力例 2
-1
入力例 3
14 BBBWBWWWBBWWBW WBWWBBWWWBWBBB
出力例 3
7
Score : 425 points
Problem Statement
There are N+2 cells arranged in a row. Let cell i denote the i-th cell from the left.
There is one stone placed in each of the cells from cell 1 to cell N.
For each 1 \leq i \leq N, the stone in cell i is white if S_i is W, and black if S_i is B.
Cells N+1 and N+2 are empty.
You can perform the following operation any number of times (possibly zero):
- Choose a pair of adjacent cells that both contain stones, and move these two stones to the empty two cells while preserving their order.
More precisely, choose an integer x such that 1 \leq x \leq N+1 and both cells x and x+1 contain stones. Let k and k+1 be the empty two cells. Move the stones from cells x and x+1 to cells k and k+1, respectively.
Determine if it is possible to achieve the following state, and if so, find the minimum number of operations required:
- Each of the cells from cell 1 to cell N contains one stone, and for each 1 \leq i \leq N, the stone in cell i is white if T_i is
W, and black if T_i isB.
Constraints
- 2 \leq N \leq 14
- N is an integer.
- Each of S and T is a string of length N consisting of
BandW.
Input
The input is given from Standard Input in the following format:
N S T
Output
If it is possible to achieve the desired state, print the minimum number of operations required. If it is impossible, print -1.
Sample Input 1
6 BWBWBW WWWBBB
Sample Output 1
4
Using . to represent an empty cell, the desired state can be achieved in four operations as follows, which is the minimum:
BWBWBW..BW..BWBWBWWBB..W..WBBBWWWWWBBB..
Sample Input 2
6 BBBBBB WWWWWW
Sample Output 2
-1
Sample Input 3
14 BBBWBWWWBBWWBW WBWWBBWWWBWBBB
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 個のマスが左右一列に並んでいます。
左から i (1\leq i\leq N) 番目のマスをマス i と呼びます。
最初、すべてのマスは白く塗られています。
Q 個のクエリが与えられるので、順に処理してください。
i (1\leq i\leq Q) 個目のクエリは次のとおりです。
2 つの整数 (L_i,R_i) (1\leq L_i\leq R_i\leq N) が与えられる。
マス L_i, L_i+1, \ldots, R_i をすべて黒く塗る。
このとき、元々白く塗られていたマスは新しく黒く塗られ、元々黒く塗られていたマスはそのままとなる。
その後、(N 個のマスのうち)白く 塗られているマスの個数を求める。
制約
- 1\leq N\leq 10^9
- 1\leq Q\leq 2\times 10^5
- 1\leq L_i\leq R_i\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q
出力
Q 行出力せよ。
i 行目 (1\leq i\leq Q) には、i 個目のクエリに対する答えを出力せよ。
入力例 1
10 5 3 5 8 9 5 8 2 9 6 6
出力例 1
7 5 3 2 2
最初、10 個のマスが左右一列に並んでいます。
- 1 個目のクエリではマス 3,4,5 を黒く塗ります。操作後、白く塗られているのはマス 1,2,6,7,8,9,10 の 7 マスです。
- 2 個目のクエリではマス 8,9 を黒く塗ります。操作後、白く塗られているのはマス 1,2,6,7,10 の 5 マスです。
- 3 個目のクエリではマス 5,6,7,8 を黒く塗ります。操作後、白く塗られているのはマス 1,2,10 の 3 マスです。
- 4 個目のクエリではマス 2,3,\ldots,9 を黒く塗ります。操作後、白く塗られているのはマス 1,10 の 2 マスです。
- 5 個目のクエリではマス 6 を黒く塗ります。操作後、白く塗られているのはマス 1,10 の 2 マスです。
よって、7,5,3,2,2 をこの順に各行に出力します。
入力例 2
1000000000 1 1 500000000
出力例 2
500000000
Score : 450 points
Problem Statement
There are N cells arranged in a row from left to right.
The i-th cell from the left (1\leq i\leq N) is called cell i.
Initially, all cells are painted white.
Process Q queries in order.
The i-th query (1\leq i\leq Q) is as follows:
Two integers (L_i,R_i) (1\leq L_i\leq R_i\leq N) are given.
Paint all cells L_i, L_i+1, \ldots, R_i black.
Here, cells that were painted white are now painted black, and cells that were painted black remain as is.
After that, find the number of cells (among the N cells) that are painted white.
Constraints
- 1\leq N\leq 10^9
- 1\leq Q\leq 2\times 10^5
- 1\leq L_i\leq R_i\leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q L_1 R_1 L_2 R_2 \vdots L_Q R_Q
Output
Output Q lines.
The i-th line (1\leq i\leq Q) should contain the answer to the i-th query.
Sample Input 1
10 5 3 5 8 9 5 8 2 9 6 6
Sample Output 1
7 5 3 2 2
Initially, 10 cells are arranged in a row from left to right.
- The first query paints cells 3,4,5 black. After the operation, the cells painted white are cells 1,2,6,7,8,9,10, which is 7 cells.
- The second query paints cells 8,9 black. After the operation, the cells painted white are 1,2,6,7,10, which is 5 cells.
- The third query paints cells 5,6,7,8 black. After the operation, the cells painted white are 1,2,10, which is 3 cells.
- The fourth query paints cells 2,3,\ldots,9 black. After the operation, the cells painted white are 1,10, which is 2 cells.
- The fifth query paints cell 6 black. After the operation, the cells painted white are cells 1,10, which is 2 cells.
Thus, output 7,5,3,2,2 in this order on each line.
Sample Input 2
1000000000 1 1 500000000
Sample Output 2
500000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1,\ldots,N の番号がついた N 枚のカードがあり、カード i の表には P_i が、裏には Q_i が書かれています。
ここで、P=(P_1,\ldots,P_N) 及び Q=(Q_1,\ldots,Q_N) はそれぞれ (1, 2, \dots, N) の並び替えです。
N 枚のカードから何枚かを選ぶ方法のうち、次の条件を満たすものは何通りありますか? 998244353 で割った余りを求めてください。
条件:1,2,\ldots,N のどの数も選んだカードのいずれかに書かれている
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq P_i,Q_i \leq N
- P,Q はそれぞれ (1, 2, \dots, N) の並び替えである
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
出力
答えを出力せよ。
入力例 1
3 1 2 3 2 1 3
出力例 1
3
例えばカード 1,3 を選ぶと、1 はカード 1 の表に、2 はカード 1 の裏に、3 はカード 3 の表に書かれているため条件を満たします。
条件を満たすカードの選び方は \{1,3\},\{2,3\},\{1,2,3\} の 3 通りです。
入力例 2
5 2 3 5 4 1 4 2 1 3 5
出力例 2
12
入力例 3
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
出力例 3
1
Score : 500 points
Problem Statement
There are N cards numbered 1,\ldots,N. Card i has P_i written on the front and Q_i written on the back.
Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N).
How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353.
Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq P_i,Q_i \leq N
- P and Q are permutations of (1, 2, \dots, N).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
Output
Print the answer.
Sample Input 1
3 1 2 3 2 1 3
Sample Output 1
3
For example, if you choose Cards 1 and 3, then 1 is written on the front of Card 1, 2 on the back of Card 1, and 3 on the front of Card 3, so this combination satisfies the condition.
There are 3 ways to choose cards satisfying the condition: \{1,3\},\{2,3\},\{1,2,3\}.
Sample Input 2
5 2 3 5 4 1 4 2 1 3 5
Sample Output 2
12
Sample Input 3
8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Sample Output 3
1