Infoleague Winter 2021 Training Round
A. Mountains
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

In the wonderful mountain ranges of the United States of Berland there are $$$n$$$ peaks. Each peak is characterized by 3 integers: $$$h$$$, $$$l$$$ and $$$r$$$. Peak $$$i$$$ is currently $$$h_i$$$ meters tall and its height can vary within a year by any integer $$$\Delta h \in [l_i,r_i]$$$.

Formally, if at the start of year $$$k$$$ the height of peak $$$i$$$ was $$$h_k$$$, at the end of that year the height of peak $$$i$$$ can be any integer in the interval $$$[h_k+l_i,h_k+r_i]$$$. It is possible for some peaks to reach negative heights (aka become sinkholes) after some time.

For each mountain $$$i$$$ you are given $$$h_i$$$, $$$l_i$$$ and $$$r_i$$$. Find the maximum possible number of peaks with equal heights at the beginning of year $$$y$$$ and the number of distinct heights reachable by this maximum number of peaks.

Input

The first line of input contains two integers $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$, the number of peaks and $$$y$$$ $$$(0 \le y \le 10^9)$$$, the number of years. Each of the next $$$n$$$ lines of input contain 3 integers $$$h_i,l_i$$$ and $$$r_i$$$, describing the peaks $$$(-10^9 \le l_i \le r_i \le 10^9$$$ and $$$1 \le h_i \le 10^9)$$$.

Output

Print two integers, the maximum number of peaks that can reach the same height after $$$y$$$ years, and the number of distinct heights that can be reached by the maximum number of peaks.

Scoring
  • Subtask 1 (10 points): $$$y=0$$$;
  • Subtask 2 (30 points): $$$1 \le n,y \le 100$$$, $$$-100 \le l_i \le r_i \le 100$$$, $$$1 \le h_i \le 100$$$;
  • Subtask 3 (20 points): $$$y=1$$$;
  • Subtask 4 (40 points): No further constraints.
Examples
Input
3 2
3000 -250 600
4000 0 50
5000 -500 250
Output
3 101
Input
5 1
1000 0 1000
3000 -1000 0
5000 0 1000
7000 -1000 0
9000 -9000 0
Output
3 2
Input
7 0
2000 -1000 1000
1000 -1000 1000
3000 -1000 1000
1000 -1000 1000
4000 -1000 1000
2000 -1000 1000
3000 -1000 1000
Output
2 3
Note
  • In the first sample, the three peaks can reach any of the heights from $$$4000$$$ to $$$4100$$$, in total $$$101$$$ possible heights.
  • In the second sample, the peaks indexed $$$1,2$$$ and $$$5$$$ can reach height $$$2000$$$, and the peaks indexed $$$3,4$$$ and $$$5$$$ can reach height $$$6000$$$.
  • In the third sample, $$$y=0$$$, and there are two peaks with each of the heights $$$1000$$$, $$$2000$$$ and $$$3000$$$.

B. Antigo
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Eu nu strivesc corola de minuni a lumii

şi nu ucid

cu mintea tainele, ce le-ntâlnesc

în calea mea

în flori, în ochi, pe buze ori morminte.

Lumina altora

sugrumă vraja nepătrunsului ascuns

în adâncimi de întuneric,

dar eu,

eu cu lumina mea sporesc a lumii taină -

şi-ntocmai cum cu razele ei albe luna

nu micşorează, ci tremurătoare

măreşte şi mai tare taina nopţii,

aşa îmbogăţesc şi eu întunecata zare

cu largi fiori de sfânt mister

şi tot ce-i neînţeles

se schimbă-n neînţelesuri şi mai mari

sub ochii mei-

căci eu iubesc

şi flori şi ochi şi buze şi morminte.

— Lucian Blaga, Eu nu strivesc corola de minuni a lumii

Recently, the International Olympiad in Antigo has been announce to be held this year, and Aznurf has begun training for it. Antigo is a game similar to Go, only that it is played on a finite rectangular surface on an infinite table. For simplicity, the entire table is an xOy cartezian plane, and before each round, the players are announced that they will play with the points in the rectangular area between lower-left corner $$$(x1,y1)$$$ and upper-right $$$(x2,y2)$$$ (i.e. any point defined by $$$(x,y)$$$ such that $$$x1 \le x \le x2$$$ and $$$y1 \le y \le y2$$$ will be considered inside the area). With few fays before the championship, Aznurf found the key to the perfect opening move: any point defined by $$$(A,B)$$$ is given a value, which is either 1 if the point is "valorous" or 0 otherwise. If F(A,B) is the value of this point, then:

$$$F(A,B) = \left\{ \begin{array}{ c l } 1 & \quad \textrm{if } A=0 \textrm{ and } B=0 \\ F(A/5,B/5) & \quad \textrm{if } (A \textrm{ mod } 5) \textrm{ mod } 3 = (B \textrm{ mod } 5) \textrm{ mod } 3 \\ 0 & \quad \textrm{otherwise} \end{array} \right.$$$

One can reconsider this process as if both numbers are first converted to base 5, then after putting one above the other, each pair of corresponding digits are congruent modulo 3. Such an example of a point with value 1 would be (11206,1399), because $$$11206=324311_{(5)}, 1399=021044_{(5)}$$$ and $$$3 \equiv 0, 2 \equiv 2, 4 \equiv 1, 3 \equiv 0, 1 \equiv 4, 1 \equiv 4$$$

An hour before the contest, Aznurf gave into pressure saying "I want to start playing some game that is at least respected, like chess" , so now you're the only one in the national team.

After the competition, you made a fool of yourself. You're now curios in how many ways for each game in how many ways you could've opened the game, respecting the rules of Aznurf.

Input

On the first line you have Q, the number of areas you have. Then, on each of the following Q lines, you are given $$$x1_{i},y1_{i},x2_{i},y2_{i}$$$, representing the i-th query

Output

For each query, print the sum of the values of the points inside the respective rectangle

Scoring

Restrictions:

$$$1\le Q \le 1\,000$$$

$$$1 \le x1 \le x2 \le 10^{18}$$$

$$$1 \le y1 \le y2 \le 10^{18}$$$

Additionally:

  • For 20 points (group 1), the area in the rectangles given at input does not surpass $$$55\,000\,000$$$
  • For another 40 points (group 2), the perimeter in the rectangles given at input does not surpass $$$200\,000$$$
Example
Input
3
1 1 5 5
1 3 4 6
365 100 425 178
Output
7
3
81
Note

For the first game, we have the points with value 1: $$$(1,1), (2,2), (3,3), (4,4), (5,5), (1,4)$$$ and $$$(4,1)$$$

For the second game, we have the points with value 1: $$$(3,3), (4,4)$$$ and $$$(1,4)$$$

For the third game, we have a lot of points with value 1, so we ask you to believe us.

C. Find Set
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After creating $$$10^6$$$ boring problems , Le GrandPaPà decided to create a new one.

Your friend from Strehaia , Le GrandPaPà , once had a set $$$A$$$ of $$$N$$$ positive integers , but had unfortunately lost it. He only knows some properties of the array : It certainly contained $$$K$$$ pairs $$$(i,j)$$$ $$$1 \le i \lt j \le \lfloor \frac{N}{2} \rfloor$$$ satisfying the following property : $$$$$$A_{i+j} + A_{j-i} = \lfloor \frac{A_{2i} + A_{2j}}{2} \rfloor$$$$$$ Where $$$\lfloor X \rfloor$$$ denotes the biggest integer smaller or equal to $$$X$$$. GrandPaPà gives you integers $$$N,K$$$ and the $$$K$$$ pairs and asks you to find a set that could have been the one he lost.

Input

The input consits of integers $$$N,K$$$ , followed by $$$K$$$ lines denoting the pairs $$$(i,j)$$$. $$$N,K \le 1000$$$

Output

The output consists of $$$N$$$ integers , a set that could be the one GrandPaPà lost. All printed integers must be in range $$$[0,10^9]$$$ , and all must be distinct. Print -1 if there is no solution.

Scoring

This problem allows partial scoring.

Let T be the number of pairs that satisfy the propriety. The score is given by the following formula :

  • 0 points if $$$T \lt \lfloor \sqrt{K} \rfloor$$$
  • $$$\frac{T}{K} \cdot 100$$$ percent of the test score
Example
Input
4 1
1 2
Output
7 4 1 12
Note

The output is correct since $$$A_{1+2} + A_{2-1} = \lfloor \frac{A_{1 \cdot 2} + A_{2 \cdot 2}}{2} \rfloor$$$ $$$\Leftrightarrow$$$ $$$A_3 + A_1 = \lfloor \frac{A_2 + A_4}{2} \rfloor$$$ $$$\Leftrightarrow$$$ $$$1+7 = \lfloor \frac{16}{2} \rfloor$$$ $$$\Leftrightarrow$$$ $$$8 = 8$$$ which is true.

You can find information about Strehaia $$$\href{https://en.wikipedia.org/wiki/Strehaia}{here}$$$

D. Updating Inversions
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ of $$$n$$$ integers $$$a_1,a_2, \ldots, a_n$$$.

You are also given $$$q$$$ queries. Each query consists of two integers: $$$p$$$ ($$$1 \le p \le n $$$) and $$$x$$$ ($$$1 \le x \le 10^9$$$), meaning that $$$a_p$$$ will be replaced by $$$x$$$. For example, if $$$a=[6,5,5,3,6]$$$, $$$p=5$$$ and $$$x=7$$$, after the query $$$a$$$ will be equal to $$$[6,5,5,3,7]$$$.

After each query, print the number of inversions in array $$$a$$$. An inversion is a pair of indices $$$(i,j)$$$ where $$$i \lt j$$$ and $$$a_i \gt a_j$$$.

Input

The first line of input contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 10^5$$$ and $$$1 \le q \le 10^5$$$). The second line of input contains $$$n$$$ integers $$$1 \le a_1,a_2,\ldots,a_n \le 10^9$$$, the elements of the initial array. Each of the next $$$q$$$ lines contains two integers $$$p$$$ and $$$x$$$ ($$$1 \le p \le n$$$, $$$1 \le x \le 10^9$$$), the descriptions of the queries.

Output

Print $$$q$$$ integers, the number of inversions after each query.

Scoring
  • Subtask 1 (15 points): $$$1 \le N,Q \le 5000$$$;
  • Subtask 2 (20 points): $$$1 \le N \le 10^5$$$, $$$1 \le Q \le 10$$$;
  • Subtask 3 (30 points): $$$1 \le N,Q \le 40000$$$;
  • Subtask 4 (10 points): $$$1 \le a_i \le 10^5$$$;
  • Subtask 5 (25 points): No further constraints.
Example
Input
5 4
6 5 5 3 6
5 7
1 10
2 3
3 8
Output
5
6
5
6
Note
  • After the first update the array is $$$[6,5,5,3,7]$$$; there are $$$5$$$ inversions
  • After the second update the array is $$$[10,5,5,3,7]$$$; there are $$$6$$$ inversions
  • After the third uddate the array is $$$[10,3,5,3,7]$$$; there are $$$5$$$ inversions
  • After the fourth update the array is $$$[10,3,8,3,7]$$$; there are $$$6$$$ inversions

E. Rubik String
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

The Rubik's cube is a 3-D combination puzzle created by Hungarian sculptor and proffesor of architecture Erno Rubik. On the original classic Rubik's Cube, each of the six faces was covered by nine stickers, each of one of six solid colours: white, red, blue, orange, green, and yellow. In models as of 1988, white is opposite yellow, blue is opposite green, and orange is opposite red. - excerpt from Wikipedia

You are given a string $$$s$$$ of $$$n$$$ characters $$$s_i \in \{W, R, B, O, G, Y\}$$$. Each of these 6 letters coresponds to a colour featured on the Rubik's Cube (White, Red, Blue, Orange, Green and Yellow, respectively).

$$$s$$$ is considered a Rubik String if, for all adjacent characters, their coresponding colours are adjacent faces of the Rubik's Cube. For example, $$$"WRBOGY"$$$ is a Rubik String, while $$$"WW"$$$ and $$$"WYO"$$$ are not.

Naturally, not all strings are Rubik Strings, but they can be transformed into one via a sequence of (hopefully harmless) operations. In one operation, one can turn any character in $$$s$$$ into any other character $$$c_2 \in \{W, R, B, O, G, Y\}$$$.

For a given string $$$s$$$, find the minimum amount of operations required to make $$$s$$$ a Rubik String.

Input

Each test contains multiple testcases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The following lines will contain the descriptions of the test cases.

 
The description of each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of string $$$s$$$.

The second line contains $$$n$$$ characters — the elements of string $$$s$$$.

It is guaranteed that the sum of $$$n$$$ will not exceed $$$5 \cdot 10^5$$$.
Output

For each testcase print one integer — the minimum amount of operations required to turn $$$s$$$ into a Rubik String.

Scoring
  • Subtask 1 (20 points): $$$s_i \in \{W,Y\}, \forall i \in [1,n]$$$;
  • Subtask 2 (30 points): $$$s_i \in \{W,R,B\}, \forall i \in [1,n]$$$;
  • Subtask 3 (50 points): No further constraints.
Example
Input
5
7
WWWWWWW
6
WRBOGY
2
WW
3
WYO
10
WYYWWBGROB
Output
3
0
1
1
4
Note
  • In the first testcase, minimum number of operations needed to transform $$$s$$$ into a Rubik String is $$$3$$$. One such Rubik String is $$$s="WRWOWBW"$$$;
  • In the second testcase, $$$s$$$ is already a Rubik String;
  • In the third testcase, an example of a Rubik String obtainable in one operation is $$$s="BW"$$$;
  • In the fourth testcase, an example of a Rubik String obtainable in one operation is $$$s="WGO"$$$;
  • In the fifth testcase, an example of a Rubik String obtainable in four operations is $$$s="WGYOWBYRWB"$$$;

F. àPaPdnarG
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

GrandPaPà gives you an array $$$A$$$ of $$$N$$$ elements. Your task is to split this array into $$$K$$$ non-intersecting non-empty contiguous subarrays such that the sum of all subarrays costs is minimum. The cost of an array $$$V$$$ is the number of pairs of indices $$$(i,j)$$$ such that i<j and $$$V_i \gt V_j.$$$

Input

Input consists of numbers $$$N,K$$$ and an array $$$A$$$ of $$$N$$$ elements.

Output

Print one integer : The minimum cost you can have.

Scoring
  • For all test data $$$N \cdot K \le 10^6$$$ , $$$1\le K \le N , -10^9 \le A_i \le 10^9$$$
  • For 5 points $$$K = 1$$$.
  • For another 10 points $$$K = 2$$$.
  • For another 20 points $$$N,K \le 100$$$
  • For the remaining test data , there are no further restrictions
Example
Input
4 2
4 3 2 1
Output
2
Note

The optimal split is [4,3][2,1]. The cost of this split is $$$1+1=2$$$ , which is minimum possible.

G. Xor Plains
time limit per test
0.5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

After getting money from the bank, Glob and Madf started their adventure. They have come across the Xor Plains, right below the Frozen Mountains.

The Plains can be represented as an array of $$$n$$$ distinct integers, where each element represents the height of the plains in that area. Let this array be called the 'plains array'. Glob knows that the bitwise xor of all heights is equal to $$$0$$$ and that the plains array is lexicographically minimal among all arrays whose xor-sum is $$$0$$$.

Glob has asked Madf to find the plains array, since he is too busy being the hero and gazing into the sunset. Madf succeeded, but can you?

Input

The first line of input contains one integer $$$k$$$ ($$$1 \le k \lt 1500$$$), the number of testcases. The second line of input contains $$$k$$$ space separated integers $$$n_i$$$ ($$$3 \le n_i \le 2 \cdot 10^5$$$), the sizes of the plains arrays. It is guaranteed that the sum of $$$n$$$ across all testcases does not exceed $$$10^6$$$ (i.e. $$$\sum{n} \le 10^6$$$).

Output

For each testcase, print $$$n_i$$$ distinct numbers from the interval $$$[1,10^6]$$$, representing the plains arrays.

Scoring
  • Subtask 1 (10 points): $$$n_i+1$$$ is a power of $$$2$$$;
  • Subtask 2 (20 points): $$$n_i, \Sigma n \le 300$$$;
  • Subtask 3 (30 points): $$$n_i, \Sigma n \le 3000$$$;
  • Subtask 4 (40 points): No further constraints.
Example
Input
3
3 5 6
Output
1 2 3
1 2 4 8 15
1 2 3 4 8 12
Note

These are the lexicographically minimal arrays whose xorsum is 0.

  • $$$1 \wedge 2 \wedge 3=0$$$
  • $$$1 \wedge 2 \wedge 4 \wedge 8 \wedge 15=0$$$
  • $$$1 \wedge 2 \wedge 3 \wedge 4 \wedge 8 \wedge 12=0$$$

H. Autumn
time limit per test
0.3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Tu rămâi la toate rece,

De te-ndeamnă, de te cheamă;

Ce e val, ca valul trece,

Nu spera și nu ai teamă;

Te întreabă și socoate

Ce e rău și ce e bine;

Toate-s vechi și nouă toate:

Vreme trece, vreme vine.

— Mihai Eminescu, Glossă

Reminiscing sadly on the prispă, El Capoc remembers the autumn. And with the autumn, he remembers a problem that made him very sad at the time:

Given a tree (undirected acyclic graph), rooted in node 1; Let p[u] be the parent of u (i.e. if we were to list the nodes on the path from the root to u, then the second-to-last element would be considered the father of u). Consider that p[1]=1.

Tolerate the following operations:

Update: Given u, the edge between $$$(p[u],u)$$$ is erased and $$$(p[p[u]],u)$$$ is inserted (i.e. the parent of leaf $$$u$$$ becomes $$$p[p[u]]$$$)

Query: Given u, Determine the maximum distance from u to one of the leaves in it's subtree (i.e. it's height)

Input

On the first line of input, you are given $$$N$$$ and $$$Q$$$ ($$$1 \le N \le 90\,000$$$, $$$1 \le Q \le 190\, 000$$$), the number of nodes, respectively the number of operations. Then, on the following line, $$$N-1$$$ numbers follow, the i-th of with represents $$$p[i+1]$$$. Then, $$$Q$$$ lines follow, on each of these there will be two numbers: $$$t_i$$$ and $$$u_i$$$. If $$$t_i = 1$$$, then you must update the leaf $$$u_i$$$. Otherwise ($$$t_i = 2$$$), print the answer for the query with parameter $$$u_i$$$

Output

For each query, print the respective distance

Scoring
  • For 10 points (Group 1), $$$1 \le N \le 1000$$$, $$$1 \le Q \le 2000$$$
  • For Another 20 points (Group 2), $$$1 \le N \le 15\,000$$$, $$$1 \le Q \le 30\,000$$$
  • For Another 30 points (Group 3), $$$1 \le N \le 22\,500$$$, $$$1 \le Q \le 39\,000$$$
  • For Another 40 points (Group 4), consider the initial restrictions
Example
Input
12 18
1 1 1 4 5 6 6 3 2 2 2
1 8
1 8
1 8
2 4
1 7
1 7
1 7
2 4
1 9
1 10
1 11
1 12
2 3
2 2
1 6
2 4
1 5
2 4
Output
3
2
0
0
1
1
Note
You can find the more clear and animated version of this at https://ibb.co/NW2fwD6

Also, we're terribly sorry for the weird restrictions.