E - Snake Queue

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

ヘビの待ち行列があります。最初、列は空です。

クエリが Q 個与えられるので、与えられた順に処理してください。クエリは以下の 3 種類です。

  • タイプ 1 : 1 l の形式で与えられる。長さ l のヘビが列の末尾に追加される。このとき追加するヘビの頭の位置は、元の列が空の場合は座標 0、そうでない場合は最後尾のヘビの頭の座標に最後尾のヘビの長さを加えた座標となる。
  • タイプ 2 : 2 の形式で与えられる。列の先頭にいるヘビが列から抜ける。このとき、列が空でないことは保証される。抜けたヘビの長さを m として、列に残っている全てのヘビの頭の座標が m だけ減少する。
  • タイプ 3 : 3 k の形式で与えられる。列の先頭から数えて k 番目にいるヘビの頭の座標を出力せよ。このとき、列には少なくとも k 匹のヘビがいることが保証される。

制約

  • 1 \leq Q \leq 3 \times 10^{5}
  • タイプ 1 のクエリにおいて、1 \leq l \leq 10^{9}
  • タイプ 2 のクエリにおいて、列が空でないことが保証される
  • タイプ 3 のクエリにおいて、列にいるヘビの数を n として、1 \leq k \leq n
  • 入力は全て整数

入力

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

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

ただし、\text{query}_ii 個目のクエリを表し、以下のいずれかの形式である。

1 l
2
3 k

出力

タイプ 3 のクエリの個数を q として、q 行出力せよ。i 行目には、i 個目のタイプ 3 のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

5
10
  • 1 個目のクエリ : 長さ 5 のヘビが列に追加される。列にヘビはいないため、追加されたヘビの頭の座標は 0 となる。
  • 2 個目のクエリ : 長さ 7 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 0 で長さが 5 のため、追加されたヘビの頭の座標は 5 となる。
  • 3 個目のクエリ : 前から 2 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 5 であるため、5 を出力する。
  • 4 個目のクエリ : 長さ 3 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 5 で長さが 7 のため、追加されたヘビの頭の座標は 12 となる。
  • 5 個目のクエリ : 長さ 4 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 12 で長さが 3 のため、追加されたヘビの頭の座標は 15 となる。
  • 6 個目のクエリ : 先頭のヘビが列から抜ける。抜けたヘビの長さが 5 であるため、列にいるヘビの頭の座標は 5 だけ減少する。列に残っているヘビの頭の座標は先頭から順に 0,7,10 となる。
  • 7 個目のクエリ : 前から 3 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 7, 10 であるため、10 を出力する。

入力例 2

3
1 1
2
1 3

出力例 2



タイプ 3 のクエリが 1 つもない場合もあります。


入力例 3

10
1 15
1 10
1 5
2
1 5
1 10
1 15
2
3 4
3 2

出力例 3

20
5

Score : 300 points

Problem Statement

There is a queue of snakes. Initially, the queue is empty.

You are given Q queries, which should be processed in the order they are given. There are three types of queries:

  • Type 1: Given in the form 1 l. A snake of length l is added to the end of the queue. If the queue was empty before adding, the head position of the newly added snake is 0; otherwise, it is the sum of the head coordinate of the last snake in the queue and the last snake’s length.
  • Type 2: Given in the form 2. The snake at the front of the queue leaves the queue. It is guaranteed that the queue is not empty at this time. Let m be the length of the snake that left, then the head coordinate of every snake remaining in the queue decreases by m.
  • Type 3: Given in the form 3 k. Output the head coordinate of the snake that is k-th from the front of the queue. It is guaranteed that there are at least k snakes in the queue at this time.

Constraints

  • 1 \leq Q \leq 3 \times 10^{5}
  • For a query of type 1, 1 \leq l \leq 10^{9}
  • For a query of type 2, it is guaranteed that the queue is not empty.
  • For a query of type 3, let n be the number of snakes in the queue, then 1 \leq k \leq n.
  • All input values are integers.

Input

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

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

Here, \text{query}_i is the i-th query in one of the following forms:

1 l
2
3 k

Output

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


Sample Input 1

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

Sample Output 1

5
10
  • 1st query: A snake of length 5 is added to the queue. Since the queue was empty, the head coordinate of this snake is 0.
  • 2nd query: A snake of length 7 is added to the queue. Before adding, the last snake has head coordinate 0 and length 5, so the newly added snake’s head coordinate is 5.
  • 3rd query: Output the head coordinate of the snake that is 2nd from the front. Currently, the head coordinates of the snakes in order are 0, 5, so output 5.
  • 4th query: A snake of length 3 is added to the queue. Before adding, the last snake has head coordinate 5 and length 7, so the new snake’s head coordinate is 12.
  • 5th query: A snake of length 4 is added to the queue. Before adding, the last snake has head coordinate 12 and length 3, so the new snake’s head coordinate is 15.
  • 6th query: The snake at the front leaves the queue. The length of the snake that left is 5, so the head coordinate of each remaining snake decreases by 5. The remaining snake’s head coordinate becomes 0, 7, 10.
  • 7th query: Output the head coordinate of the snake that is 3rd from the front. Currently, the head coordinates of the snakes in order are 0, 7, 10, so output 10.

Sample Input 2

3
1 1
2
1 3

Sample Output 2



It is possible that there are no queries of type 3.


Sample Input 3

10
1 15
1 10
1 5
2
1 5
1 10
1 15
2
3 4
3 2

Sample Output 3

20
5
F - Variety Split Easy

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

この問題は F 問題の簡易版です。

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

A1 か所で区切って 2 個の空でない連続する部分列に分割するとき、数列に含まれる数の種類数の和としてありえる最大値を求めてください。

より厳密には、1 \leq i \leq N-1 を満たす整数 i について (A_1,A_2,\ldots,A_i), (A_{i+1},A_{i+2},\ldots,A_N) のそれぞれに含まれる整数の種類数の和としてありえる最大値を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • 入力はすべて整数である

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

5
  • i=1 としたとき、(3)(1,4,1,5) のそれぞれに含まれる整数の種類数は 1,3 で、これらの和は 4 です。
  • i=2 としたとき、(3,1)(4,1,5) のそれぞれに含まれる整数の種類数は 2,3 で、これらの和は 5 です。
  • i=3 としたとき、(3,1,4)(1,5) のそれぞれに含まれる整数の種類数は 3,2 で、これらの和は 5 です。
  • i=4 としたとき、(3,1,4,1)(5) のそれぞれに含まれる整数の種類数は 3,1 で、これらの和は 4 です。

よって、 i=2,3 のときに、最大値 5 を取ります。


入力例 2

10
2 5 6 5 2 1 7 9 7 2

出力例 2

8

Score : 350 points

Problem Statement

This problem is a simplified version of Problem F.

You are given an integer sequence of length N: A = (A_1, A_2, \ldots, A_N).

When splitting A at one position into two non-empty (contiguous) subarrays, find the maximum possible sum of the counts of distinct integers in those subarrays.

More formally, find the maximum sum of the following two values for an integer i such that 1 \leq i \leq N-1: the count of distinct integers in (A_1, A_2, \ldots, A_i), and the count of distinct integers in (A_{i+1}, A_{i+2}, \ldots, A_N).

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

5
  • For i=1, (3) contains 1 distinct integer, and (1,4,1,5) contains 3 distinct integers, for a total of 4.
  • For i=2, (3,1) contains 2 distinct integers, and (4,1,5) contains 3 distinct integers, for a total of 5.
  • For i=3, (3,1,4) contains 3 distinct integers, and (1,5) contains 2 distinct integers, for a total of 5.
  • For i=4, (3,1,4,1) contains 3 distinct integers, and (5) contains 1 distinct integer, for a total of 4.

Therefore, the maximum sum is 5 for i=2,3.


Sample Input 2

10
2 5 6 5 2 1 7 9 7 2

Sample Output 2

8
G - Play Train

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は電車のおもちゃを連結させたり分離させたりして遊んでいます。
電車は N 個あり、電車 1 、電車 2\ldots 、電車 N と名前がついています。
はじめ電車どうしは連結しておらず全てバラバラです。

クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 x y :電車 x の後部と、電車 y の前部を連結させる。
    以下のことが保証されます。

    • x \neq y
    • クエリを処理する直前に、電車 x の後部と連結している電車は存在しない
    • クエリを処理する直前に、電車 y の前部と連結している電車は存在しない
    • クエリを処理する直前に、電車 x と電車 y は異なる連結成分に属する
  • 2 x y :電車 x の後部と、電車 y の前部を分離させる。
    以下のことが保証されます。

    • x \neq y
    • クエリを処理する直前に、電車 x の後部と電車 y の前部は直接連結している
  • 3 x :電車 x が含まれる連結成分に属する電車の番号を、先頭から順番に全て出力する。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq x \leq N
  • 1 \leq y \leq N
  • 入力は全て整数
  • クエリは全て問題文の条件を満たす
  • 3 x の形式のクエリで出力する電車の番号の個数の合計は 10^6 以下

入力

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

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

i 番目のクエリ \mathrm{query}_i では、まずクエリの種類 c_i1, 2, 3 のいずれか)が与えられる。
c_i = 1,2 の場合は x,y が追加で与えられ、c_i =3 の場合は x が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x y
2 x y
3 x

出力

ある c_i = 3 のタイプのクエリにおいて、出力すべき値が j_1, j_2, \ldots , j_M であるとする。
このとき以下の形式で 1 行に出力せよ。

M j_1 j_2 \ldots j_M

c_i = 3 のタイプのクエリの数を q として、q 行出力せよ。
k (1 \leq k \leq q) 行目では k 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

7 14
1 6 3
1 4 1
1 5 2
1 2 7
1 3 5
3 2
3 4
3 6
2 3 5
2 4 1
1 1 5
3 2
3 4
3 6

出力例 1

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

\mathrm{query}_5 まで処理した時、電車は以下のようになっています。
この時、たとえば電車 2 は、電車 3,5,6,7 と同じ連結成分に属していますが、電車 1,4 とは同じ連結成分に属していません。

\mathrm{query}_{11} まで処理した時、電車は以下のようになっています。

Score : 400 points

Problem Statement

Takahashi is playing with toy trains, connecting and disconnecting them.
There are N toy train cars, with car numbers: Car 1, Car 2, \ldots, Car N.
Initially, all cars are separated.

You will be given Q queries. Process them in the order they are given. There are three kinds of queries, as follows.

  • 1 x y: Connect the front of Car y to the rear of Car x.
    It is guaranteed that:

    • x \neq y
    • just before this query, no train is connected to the rear of Car x;
    • just before this query, no train is connected to the front of Car y;
    • just before this query, Car x and Car y belong to different connected components.
  • 2 x y: Disconnect the front of Car y from the rear of Car x.
    It is guaranteed that:

    • x \neq y;
    • just before this query, the front of Car y is directly connected to the rear of Car x.
  • 3 x: Print the car numbers of the cars belonging to the connected component containing Car x, from front to back.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq x \leq N
  • 1 \leq y \leq N
  • All values in input are integers.
  • All queries satisfy the conditions in the Problem Statement.
  • The queries of the format 3 x ask to print at most 10^6 car numbers in total.

Input

Input is given from Standard Input in the following format:

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

The i-th query \mathrm{query}_i begins with an integer c_i (1, 2, or 3) representing the kind of the query, followed by x and y if c_i = 1 or 2, and followed by x if c_i = 3.

In short, each query is in one of the following three formats:

1 x y
2 x y
3 x

Output

If a query with c_i = 3 asks to print the values j_1, j_2, \ldots, j_M, output the following line:

M j_1 j_2 \ldots j_M

Your output should consist of q lines, where q is the number of queries with c_i = 3.
The k-th line (1 \leq k \leq q) should contain the response to the k-th such query.


Sample Input 1

7 14
1 6 3
1 4 1
1 5 2
1 2 7
1 3 5
3 2
3 4
3 6
2 3 5
2 4 1
1 1 5
3 2
3 4
3 6

Sample Output 1

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

The figure below shows the cars when the first 5 queries are processed.
For example, Car 2 belongs to the same connected component as Cars 3, 5, 6, 7, which is different from the connected component containing Cars 1, 4.

The figure below shows the cars when the first 11 queries are processed.

H - Tangency of Cuboids

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

3 次元空間内に N 個の直方体があります。

直方体同士は重なっていません。厳密には、相異なるどの 2 つの直方体の共通部分の体積も 0 です。

i 番目の直方体は、2(X_{i,1},Y_{i,1},Z_{i,1}), (X_{i,2},Y_{i,2},Z_{i,2}) を結ぶ線分を対角線とし、辺は全ていずれかの座標軸に平行です。

各直方体について、他のいくつの直方体と面で接しているか求めてください。
厳密には、各 i に対し、1\leq j \leq N かつ j\neq i である j のうち、i 番目の直方体の表面と j 番目の直方体の表面の共通部分の面積が正であるものの個数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq X_{i,1} < X_{i,2} \leq 100
  • 0 \leq Y_{i,1} < Y_{i,2} \leq 100
  • 0 \leq Z_{i,1} < Z_{i,2} \leq 100
  • 直方体同士は体積が正の共通部分を持たない
  • 入力は全て整数である

入力

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

N
X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2}
\vdots
X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}

出力

答えを出力せよ。


入力例 1

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

出力例 1

1
1
0
0

1 番目の直方体と 2 番目の直方体は、2(0,0,1),(1,1,1) を結ぶ線分を対角線とする長方形を共有しています。
1 番目の直方体と 3 番目の直方体は、点 (1,1,1) を共有していますが、面で接してはいません。


入力例 2

3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10

出力例 2

2
1
1

入力例 3

8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2

出力例 3

3
3
3
3
3
3
3
3

Score : 500 points

Problem Statement

There are N rectangular cuboids in a three-dimensional space.

These cuboids do not overlap. Formally, for any two different cuboids among them, their intersection has a volume of 0.

The diagonal of the i-th cuboid is a segment that connects two points (X_{i,1},Y_{i,1},Z_{i,1}) and (X_{i,2},Y_{i,2},Z_{i,2}), and its edges are all parallel to one of the coordinate axes.

For each cuboid, find the number of other cuboids that share a face with it.
Formally, for each i, find the number of j with 1\leq j \leq N and j\neq i such that the intersection of the surfaces of the i-th and j-th cuboids has a positive area.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq X_{i,1} < X_{i,2} \leq 100
  • 0 \leq Y_{i,1} < Y_{i,2} \leq 100
  • 0 \leq Z_{i,1} < Z_{i,2} \leq 100
  • Cuboids do not have an intersection with a positive volume.
  • All input values are integers.

Input

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

N
X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2}
\vdots
X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

1
1
0
0

The 1-st and 2-nd cuboids share a rectangle whose diagonal is the segment connecting two points (0,0,1) and (1,1,1).
The 1-st and 3-rd cuboids share a point (1,1,1), but do not share a surface.


Sample Input 2

3
0 0 10 10 10 20
3 4 1 15 6 10
0 9 6 1 20 10

Sample Output 2

2
1
1

Sample Input 3

8
0 0 0 1 1 1
0 0 1 1 1 2
0 1 0 1 2 1
0 1 1 1 2 2
1 0 0 2 1 1
1 0 1 2 1 2
1 1 0 2 2 1
1 1 1 2 2 2

Sample Output 3

3
3
3
3
3
3
3
3
I - Shift Table

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

高橋君と青木君は、これから N 日間アルバイトをします。
高橋君のシフト表は文字列 S により与えられ、Si 文字目が # のとき i 日目に出勤、. のとき i 日目に欠勤します。
これをもとに、青木君は以下のようにシフト表を作りました。

  • まず、N でない N の正の約数 M をとる。
  • 次に、1 日目から M 日目までの勤怠を決める。
  • 最後に、i = 1, 2, \ldots, N - M の順に M + i 日目の勤怠が i 日目の勤怠と一致するように M + i 日目の勤怠を決める。

ここで、M の値が異なる場合でも最終的にできたシフトが同じものとなることがあることに注意してください。

N 日すべてについて高橋君と青木君の少なくとも一方が出勤することになったとき、青木君のシフト表として考えられるものの個数を 998244353 で割った余りを求めてください。

制約

  • N2 以上 10^5 以下の整数
  • S は 長さ N#. からなる文字列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

6
##.#.#

出力例 1

3

高橋君は 1 \cdot 2 \cdot 4 \cdot 6 日目に出勤します。
青木君のシフト表を表す文字列を T とし、青木君は Ti 文字目が # のとき i 日目に出勤、. のとき i 日目に欠勤するとします。
T としてあり得るものは ###### \cdot #.#.#. \cdot .##.##3 つです。

1 つめのシフト表は M1 または 2 または 32 つめのシフト表は M = 23 つめのシフト表は M = 3 とすることにより実現できます。


入力例 2

7
...####

出力例 2

1

入力例 3

12
####.####.##

出力例 3

19

Score : 525 points

Problem Statement

Takahashi and Aoki will work part-time for the next N days.
Takahashi's shift schedule is given by the string S, where the i-th character of S is # if he works on the i-th day, and . if he is absent on the i-th day.
Based on this, Aoki created his shift schedule as follows.

  • First, take a positive divisor M of N that is not equal to N.
  • Next, decide the attendance for the first M days.
  • Finally, for i = 1, 2, \ldots, N - M in this order, decide the attendance for the (M + i)-th day so that it matches the attendance for the i-th day.

Note that different values of M may lead to the same final shift schedule.

Find the number of possible shift schedules for Aoki such that at least one of Takahashi and Aoki works on each of the N days, modulo 998244353.

Constraints

  • N is an integer between 2 and 10^5, inclusive.
  • S is a string of length N consisting of # and ..

Input

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

N
S

Output

Print the answer.


Sample Input 1

6
##.#.#

Sample Output 1

3

Takahashi works on days 1, 2, 4, and 6.
Let T be the string representing Aoki's shift schedule, where the i-th character of T is # if he works on the i-th day, and . if he is absent on the i-th day.
There are three possible strings for T: ######, #.#.#., and .##.##.

The first shift schedule can be realized by choosing M = 1 or 2 or 3, the second by choosing M = 2, and the third by choosing M = 3.


Sample Input 2

7
...####

Sample Output 2

1

Sample Input 3

12
####.####.##

Sample Output 3

19