JOI2022
JOI2022
Final Round
February 13, 2022 (Online)
1 Intercastellar
In 30XX, due to the constant efforts of scientists and engineers, interaction among different planets becomes
very active. Bitaro is a beaver who is working as an ambassador of an exchange program. His task is to
introduce foods from the Earth to the habitants in different planets. He will leave for the JOI Planet at 1:00 in
the afternoon.
Now, Bitaro is planning to introduce castella to the habitants in the JOI Planet. The castella was already cut
into several pieces. Castella is a baked sponge cake made of flour, egg, sugar, and starch syrup.
The shape of the castella is a horizontally long rectangular box. It was cut into N pieces. The length of the
i-th piece (1 ≤ i ≤ N) from the left is an integer Ai .
A couple of minutes ago, it turned out that the habitants in the JOI Planet do not like even integers. To cope
with this problem, you will perform the following sequential operations until pieces of even length disappear.
1. Among the pieces of even length, you choose the rightmost one.
2. You cut the chosen piece into two pieces of equal length. Namely, if the length of the chosen piece is k,
k
you cut it into two pieces of length . You do not move the position of the pieces.
2
To confirm whether the operations are performed correctly, Bitaro will ask you Q questions. The j-th question
(1 ≤ j ≤ Q) is as follows.
• After all the operations are performed, what is the length of the X j -th piece from the left?
Given information of the castella and the questions, write a program which answer the questions.
Task 1- 1 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Input
Read the following data from the standard input. Given values are all integers.
N
A1
A2
..
.
AN
Q
X1
X2
..
.
XQ
Output
Write Q lines to the standard output. The j-th line (1 ≤ j ≤ Q) should contain the answer to the j-th question.
Constraints
• 1 ≤ N ≤ 200 000.
• 1 ≤ Ai ≤ 1 000 000 000 (1 ≤ i ≤ N).
• 1 ≤ Q ≤ 200 000.
• 1 ≤ X j ≤ 1 000 000 000 000 000 (= 1015 ) (1 ≤ j ≤ Q).
• X j ≤ X j+1 (1 ≤ j ≤ Q − 1).
• After all the operations are performed, the castella is cut into at least XQ pieces.
Subtasks
1. (25 points) Ai ≤ 8 (1 ≤ i ≤ N).
2. (35 points) N ≤ 1 000, Q ≤ 1 000.
3. (40 points) No additional constraints.
Task 1- 2 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
In the beginning, the lengths of the pieces of the castella are 14, 9, 8, 12 from the left.
After all the operations are performed, the castella is cut into 15 pieces. The lengths of the pieces are
7, 7, 9, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3 from the left.
This sample input satisfies the constraints of Subtasks 2, 3.
Task 1- 3 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Task 1- 4 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Task 1- 5 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
2 Self Study
In the third semester of the first grade of JOI High School, N courses are given for M weeks from the first
week to the M-th week. The courses are numbered from 1 to N. In each week, N classes are given. The i-th
class in each week is a class for Course i.
Bitaro is a student of the first grade. In each of the N × M classes, he takes one of the following actions.
• Action 1: Bitaro attends the class. If he attends a class for Course i (1 ≤ i ≤ N), the comprehension level
of Course i will be increased by Ai .
• Action 2: Bitaro does not attend the class. Instead, he chooses any one of the courses, and studies for the
chosen course by himself. If he studies for Course i (1 ≤ i ≤ N) by himself for the duration of a class,
the comprehension level of Course i will be increased by Bi .
In the beginning, the comprehension level of every course is 0. Since Bitaro wants to practice competitive
programming after school, he will not study outside the duration of the classes. When all the classes in the third
semester finish, the final examination will be held.
Bitaro does not want to get a failing grade. Therefore, he wants to maximize the minimum comprehension
level of the courses at the moment of the final examination.
Given the length of the semester, the number of the courses, and the incremental values of the comprehension
levels, write a program which calculates the maximum possible value of the minimum comprehension level of
the courses at the moment of the final examination.
Input
Read the following data from the standard input. Given values are all integers.
NM
A1 A2 · · · AN
B1 B2 · · · BN
Output
Write one line to the standard output. The output should contain the maximum possible value of the minimum
comprehension level of the courses at the moment of the final examination.
Task 2- 1 / 3
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Constraints
• 1 ≤ N ≤ 300 000.
• 1 ≤ M ≤ 1 000 000 000.
• 1 ≤ Ai ≤ 1 000 000 000 (1 ≤ i ≤ N).
• 1 ≤ Bi ≤ 1 000 000 000 (1 ≤ i ≤ N).
Subtasks
1. (10 points) M = 1.
2. (25 points) N × M ≤ 300 000,Ai = Bi (1 ≤ i ≤ N).
3. (27 points) N × M ≤ 300 000.
4. (29 points) Ai = Bi (1 ≤ i ≤ N).
5. (9 points) No additional constraints.
For example, if Bitaro studies in the following way, the compehension level of Course 1, 2, 3 will be 19, 18, 19,
respectively.
• In the first week, at the time of Course 1, he studies for Course 2 by himself.
• In the first week, at the time of Course 2, he studies for Course 2 by himself.
• In the first week, at the time of Course 3, he attends the class for Course 3.
• In the second week, at the time of Course 1, he attends the class for Course 1.
• In the second week, at the time of Course 2, he studies for Course 3 by himself.
• In the second week, at the time of Course 3, he attends the class for Course 3.
• In the third week, at the time of Course 1, he studies for Course 3 by himself.
• In the third week, at the time of Course 2, he studies for Course 2 by himself.
• In the third week, at the time of Course 3, he attends the class for Course 3.
Task 2- 2 / 3
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Since the minimum comprehension level of the courses cannot be larger than or equal to 19, output 18.
This sample input satisfies the constraints of Subtasks 3, 5.
Task 2- 3 / 3
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Republic of JOI consists of N states, numbered from 1 to N. In 2022, the presidential election will be held in
Republic of JOI. The election will be held in each state. The winner of the election in a state will get the vote
of the state.
Rie will run for the president. She is planning to win the election. Her plan is to deliver a speech in order to
increase the degree of reliability. After she delivers a speech, the following will happen.
• If the total time of speech in State i (1 ≤ i ≤ N) reaches Ai hours, she will get the vote of State i.
• If the total time of speech in State i (1 ≤ i ≤ N) reaches Bi hours, she will get a collaborator from State i.
After that, the collaborator will be able to deliver a speech in order to increase the total time of speech.
• It may be the case that Rie cannot get any collaborator from State i. In this case, Bi = −1. Otherwise, it
is guaranteed that Bi ≥ Ai holds.
A collaborator from State i (1 ≤ i ≤ N) may deliver a speech outside State i. More than one person may
deliver a speech in the same state simultaneously. For example, if two people deliver a speech in a state for x
hours, the total time of speech in the state will be increased by 2x hours. The time of speech needs not be an
integer. We will ignore the travel time between states.
Since the election day is coming soon, Rie would like to get K votes as soon as possible.
Given the number of the states and information of each state, write a program which calculate the minimum
number of hours required to get K votes.
Input
Read the following data from the standard input. Given values are all integers.
N
K
A1 B1
A2 B2
..
.
A N BN
Task 3- 1 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Output
Write one line to the standard output. The output should contain the minimum number of hours required to
get K votes. Your solution will be judged correct if the absolute value of the difference from correct answer is
less than or equal to 0.01. The output should be written in one of the following formats.
The output should not be written in exponential notation. For example, 1.23456e+05 and 1.23456e5 are
not allowed.
Constraints
• 1 ≤ N ≤ 500.
• 1 ≤ K ≤ N.
• 1 ≤ Ai ≤ 1 000 (1 ≤ i ≤ N).
• Ai ≤ Bi ≤ 1 000 or Bi = −1 (1 ≤ i ≤ N).
Subtasks
1. (5 points) Bi = −1 (1 ≤ i ≤ N).
2. (5 points) Bi = −1 or Bi = Ai (1 ≤ i ≤ N).
3. (11 points) N ≤ 7.
4. (12 points) N ≤ 20.
5. (33 points) N ≤ 100.
6. (11 points) K = N.
7. (23 points) No additional constraints.
Task 3- 2 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
If the election campaign is held in the following order, Rie will get the vote of every state in 5.5 hours.
1. Rie delivers a speech for 2 hours in State 2, and gets the vote of State 2.
2. Moreover, Rie delivers a speech for one hour in State 2, and gets a collaborator from State 2.
3. Rie and the collaborator deliver a speech for 2 hours in State 3, and Rie gets the vote of State 3.
4. Rie and the collaborator deliver a speech for 0.5 hour in State 1, and Rie gets the vote of State 1.
If the election campaign is held in the following order, Rie will get 4 votes in 32 hours.
1. Rie delivers a speech for 4 hours in State 1, and gets the vote of State 1.
2. Rie delivers a speech for 11 hours in State 2, and gets the vote of State 2.
3. Rie delivers a speech for 6 hours in State 3, and gets the vote of State 3.
4. Rie delivers a speech for 11 hours in State 6, and gets the vote of State 6.
Task 3- 3 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
If the election campaign is held in the following order, Rie will get 3 votes in 11.5 hours.
1. Rie delivers a speech for 7 hours in State 4, and gets the vote of State 4 and a collaborator from State 4.
2. Rie delivers a speech for 4 hours in State 1, and gets the vote of State 1. At the same time, the collaborator
delivers a speech for 4 hours in State 2.
3. Rie and the collaborator deliver a speech for 0.5 hour in State 2, and Rie gets the vote of State 2.
Task 3- 4 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Task 3- 5 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
4 Railway Trip 2
IOI Railway Company is operating lines on a railway track. There are N stations in a straight line, numbered
from 1 to N. For each i (1 ≤ i ≤ N − 1), Station i and Station i + 1 are connected directly by a railway track.
IOI Railway Company is operating M lines, numbered from 1 to M. In Line j (1 ≤ j ≤ M), the starting
station is Station A j , and the terminal station is Station B j . A train stops at every station. Namely, if A j < B j
a train of Line j stops at Station A j , Station A j + 1, . . . , Station B j , in this order. If A j > B j , a train of Line j
stops at Station A j , Station A j − 1, . . . , Station B j , in this order.
JOI-kun is a traveler. He has Q travel plans. In the k-th plan (1 ≤ k ≤ Q), he travels from Station S k to Station
T k by taking lines.
However, JOI-kun is tired from a long journey. He wants to take a vacant train and get a seat. Thus, JOI-kun
decided that he takes a train of a line at a station only if it is the K-th or earlier stop from the starting station of
the line. In other words, if A j < B j , he can take a train of Line j only at Station A j , Station A j + 1, . . . , Station
min{A j + K − 1, B j − 1}. If A j > B j , he can take a train of Line j only at Station A j , Station A j − 1, . . . , Station
max{A j − K + 1, B j + 1}. JOI-kun will get out of the train at a station between the station next to where he takes
the train and the terminal station, inclusive.
Under these conditions, JOI-kun wants to minimize the number of times of taking trains.
Given the information of the lines of IOI Railway Company and JOI-kun’s plans, write a program which
calculates, for each of JOI-kun’s plans, the minimum number of times of taking trains needed for JOI-kun to
achieve it.
Task 4- 1 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Input
Read the following data from the standard input. Given values are all integers.
NK
M
A1 B1
A2 B2
..
.
A M BM
Q
S 1 T1
S 2 T2
..
.
S Q TQ
Output
Write Q lines to the standard output. The k-th line (1 ≤ k ≤ Q) should contain the minimum number of times
of taking trains needed for JOI-kun to achieve the k-th plan. If it is not possible to achieve the k-th plan, output
-1.
Constraints
• 2 ≤ N ≤ 100 000.
• 1 ≤ K ≤ N − 1.
• 1 ≤ M ≤ 200 000.
• 1 ≤ A j ≤ N (1 ≤ j ≤ M).
• 1 ≤ B j ≤ N (1 ≤ j ≤ M).
• A j , B j (1 ≤ j ≤ M).
• (A j , B j ) , (Ak , Bk ) (1 ≤ j < k ≤ M).
• 1 ≤ Q ≤ 50 000.
• 1 ≤ S k ≤ N (1 ≤ k ≤ Q).
• 1 ≤ T k ≤ N (1 ≤ k ≤ Q).
Task 4- 2 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
• S k , T k (1 ≤ k ≤ Q).
• (S k , T k ) , (S l , T l ) (1 ≤ k < l ≤ Q).
Subtasks
1. (8 points) N ≤ 300, M ≤ 300, Q ≤ 300.
2. (8 points) N ≤ 2 000, M ≤ 2 000,Q ≤ 2 000.
3. (11 points) Q = 1.
4. (25 points) K = N − 1.
5. (35 points) A j < B j (1 ≤ j ≤ M), S k < T k (1 ≤ k ≤ Q).
6. (13 points) No additional constraints.
In the first plan, JOI-kun travels from Station 5 to Station 3. For example, this plan is achieved if JOI-kun
takes a train of Line 1 at Station 5, and get out of the train at Station 3. In total, JOI-kun will take one train.
Since it is impossible to achieve the plan by taking less than one train, output 1 in the first line.
In the second plan, JOI-kun travels from Station 3 to Station 2. For example, this plan is achieved if JOI-kun
takes a train of Line 2 at Station 3, get out of the train at Station 4, takes a train of Line 1 at Station 4, and get
out of the train at Station 2. In total, JOI-kun will take two trains. Since it is impossible to achieve the plan by
taking less than two trains, output 2 in the second line.
In the third plan, JOI-kun travels from Station 2 to Station 1. Since it is impossible for JOI-kun to achieve
this plan, output -1 in the third line.
This sample input satisfies the constraints of Subtasks 1, 2, 6.
Task 4- 3 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Task 4- 4 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Task 4- 5 / 5
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
5 Sandcastle 2
JOI-kun is playing on a sand beach. He makes a sandcastle. The sandcastle made by JOI-kun is contained
in a rectangular region in the sand beach. The rectangular region consists of cells of H horizontal rows and W
vertical columns. The cell in the i-th row (1 ≤ i ≤ H) from the north and the j-th column (1 ≤ j ≤ W) from the
west has height Ai, j . Note that the values of Ai, j are different from each other.
To the sandcastle, JOI-kun performed the following actions.
1. First, JOI-kun chose a cell, and he started moving from the chosen cell.
2. Then, he moved from the current cell to an adjacent cell in one of the four direction. He had to move to
a cell which is lower than the current cell. He repeated this zero or more times.
Finally, if we view the cells he visited from above, the cells form a rectangle.
Given the information of the height Ai, j of each cell, write a program which calculates the number of possible
rectangles formed by the the cells JOI-kun visited.
Input
Read the following data from the standard input. Given values are all integers.
HW
A1,1 A1,2 · · · A1,W
A2,1 A2,2 · · · A2,W
..
.
AH,1 AH,2 · · · AH,W
Output
Write one line to the standard output. The output should contain the number of possible rectangles formed
by the cells JOI-kun visited.
Task 5- 1 / 3
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Constraints
• H ≥ 1.
• W ≥ 1.
• H × W ≤ 50 000.
• 1 ≤ Ai, j ≤ 10 000 000 (1 ≤ i ≤ H, 1 ≤ j ≤ W).
• Ai1 , j1 , Ai2 , j2 (1 ≤ i1 ≤ H, 1 ≤ j1 ≤ W, 1 ≤ i2 ≤ H, 1 ≤ j2 ≤ W, (i1 , j1 ) , (i2 , j2 )).
Subtasks
1. (9 points) H = 1.
2. (10 points) H × W ≤ 100.
3. (5 points) H × W ≤ 1 500.
4. (56 points) H × W ≤ 7 000.
5. (20 points) No additional constraints.
Since there are 10 possible rectangles formed by the cells JOI-kun visited, output 10.
2 4 7 1 5 2 4 7 1 5 2 4 7 1 5 2 4 7 1 5 2 4 7 1 5
2 4 7 1 5 2 4 7 1 5 2 4 7 1 5 2 4 7 1 5 2 4 7 1 5
Task 5- 2 / 3
The 21st Japanese Olympiad in Informatics (JOI 2021/2022)
Final Round
February 13, 2022 (Online)
Since there are 15 possible rectangles formed by the cells JOI-kun visited, output 15.
18 10 18 10 18 10 18 10 18 10 18 10 18 10 18 10
19 12 19 12 19 12 19 12 19 12 19 12 19 12 19 12
17 13 17 13 17 13 17 13 17 13 17 13 17 13 17 13
18 10 18 10 18 10 18 10 18 10 18 10 18 10
19 12 19 12 19 12 19 12 19 12 19 12 19 12
17 13 17 13 17 13 17 13 17 13 17 13 17 13
For example, the following rectangles can be formed by the cells JOI-kun visited. Since there are 65 possible
rectangles in total, output 65.
83 47 36 38 40 83 47 36 38 40 83 47 36 38 40
13 10 26 68 67 13 10 26 68 67 13 10 26 68 67
15 19 20 70 90 15 19 20 70 90 15 19 20 70 90
Task 5- 3 / 3