1337xyz1337xyz/codeforces-editorial-full-2026-07-16 huggingface dataset:
Inna and Large Sweet Matrix
Contest: Codeforces Round 229 (Div. 2)

Inna loves sweets very much. That's why she wants to play the "Sweet Matrix" game with Dima and Sereja. But Sereja is a large person, so the game proved small for him. Sereja suggested playing the "Large Sweet Matrix" game.

The "Large Sweet Matrix" playing field is an n × m matrix. Let's number the rows of the matrix from 1 to n, and the columns — from 1 to m. Let's denote the cell in the i-th row and j-th column as (i, j). Each cell of the matrix can contain multiple candies, initially all cells are empty. The game goes in w moves, during each move one of the two following events occurs:

1. Sereja chooses five integers x1, y1, x2, y2, v (x1 ≤ x2, y1 ≤ y2) and adds v candies to each matrix cell (i, j) (x1 ≤ i ≤ x2; y1 ≤ j ≤ y2).
2. Sereja chooses four integers x1, y1, x2, y2 (x1 ≤ x2, y1 ≤ y2). Then he asks Dima to calculate the total number of candies in cells (i, j) (x1 ≤ i ≤ x2; y1 ≤ j ≤ y2) and he asks Inna to calculate the total number of candies in the cells of matrix (p, q), which meet the following logical criteria: (p < x1 OR p > x2) AND (q < y1 OR q > y2). Finally, Sereja asks to write down the difference between the number Dima has calculated and the number Inna has calculated (D - I).

Unfortunately, Sereja's matrix is really huge. That's why Inna and Dima aren't coping with the calculating. Help them!

Input format:
The first line of the input contains three integers n, m and w (3 ≤ n, m ≤ 4·106; 1 ≤ w ≤ 105).

The next w lines describe the moves that were made in the game.

- A line that describes an event of the first type contains 6 integers: 0, x1, y1, x2, y2 and v (1 ≤ x1 ≤ x2 ≤ n; 1 ≤ y1 ≤ y2 ≤ m; 1 ≤ v ≤ 109).
- A line that describes an event of the second type contains 5 integers: 1, x1, y1, x2, y2 (2 ≤ x1 ≤ x2 ≤ n - 1; 2 ≤ y1 ≤ y2 ≤ m - 1).

It is guaranteed that the second type move occurs at least once. It is guaranteed that a single operation will not add more than 109 candies.

Be careful, the constraints are very large, so please use optimal data structures. Max-tests will be in pretests.

Output format:
For each second type move print a single integer on a single line — the difference between Dima and Inna's numbers.

Constraints:
Time limit: 1.0 s
Memory limit: 512.0 MB

Note:
Note to the sample. After the first query the matrix looks as:

After the second one it is:

After the third one it is:

For the fourth query, Dima's sum equals 5 + 0 + 3 + 0 = 8 and Inna's sum equals 4 + 1 + 0 + 1 = 6. The answer to the query equals 8 - 6 = 2. For the fifth query, Dima's sum equals 0 and Inna's sum equals 18 + 2 + 0 + 1 = 21. The answer to the query is 0 - 21 = -21.

Example input:
4 5 5
0 1 1 2 3 2
0 2 2 3 3 3
0 1 5 4 5 1
1 2 3 3 4
1 3 4 3 4

Example output:
2
-21

Editorial:
Next Round
Contest: VK Cup 2012 Qualification Round 1
Difficulty rating: 800
Tags: *special, implementation

"Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..." — an excerpt from contest rules.

A total of n participants took part in the contest (n ≥ k), and you already know their scores. Calculate how many participants will advance to the next round.

Input format:
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50) separated by a single space.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: ai ≥ ai + 1).

Output format:
Output the number of participants who advance to the next round.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.

In the second example nobody got a positive score.

Example input:
8 5
10 9 8 7 7 7 5 5

Example output:
6

Example input:
4 2
0 0 0 0

Example output:
0

Editorial:
Taxi
Contest: VK Cup 2012 Qualification Round 1
Difficulty rating: 1100
Tags: *special, greedy, implementation

After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?

Input format:
The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.

Output format:
Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first test we can sort the children into four cars like this:

- the third group (consisting of four children),
- the fourth group (consisting of three children),
- the fifth group (consisting of three children),
- the first and the second group (consisting of one and two children, correspondingly).

There are other ways to sort the groups into four cars.

Example input:
5
1 2 4 3 3

Example output:
4

Example input:
8
2 3 4 4 2 1 3 1

Example output:
5

Editorial:
Cd and pwd commands
Contest: VK Cup 2012 Qualification Round 1
Difficulty rating: 1400
Tags: *special, data structures, implementation

Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).

Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..".

The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.

The command pwd should display the absolute path to the current directory. This path must not contain "..".

Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.

Input format:
The first line of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands.

Then follow n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter.

The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive.

Directories in the file system can have the same names.

Output format:
For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Example input:
7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd

Example output:
/
/home/vasya/
/home/
/home/petya/

Example input:
4
cd /a/b
pwd
cd ../a/b
pwd

Example output:
/a/b/
/a/a/b/

Editorial:
Ice Sculptures
Contest: VK Cup 2012 Qualification Round 1
Difficulty rating: 1300
Tags: *special, brute force, number theory

The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus n ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they form a regular n-gon. They are numbered in clockwise order with numbers from 1 to n.

The site of the University has already conducted a voting that estimated each sculpture's characteristic of ti — the degree of the sculpture's attractiveness. The values of ti can be positive, negative or zero.

When the university rector came to evaluate the work, he said that this might be not the perfect arrangement. He suggested to melt some of the sculptures so that:

- the remaining sculptures form a regular polygon (the number of vertices should be between 3 and n),
- the sum of the ti values of the remaining sculptures is maximized.

Help the Vice Rector to analyze the criticism — find the maximum value of ti sum which can be obtained in this way. It is allowed not to melt any sculptures at all. The sculptures can not be moved.

Input format:
The first input line contains an integer n (3 ≤ n ≤ 20000) — the initial number of sculptures. The second line contains a sequence of integers t1, t2, ..., tn, ti — the degree of the i-th sculpture's attractiveness ( - 1000 ≤ ti ≤ 1000). The numbers on the line are separated by spaces.

Output format:
Print the required maximum sum of the sculptures' attractiveness.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 и 3.

Example input:
8
1 2 -3 4 -5 5 2 3

Example output:
14

Example input:
6
1 -2 3 -4 5 -6

Example output:
9

Editorial:
Phone Talks
Contest: VK Cup 2012 Qualification Round 1
Difficulty rating: 1900
Tags: *special, dp, sortings

Cool J has recently become a businessman Mr. Jackson, and he has to make a lot of phone calls now. Today he has n calls planned. For each call we know the moment ti (in seconds since the start of the day) when it is scheduled to start and its duration di (in seconds). All ti are different. Mr. Jackson is a very important person, so he never dials anybody himself, all calls will be incoming.

Mr. Jackson isn't Caesar and he can't do several things at once. If somebody calls him while he hasn't finished the previous conversation, Mr. Jackson puts the new call on hold in the queue. In this case immediately after the end of the current call Mr. Jackson takes the earliest incoming call from the queue and starts the conversation. If Mr. Jackson started the call at the second t, and the call continues for d seconds, then Mr. Jackson is busy at seconds t, t + 1, ..., t + d - 1, and he can start a new call at second t + d. Note that if Mr. Jackson is not busy talking when somebody calls, he can't put this call on hold.

Mr. Jackson isn't Napoleon either, he likes to sleep. So sometimes he allows himself the luxury of ignoring a call, as if it never was scheduled. He can ignore at most k calls. Note that a call which comes while he is busy talking can be ignored as well.

What is the maximum number of seconds Mr. Jackson can sleep today, assuming that he can choose an arbitrary continuous time segment from the current day (that is, with seconds from the 1-st to the 86400-th, inclusive) when he is not busy talking?

Note that some calls can be continued or postponed to the next day or even later. However, the interval for sleep should be completely within the current day.

Input format:
The first input line contains a pair of integers n, k (0 ≤ k ≤ n ≤ 4000) separated by a space. Following n lines contain the description of calls for today. The description of each call is located on the single line and consists of two space-separated integers ti and di, (1 ≤ ti, di ≤ 86400). All ti are distinct, the calls are given in the order of strict increasing ti.

Scheduled times of calls [ti, ti + di - 1] can arbitrarily intersect.

Output format:
Print a number from 0 to 86400, inclusive — the maximally possible number of seconds for Mr. Jackson to sleep today.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first sample the most convenient way is to ignore the first two calls.

In the second sample it is best to ignore the third call. In this case Mr. Jackson will have been speaking:

- first call: from 1-st to 20000-th second,
- second call: from 20001-st to 30000-th second,
- fourth call: from 30001-st to 40000-th second (the third call is ignored),
- fifth call: from 80000-th to 139999-th second.

Thus, the longest period of free time is from the 40001-th to the 79999-th second.

Example input:
3 2
30000 15000
40000 15000
50000 15000

Example output:
49999

Example input:
5 1
1 20000
10000 10000
20000 20000
25000 10000
80000 60000

Example output:
39999

Editorial:
Domino
Contest: Yandex.Algorithm 2011: Finals
Difficulty rating: 2400
Tags: brute force, implementation

Little Gennady was presented with a set of domino for his birthday. The set consists of 28 different dominoes of size 2 × 1. Both halves of each domino contain one digit from 0 to 6.

The figure that consists of 28 dominoes is called magic, if it can be fully covered with 14 non-intersecting squares of size 2 × 2 so that each square contained four equal numbers. Every time Gennady assembles a magic figure, some magic properties of the set appear — he wins the next contest. Gennady noticed that he can't assemble a figure that has already been assembled, otherwise someone else wins the contest.

Gennady chose a checked field of size n × m and put there rectangular chips of sizes 1 × 2 and 2 × 1. Each chip fully occupies exactly two neighboring squares of the field. Those chips do not overlap but they can touch each other. Overall the field has exactly 28 chips, equal to the number of dominoes in the set. Now Gennady wants to replace each chip with a domino so that a magic figure appeared as a result. Different chips should be replaced by different dominoes. Determine in what number of contests Gennady can win over at the given position of the chips. You are also required to find one of the possible ways of replacing chips with dominoes to win the next Codeforces round.

Input format:
The first line contains two positive integers n and m (1 ≤ n, m ≤ 30). Each of the following n lines contains m characters, which is the position of chips on the field. The dots stand for empty spaces, Latin letters from "a" to "z" and "A", "B" stand for the positions of the chips. There are exactly 28 chips on the field. The squares covered by the same chip are marked by the same letter, different chips are marked by different letters. It is guaranteed that the field's description is correct.

It is also guaranteed that at least one solution exists.

Output format:
Print on the first line the number of ways to replace chips with dominoes to get a magic figure. That is the total number of contests that can be won using this arrangement of the chips. Next n lines containing m characters each, should contain a field from dots and numbers from 0 to 6 — any of the possible solutions. All dominoes should be different.

Constraints:
Time limit: 0.5 s
Memory limit: 256.0 MB

Example input:
8 8
.aabbcc.
.defghi.
kdefghij
klmnopqj
.lmnopq.
.rstuvw.
xrstuvwy
xzzAABBy

Example output:
10080
.001122.
.001122.
33440055
33440055
.225566.
.225566.
66113344
66113344

Editorial:
Superset
Contest: Yandex.Algorithm 2011: Finals
Difficulty rating: 2300
Tags: constructive algorithms, divide and conquer

A set of points on a plane is called good, if for any two points at least one of the three conditions is true:

- those two points lie on same horizontal line;
- those two points lie on same vertical line;
- the rectangle, with corners in these two points, contains inside or on its borders at least one point of the set, other than these two. We mean here a rectangle with sides parallel to coordinates' axes, the so-called bounding box of the two points.

You are given a set consisting of n points on a plane. Find any good superset of the given set whose size would not exceed 2·105 points.

Input format:
The first line contains an integer n (1 ≤ n ≤ 104) — the number of points in the initial set. Next n lines describe the set's points. Each line contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — a corresponding point's coordinates. It is guaranteed that all the points are different.

Output format:
Print on the first line the number of points m (n ≤ m ≤ 2·105) in a good superset, print on next m lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize m, it is enough to find any good superset of the given set, whose size does not exceed 2·105.

All points in the superset should have integer coordinates.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
2
1 1
2 2

Example output:
3
1 1
2 2
1 2

Editorial:
Leaders
Contest: Yandex.Algorithm 2011: Finals
Difficulty rating: 2200
Tags: dfs and similar, dsu, graphs, trees

After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology — to make the leaders work in pairs.

A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple — we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.

Input format:
The first line contains integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 ≤ a, b ≤ n). It is guaranteed that the graph has no loops or multiple edges.

Next line contains number q (1 ≤ q ≤ 105) — the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).

Output format:
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Notes to the samples:

1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.

2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.

5) Vertices 1 and 5 are located in different connected components, there's no path between them.

Example input:
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7

Example output:
No
Yes
Yes
Yes
No
Yes
Yes
Yes

Editorial:
Short Program
Contest: Codeforces Round 443 (Div. 1)
Difficulty rating: 1600
Tags: bitmasks, constructive algorithms

Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.

In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.

Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023.

Input format:
The first line contains an integer n (1 ≤ n ≤ 5·105) — the number of lines.

Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 ≤ xi ≤ 1023.

Output format:
Output an integer k (0 ≤ k ≤ 5) — the length of your program.

Next k lines must contain commands in the same format as in the input.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
You can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.

Second sample:

Let x be an input of the Petya's program. It's output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.

Example input:
3
| 3
^ 2
| 1

Example output:
2
| 3
^ 2

Example input:
3
& 1
& 3
& 5

Example output:
1
& 1

Editorial:
Let's see what happens with a single bit. All operations work with each bit separately, so each bit of output depends only on the corresponding bit of input.
There are only four options: bit doesn't change, bit always changes, bit is set to 0, bit is set to 1. For each bit it's easy to find which of these options happens to it.
Now let's write a program of three lines:
1. Use XOR with a number that has ones in bits that must be reversed.
2. Use OR with a number that has ones in bits which must be set to 1.
3. Use AND with a number that has zeroes in bits that must be set to 0.
Depending on the implementation, it may works in O(n) or $${\mathcal O}(n\log X)$$.
BONUS: solve a problem using at most two commands.