Spring Lyceum Second school olympiad in informatics 2025
A. The Ultimate Punishment
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Our teacher is, as usual, late for the first lesson... Of course, someone opened the classroom, but the resentment towards the teacher, who keeps repeating his mistakes, is only growing. It's time to teach our teacher a lesson!

Suddenly, someone had the idea to rummage through the closet. There turned out to be $$$n$$$ sticks from a broken cross, gifted for the New Year. To scare and punish the culprit, something terrifying needs to be created—a non-degenerate triangle$$$^{\text{∗}}$$$ with a huge perimeter!

Since doing this manually is difficult, help us understand what the maximum perimeter of a triangle can be obtained, or $$$-1$$$ if it is impossible to form any triangle. Two sticks cannot form one side. That is, for example, with sticks $$$1,1,2,2$$$, it is not possible to make a triangle with sides $$$2$$$, $$$2$$$, and $$$1+1$$$.

$$$^{\text{∗}}$$$A triangle with sides $$$(a,b,c)$$$ is considered non-degenerate if the following system of inequalities holds: $$$ \left\{ \begin{array}{c} a + b \gt c \\ b + c \gt a \\ c + a \gt b \end{array}\right.$$$

Input

The first line of input contains a single integer $$$n$$$ ($$$3 \le n \le 10^5$$$) — the number of sticks found in the closet.

The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — the lengths of the sticks found in the closet.

Output

Output a single line with the maximum perimeter that can be obtained, or $$$-1$$$ if it is impossible to form any triangle.

Scoring
Additional ConstraintsPointsRequired GroupsComment
$$$0$$$Tests from the statement
$$$1$$$$$$n \le 200$$$$$$39$$$
$$$2$$$$$$n \le 1000$$$$$$22$$$$$$1$$$
$$$3$$$$$$39$$$$$$0-2$$$
Examples
Input
5
3 6 2 7 4
Output
17
Input
3
1 2 9
Output
-1

B. Spaceship
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a set of $$$n$$$ weights, where $$$a_i$$$ is the weight of the $$$i$$$-th weight. In the store, there is an infinite number of products of each natural weight.

You only have balance scales in your store, where you can place products on the right pan and weights on the left pan. Your task is to determine the maximum weight $$$p$$$ such that any product in the store that weighs no more than $$$p$$$ can be uniquely (that is, you can guarantee to say what the weight of a particular product is) determined using the given balance scales for an unlimited number of weighings.

Input

The first line of input contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of weights at your disposal.

The second line of input contains $$$n$$$ numbers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — the weights available to you.

Output

In a single line of output, you need to print one number, the maximum weight $$$p$$$. If you cannot uniquely determine the weight of any product, you should output $$$0$$$.

Scoring
Add. constraintsPointsReq. groupsComment
$$$n$$$$$$a_i$$$
$$$0$$$Tests from the statement
$$$1$$$$$$a_i \le i$$$$$$8$$$
$$$2$$$$$$n \le 18$$$$$$12$$$
$$$3$$$$$$n \le 100$$$$$$\sum a_i \le 10^4$$$$$$17$$$
$$$4$$$$$$n \le 1000$$$$$$a_i \le 1000$$$$$$36$$$
$$$5$$$$$$27$$$$$$0-4$$$
Examples
Input
4
4 2 3 1
Output
10
Input
2
1 3
Output
4

C. 28 stab wounds
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem

Imagine that you find yourself as an investigator in a well-known game. You need to expose $$$n$$$ deviants who have committed outrageous crimes. Each android $$$i$$$ has a punishment measure $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$, $$$a_i$$$ is an integer) and a stress parameter $$$c_i$$$, initially equal to zero. Since we have little time, you will conduct a parallel interrogation. With one question, you can shout aloud (all non-confessing deviants will hear your statement) "$$$X$$$ STAB WOUNDS! You didn't want to leave him a chance, huh?" Each deviant perceives this statement personally and starts to think:

  • If $$$X \gt a_i$$$, the deviant will be sure that you do not know that he committed a crime and are just guessing, so after this, he will stop responding to any questions.
  • If $$$X \le a_i$$$, the deviant receives $$$X$$$ units of stress because he is sure that you are about to uncover his sins. That is, $$$c_i := c_i + X$$$ (the value of $$$c_i$$$ increases by $$$X$$$).
After any question, if the stress level $$$c_i$$$ is at least the measure of punishment, he will confess and start giving up everyone, but he will do it cunningly: he will name the sum of the punishments of himself and another deviant. More formally, when it holds that $$$c_i \ge a_i$$$, android $$$i$$$ will say for each accomplice $$$j$$$ the number $$$a_i + a_j$$$, $$$i \neq j$$$.

When a deviant confesses, he leaves the interrogation room and does not hear any further questions.

Your task is to ensure that all deviants confess, and also to find out their measures of punishment. If at least one deviant does not confess, you will receive a Wrong Answer verdict even if you output the correct measures of punishment. Also, in some subgroups, you will not be required to find out the measures of punishment; it is enough to ensure that everyone confesses.

Input

A single line contains three integers $$$n$$$, $$$k$$$, $$$t$$$ ($$$3 \le n \le 100$$$, $$$30 \le k \le 1000$$$, $$$0 \le t \le 1$$$) — the number of deviants in the precinct, the maximum number of questions that can be asked, and a parameter that determines whether you need to output all measures of punishment or just ensure that everyone confesses.

Interaction

In this problem, you can ask questions of two types:

  • "$$$?$$$ $$$X$$$" ($$$1 \le X \le 10^9$$$). You make an accusation with $$$X$$$ stabs.

    After each query, the interactor returns the following data:

    In the first line, the number $$$m$$$ ($$$0 \le m \le n$$$), the number of deviants who want to confess.

    In the next $$$m$$$ lines, $$$n + 1$$$ numbers are given: $$$j$$$ and $$$n$$$ numbers $$$b_i$$$, where $$$b_i = a_j + a_i$$$ for all $$$j \neq i$$$. For $$$i = j$$$, $$$b_i = 0$$$. The number of the confessing deviant and information about accomplices, respectively.

    It is guaranteed that each deviant, if they confess, will confess only once.

  • "$$$!$$$ $$$a_1\ a_2\ \dots\ a_n$$$". This question means that you are ready to state all measures of punishment, and the interrogation ends, and your program must terminate immediately. If not all deviants confessed, you will receive a Wrong Answer verdict. All numbers you output $$$a_i$$$ must be integers, and it must also hold that $$$0 \le a_i \le 10^9$$$ ($$$0$$$ means that you do not know the measure of punishment). If at least one number is not an integer or outside this range, you will receive a Wrong Answer verdict. Also, if $$$t = 1$$$ and you output at least one incorrect measure of punishment, you will receive a Wrong Answer verdict.

    After making this query, your program must terminate.

You can make no more than $$$k$$$ queries of the first type and only one query of the second type. Even if you made all androids confess, but cannot determine their measures of punishment, the program must terminate with a query of the second type.

The interactor in this problem is non-adaptive.

After outputting each query, do not forget to output a newline and flush the output buffer. Otherwise, you will receive a verdict of "Idleness Limit Exceeded" To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • sys.stdout.flush() in Python;
  • see the documentation for other languages.
Scoring
Additional constraintsPointsRequired groupsComment
$$$k$$$$$$a_i$$$$$$t$$$
$$$0$$$Tests from the statement
$$$1$$$$$$k = \max(n, 30)$$$$$$t=0$$$$$$6$$$$$$a_1 = 1$$$
$$$2$$$$$$t=1$$$$$$7$$$$$$1$$$
$$$3$$$$$$k = 1000$$$$$$a_i \le 1000$$$$$$ t=0$$$$$$9$$$
$$$4$$$$$$t=1$$$$$$10$$$$$$3$$$
$$$5$$$$$$a_i \le 5 \cdot 10^5$$$$$$t = 0$$$$$$10$$$$$$3$$$
$$$6$$$$$$t=1$$$$$$11$$$$$$3-5$$$
$$$7$$$$$$k = 30$$$$$$t=0$$$$$$8$$$$$$a_i$$$ — power of two
$$$8$$$$$$t=1$$$$$$8$$$$$$7$$$
$$$9$$$$$$t=0$$$$$$15$$$$$$1,3,5,7$$$
$$$10$$$$$$t=1$$$$$$16$$$$$$0-9$$$
Examples
Input
4 30 0

1
1 0 8 7 9

3
2 8 0 9 11
3 7 9 0 10
4 9 11 10 0
Output

? 3


? 3




! 0 0 0 0
Input
4 30 1

1
1 0 8 7 9

3
2 8 0 9 11
3 7 9 0 10
4 9 11 10 0
Output

? 3


? 3




! 3 5 4 6
Note

Let's analyze what happens in the example:

First, "3 stabs" was shouted. The first deviant confessed since his stress reached his measure of punishment ($$$0 + 3 \ge 3$$$). The stress of all androids: $$$3$$$, $$$3$$$, $$$3$$$, $$$3$$$.

Next, "3 stabs" was shouted again. All deviants confessed because their stresses reached $$$6$$$. Accordingly, $$$3 + 3 \ge 5$$$, $$$3 + 3 \ge 4$$$, $$$3 + 3 \ge 6$$$ for the second, third, and fourth deviants.

Since everyone confessed, we can output the measures of punishment. They turned out to be $$$3$$$, $$$5$$$, $$$4$$$, and $$$6$$$.

Note that empty lines are left only for visual interaction; they do not need to be output.

D. Transportation Reform
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Buses on Leninsky Prospekt have started to run infrequently. Mark has become a victim of this transportation reform and is constantly late for school because of it. Nobody liked this, not even Mark, so he decided to optimize his route in other ways. Specifically, by using scooters.

It is known that Mark walks at a speed of $$$a$$$ m/s, while the scooter travels at a speed of $$$b$$$ m/s ($$$b \gt a$$$). Mark chooses a scooter as follows: the app suggests a circle centered at Mark's location with a minimum radius that contains at least one scooter, and marks all the scooters on this circle (there may be multiple scooters at some points). Mark selects one such scooter so that the total time walking to it and riding the scooter to school is minimized. If walking turns out to be no worse, Mark will walk; otherwise, he will ride that scooter.

Mark liked his method of traveling to school, so he decided to use it not only for school but also for other points in the city. Specifically, on $$$q$$$ days, he needs to get to the point ($$$x_i, y_i$$$). He lives at the point ($$$0, 0$$$). On each of the days, the circle and scooters will be the same (as companies return scooters to their places). There will always be $$$n$$$ scooters at the same distance from ($$$0, 0$$$). For each of the days, output the travel time from Mark's home to the respective point.

Input

The first line contains four integers $$$n,q,a,b$$$ ($$$1 \le n \le 10^5, 1 \le q \le 2 \cdot 10^5$$$, $$$1 \le a \lt b \le 10^9$$$) — the number of scooters that the app shows to Mark, the number of days Mark has to travel, the speed of walking, and the speed of riding the scooter, respectively.

The next $$$n$$$ lines contain two integers $$$x_j, y_j$$$ ($$$-10^9 \le x_j,y_j \le 10^9$$$) — the point where the $$$j$$$-th scooter is located.

The next $$$q$$$ lines contain two integers $$$x_i, y_i$$$ ($$$-10^9 \le x_i,y_i \le 10^9$$$) — the point Mark needs to reach on the $$$i$$$-th day.

Output

In $$$q$$$ lines, output the travel time for Mark from home to the respective point.

Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{\left|a-b\right|}{\max(1, |b|)}$$$.

Scoring
Additional ConstraintsPointsReq. GroupsComment
$$$n$$$$$$q$$$
$$$0$$$Tests from the statement
$$$1$$$$$$n \le 1000$$$$$$q \le 1000$$$$$$29$$$$$$0$$$
$$$2$$$$$$24$$$$$$|x|, |y| \le 20$$$
$$$3$$$$$$15$$$$$$x,y \ge 0$$$
$$$4$$$$$$13$$$$$$3$$$$$$y \ge 0$$$
$$$5$$$$$$19$$$$$$0-4$$$
Examples
Input
2 4 1 3
-3 4
4 3
0 -7
8 6
-6 8
0 15
Output
7.00000000000000000000
6.66666666666666666652
6.66666666666666666652
8.80058475033045972680
Input
3 4 1 2
0 10
0 10
0 -10
3 4
0 15
0 -15
20 0
Output
5.00000000000000000000
12.50000000000000000000
12.50000000000000000000
20.00000000000000000000

E. RADiant queries
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let rad$$$(n)$$$ denote the product of all distinct prime divisors of the number $$$n$$$. For example, rad$$$(504)$$$ = rad$$$(2^3 \cdot 3^2 \cdot 7)$$$ $$$= 2 \cdot 3 \cdot 7$$$ $$$=42$$$. We define rad$$$(1) = 1$$$.

The statement of this problem is simple: you have an array $$$a$$$ of size $$$n$$$. You are given $$$q$$$ queries $$$[\ell;r]$$$, and you need to compute rad of the product of the numbers $$$a_\ell, a_{\ell + 1}, \dots, a_r$$$, that is: $$$$$$\displaystyle\texttt{rad}\left(\prod_{i=\ell}^{r} a_i\right) = \texttt{rad}\left(a_\ell \times a_{\ell + 1} \times \dots \times a_r \right)$$$$$$ Since this number can be quite large, output it modulo $$$10^9 + 7$$$.

Input

The first line of input contains two numbers $$$n,q$$$ ($$$1 \le n,q \le 5 \cdot 10^5$$$), the number of elements in the array and the number of queries.

The second line of input contains $$$n$$$ numbers $$$a_i$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$), the array $$$a$$$.

In the following $$$q$$$ lines, two numbers $$$\ell, r$$$ ($$$1 \le \ell \le r \le n$$$) are given, which are the boundaries of the next query.

Output

In $$$q$$$ lines of output, print one number per line, the answer to the problem modulo $$$10^9 + 7$$$.

Scoring
Additional ConstraintsPointsReq. GroupsComment
$$$n$$$$$$q$$$$$$a_i$$$
$$$0$$$Tests from the statement
$$$1$$$$$$n \le 100$$$$$$q \le 100$$$$$$a_i \le 100$$$$$$8$$$$$$0$$$
$$$2$$$$$$9$$$$$$1$$$
$$$3$$$$$$n \le 1000$$$$$$q \le 1000$$$$$$a_i \le 1000$$$$$$10$$$$$$1$$$
$$$4$$$$$$11$$$$$$1-3$$$
$$$5$$$$$$11$$$All $$$a_i$$$ are prime and distinct
$$$6$$$$$$a_i \le 300$$$$$$12$$$$$$0-2$$$
$$$7$$$$$$n \le 5 \cdot 10^4$$$$$$q \le 5 \cdot 10^4$$$$$$7$$$$$$0,1,3$$$
$$$8$$$$$$n \le 10^5$$$$$$q \le 10^5$$$$$$4$$$$$$7$$$
$$$9$$$$$$n \le 2 \cdot 10^5$$$$$$q \le 2 \cdot 10^5$$$$$$4$$$$$$8$$$
$$$10$$$$$$n \le 3 \cdot 10^5$$$$$$q \le 3 \cdot 10^5$$$$$$3$$$$$$9$$$
$$$11$$$$$$n \le 4 \cdot 10^5$$$$$$q \le 4 \cdot 10^5$$$$$$2$$$$$$10$$$
$$$12$$$$$$7$$$$$$5$$$All $$$a_i$$$ are prime
$$$13$$$$$$12$$$$$$0-12$$$
Examples
Input
5 6
42 35 11 26 13
1 3
2 4
3 5
1 5
2 2
4 5
Output
2310
10010
286
30030
35
26
Input
2 1
2 2
1 2
Output
2

F. The Heist of the Century
time limit per test
0.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Please note the low time limit. Solutions in Python should be submitted under PyPy 3-64.

Welcome, aspiring thieves! Today, you are tasked with robbing room number $$$25$$$. In your gear, we will only provide you with a small backpack, as there isn't much valuable stuff at the destination. However, even such items can be useful for the Headquarters! Yes, the backpack is a bit old, but it can hold a lot of useful and necessary items for the Headquarters! Periodically, you will receive queries from the base. The queries can be as follows:

  1. + x. You are instructed to pick up and put a chocopie worth $$$x$$$ burleys into the backpack.
  2. - x. You urgently need to throw out any chocopie worth $$$x$$$ burleys from the backpack to avoid being caught by Kirill Evgenievich. The Headquarters closely monitors the contents of your backpack, so you must always throw out a chocopie that is present in the backpack.
  3. ? W. Vladimir Evgenievich is nearby, and he will consider the backpack suspicious if the total value of the chocopies in it exceeds $$$W$$$. The base wants to know the maximum value of chocopies that can hypothetically be left in the backpack after throwing out some chocopies. Chocopies are not thrown out after the query is made.
During the process, you cannot keep the stolen goods, so if you get caught, you won't easily get rid of the traces of the crime. We suggest you practice robbing the room in this task so that you don't mess up on the main mission. Good luck, the Headquarters is counting on you!
Input

The first line contains two numbers $$$q$$$ and $$$g$$$ ($$$1 \le q \le 10^4$$$, $$$0 \le g \le 10$$$) — the number of queries from the base and the test group number.

In the following $$$q$$$ lines, a queries is entered in the corresponding format:

  1. + x. $$$(1 \le x \le 10^4)$$$
  2. - x. $$$(1 \le x \le 10^4)$$$. It is guaranteed that $$$x$$$ is already in your backpack.
  3. ? W. $$$(0 \le W \le 10^4)$$$
Output

For each query of type ? from the base, output the maximum value of the chocopies that can be left.

Scoring
Additional constraintsPointsRequired groupComment
$$$q$$$$$$W$$$
$$$0$$$Tests from the statement
$$$1$$$$$$q \le 16$$$$$$10$$$$$$0$$$
$$$2$$$$$$q \le 32$$$$$$12$$$$$$0-1$$$
$$$3$$$$$$q \le 300$$$$$$W \le 200$$$$$$7$$$
$$$4$$$$$$7$$$All queries of type $$$1$$$ and $$$2$$$ come before all queries of type $$$3$$$
$$$5$$$$$$8$$$$$$4$$$All queries of type $$$2$$$ come before all queries of type $$$3$$$
$$$6$$$$$$11$$$$$$4$$$All queries of type $$$1$$$ come before all queries of type $$$3$$$
$$$7$$$$$$9$$$All values for removal come in reverse order of addition
$$$8$$$$$$12$$$All values for removal come in the same order as addition
$$$9$$$$$$q \le 2000$$$$$$8$$$$$$0-3$$$
$$$10$$$$$$16$$$$$$0-9$$$
Examples
Input
10 0
+ 5
+ 6
+ 1
+ 2
- 2
? 12
? 7
+ 2
- 5
? 10
Output
12
7
9
Input
14 0
+ 1
+ 1
+ 1
? 5
? 4
? 3
? 2
+ 2
+ 2
? 100
- 1
? 100
- 1
? 100
Output
3
3
3
2
7
6
5

G. Binary Automaton
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
This is where the fun begins
— Anakin Skywalker

Mark found an old automaton with two buttons in his attic, which he used to play with as a child. The toy is quite rusty, but it still performs its function. When the first button is pressed, a single $$$0$$$ appears on the screen, and when the other button is pressed, either due to age or malfunction, the automaton outputs $$$k$$$ ones at once.

Mark became curious about how many different strings of lengths from $$$\ell$$$ to $$$r$$$ can be obtained if the automaton outputs $$$k$$$ ones when the second button is pressed. However, since the automaton is old and Mark's curiosity is great, you will have to answer $$$q$$$ queries instead of the automaton.

Since the answer to any of Mark's queries can be very large, compute it modulo $$$998244353$$$.

Input

In the first line, you are given $$$2$$$ integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the maximum length of the string and the number of queries.

In the next $$$q$$$ lines, three integers $$$\ell_i, \ r_i, \ k_i$$$ are given ($$$1 \le \ell_i \le r_i \le n, 1 \le k \le n$$$) — the range of lengths and the number of ones that the automaton prints.

Output

For each query, output the number of strings that the automaton can print modulo $$$998244353$$$.

Scoring
Add. ConstraintsPointsReq. GroupsComment
$$$n$$$$$$q$$$$$$k$$$
$$$0$$$Tests from the statement
$$$1$$$$$$n \le 15$$$$$$q \le 15$$$$$$7$$$$$$0$$$
$$$2$$$$$$n \le 15$$$$$$9$$$$$$0 - 1$$$
$$$3$$$$$$n \le 5000$$$$$$q \le 5000$$$$$$11$$$$$$0 -1$$$
$$$4$$$$$$n \le 5000$$$$$$8$$$$$$0-3$$$
$$$5$$$$$$k \le 20$$$$$$9$$$$$$0-2$$$
$$$6$$$$$$12$$$$$$\ell_i = r_i = n$$$
$$$7$$$$$$20 \cdot k \ge n$$$$$$13$$$
$$$8$$$$$$n \le 50000$$$$$$q \le 50000$$$$$$21$$$$$$0, 1, 3$$$
$$$9$$$$$$10$$$$$$0-8$$$
Example
Input
8 6
1 1 1
4 8 2
1 8 3
1 8 1
4 6 2
4 6 4
Output
2
81
39
510
26
9

The Beautiful City $$$\mathbb{S}$$$
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Since time immemorial, a delegation from our lyceum has been traveling to the beautiful city $$$\mathbb{S}$$$ to participate in a well-known olympiad. Let's dive into the year when our school team first arrived in this city. At that time, the infrastructure was not so developed, the streets were filled with intellectuals, and parts of the city were connected by boats. However, traveling by boat is not a cheap pleasure, so the local government decided to establish the construction of bridges between the islands that make up the city.

Since building a bridge that lasts forever is quite challenging, bridges in the city $$$\mathbb{S}$$$ are constructed every year. In year number $$$i$$$, starting from the first arrival of the lyceum students in this city, a new bridge is built between islands $$$u_i$$$ and $$$v_i$$$. Some bridges are so impressive that they are called important. A bridge $$$v_i \leftrightarrow u_i$$$ is considered important if the islands $$$u_i$$$ and $$$v_i$$$ become disconnected upon its removal; in other words, it is impossible to reach island number $$$u_i$$$ from $$$v_i$$$ using any bridges without using the direct bridge between them.

As time goes on, more majestic and beautiful bridges are built, and the old ones inevitably cease to be important. The government of the city $$$\mathbb{S}$$$ became interested in how many years a particular bridge was important. Since the government of the city $$$\mathbb{S}$$$ mostly deals with bureaucratic matters, this task falls to you. For each bridge, output the number of years it will be important, or $$$-1$$$ if it remains important indefinitely.

Input

The first line of input contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 5 \cdot 10^5$$$), the number of islands in the city $$$\mathbb{S}$$$ and the number of years during which bridges are built.

In the following $$$m$$$ lines, two integers $$$v_i, u_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \neq v_i$$$) are given, representing the numbers of the islands connected in year $$$i$$$.

It is guaranteed that after the construction of all bridges, it is possible to reach any island from any other. Also, at any moment in time, no more than one bridge directly connects any two islands.

Output

For each of the $$$m$$$ bridges, output in a new line how many years the respective bridge was important. If it ultimately remains important, output «$$$-1$$$» without quotes.

Scoring
Add. constraintsPointsReq. groupsComment
$$$n$$$$$$m$$$
$$$0$$$Tests from the statement
$$$1$$$$$$m = n - 1$$$$$$7$$$
$$$2$$$$$$n \le 300$$$$$$m \le 300$$$$$$27$$$$$$0$$$
$$$3$$$$$$n \le 1000$$$$$$m \le 1000$$$$$$26$$$$$$0,2$$$
$$$4$$$$$$n \le 1000$$$$$$40$$$$$$0,2,3$$$
$$$5$$$$$$m \le 10^5$$$$$$32$$$$$$0,2,3$$$
$$$6$$$$$$18$$$$$$0-5$$$
Examples
Input
5 5
4 5
1 2
3 4
2 3
1 4
Output
-1
3
2
1
0
Input
2 1
1 2
Output
-1

I. Another task on parentheses sequences
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A correct parentheses sequence is a string that consists only of the characters «(» and «)», from which it is possible to obtain a valid arithmetic expression by inserting the characters «+» and «1». For example, «», «(())» and «()()» are correct, while «)(» and «(()» are not correct parentheses sequences. A simple parentheses sequence is defined as a string made up of the characters «(» and «)».

We will define the correctness of a parentheses sequence as the maximum length of its correct parentheses subsequence. For example, consider the string «()())((())()». Its correct subsequences include, for example, the following: «$$$\color{red}{\underline{\color{red}{\text{()}}}}$$$())((())()», «$$$\color{red}{\underline{\color{red}{\text{(}}}}$$$)$$$\color{red}{\underline{\color{red}{\text{(}}}}$$$))((($$$\color{red}{\underline{\color{red}{\text{))}}}}$$$()», «$$$\color{red}{\underline{\color{red}{\text{()()}}}}$$$)$$$\color{red}{\underline{\color{red}{\text{((())}}}}$$$($$$\color{red}{\underline{\color{red}{\text{)}}}}$$$», as well as others (but the problem statement is too short to list them all). As can be seen, a subsequence is our sequence from which zero or more elements have been removed, and the order of the remaining elements has not changed. For this sequence, the correctness will be equal to $$$10$$$ (the last of the provided subsequences has this length).

Given $$$n$$$ initially empty parentheses sequences. There are $$$q$$$ queries of two types:

  • «$$$1 \ \ell \ r \ x$$$» — append $$$|x|$$$ parentheses to the sequences in the segment $$$[\ell, r]$$$. If $$$x \gt 0$$$, then opening parentheses, if $$$x \lt 0$$$ — closing parentheses.
  • «$$$2 \ \ell \ r$$$» — output the sum of the correctness of the sequences in the segment $$$[\ell, r]$$$.
Input

The first line of input contains two numbers $$$n$$$, $$$q$$$ ($$$1 \le n \le 5 \cdot 10^5$$$, $$$1 \le q \le 5 \cdot 10^5$$$) — the number of empty parentheses sequences and the number of queries.

In the next $$$q$$$ lines, numbers are entered according to the format:

  • Four numbers «$$$1 \ \ell \ r \ x$$$» are entered, ($$$1 \le \ell,r \le n$$$, $$$1 \le |x| \le 10^6$$$)
  • Three numbers «$$$2 \ \ell \ r$$$» are entered, ($$$1 \le \ell,r \le n$$$)
Output

For each query of the second type, output the sum of the correctness of the sequences in the corresponding segment.

Scoring
Additional constraintsPointsRequired groupsComment
$$$n$$$$$$q$$$
$$$0$$$Tests from the problem statement
$$$1$$$$$$n=1$$$$$$6$$$
$$$2$$$$$$15$$$$$$\ell_i = r_i$$$ in queries of the first type
$$$3$$$$$$13$$$The balance$$$^{\text{∗}}$$$ of all parentheses sequences does not fall below $$$0$$$
$$$4$$$$$$n \le 10^4 $$$$$$q \le 10^4 $$$$$$7$$$
$$$5$$$$$$n \le 5 \cdot 10^4 $$$$$$q \le 5 \cdot 10^4 $$$$$$5$$$$$$4$$$
$$$6$$$$$$n \le 10^5 $$$$$$q \le 10^5 $$$$$$14$$$$$$5$$$
$$$7$$$$$$n \le 2 \cdot 10^5 $$$$$$q \le 2 \cdots 10^5 $$$$$$10$$$$$$6$$$
$$$8$$$$$$n \le 3 \cdot 10^5 $$$$$$q \le 3 \cdot 10^5 $$$$$$8$$$$$$7$$$
$$$9$$$$$$n \le 4 \cdot 10^5 $$$$$$q \le 4 \cdot 10^5 $$$$$$8$$$$$$8$$$
$$$10$$$$$$14$$$$$$0-9$$$

$$$^{\text{∗}}$$$The balance of a parentheses sequence is defined as the number of opening parentheses minus the number of closing parentheses.

Example
Input
1 11
1 1 1 1
2 1 1
1 1 1 -1
2 1 1
1 1 1 1
1 1 1 -2
1 1 1 3
1 1 1 -2
1 1 1 1
1 1 1 -1
2 1 1
Output
0
2
10
Note

The queries in the example form a string explaining the correctness.