IME++ Open Contest 2024
A. Attacking Bees
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The city of Rio de Janeiro is under attack! By a swarm of bees? That act in a very specific manner? That's correct, and unfortunately, MatheusAB has been caught in the middle of this mess. Luckily for him though, he was able to find a bunker in time to hide in and for some reason he has a special device that is able to track the location and direction of the bees. As previously mentioned, they behave in a very specific way: there are $$$n$$$ queen bees that command a swarm of millions of bees and they will attack anything that is present in the smallest convex polygon that contains all the queen bees.

With MatheusAB's special device, he's able to track the location of the queen bees, and he realizes that each of them has a specific constant velocity and direction. Since he's exhausted from this situation, given the initial positions of the queen bees, he asks you to help him find out what's the minimum amount of time he'll have to wait in order for the city to be safe to roam in again. Initially, all queen bees are within city limits. Rio de Janeiro is considered safe once there aren't any areas within it that are susceptible to a bee attack. The city is conveniently a convex polygon defined by $$$m$$$ vertices.

Input

The first line contains two integers, $$$n$$$, $$$m$$$, $$$(1 \leq n \leq 10^5)$$$, $$$(3 \leq m \leq 10^5)$$$ — the number of queen bees and the number of vertices that define the city.

Each of the next $$$n$$$ lines will contain the description of the queen bee. Each line contains four integers, $$$x_i$$$, $$$y_i$$$, $$$vx_i$$$, $$$vy_i$$$, $$$(-10^9 \leq x_i, y_i \leq 10^9)$$$, $$$(-10^7 \leq vx_i, vy_i \leq 10^7)$$$ — the position in the x and y coordinates and their respective velocities. The velocity is given in units per second. It is guaranteed that each queen bee starts within city limits.

Each of the next $$$m$$$ lines will contain two integers, $$$x_j$$$, $$$y_j$$$, $$$(-10^9 \leq x_j, y_j \leq 10^9)$$$ — the coordinates of the $$$j^{th}$$$ vertex that describes the city.

Output

If the city is forever under attack, print the word rip in a single line.

Otherwise, output a real number — the minimum amount of time (in seconds) MatheusAB will have to wait. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$, that being if your answer is $$$a$$$ and the jury's is $$$b$$$, then the judge will check if $$$\frac{|a - b|}{max(1, b)} \leq 10^{-6}$$$.

Examples
Input
3 3
-3 0 -1 0
0 -3 1 1
0 3 1 1
-5 0
0 5
0 -5
Output
12
Input
3 3
-3 0 -1 0
0 -3 -1 -1
0 3 1 1
-5 0
0 5
0 -5
Output
rip
Note
The following image describes the first test case. The polygon in red is the city, the polygon in blue describes the initial position of the queen bees and the green polygon represents their area at time $$$\approx 12$$$.

For the second test case, it is easy to see that the bees will always be attacking the city.

B. Big Numbers
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

I don't feel like wasting your time today with long statements, so let's cut straight to the point.

Let $$$p(x)$$$ be the product of all square numbers that divide $$$x$$$, or more formally, $$$p(x) = \displaystyle\prod_{\substack{i = 1 \\ i^2 | x}}^{\infty} i^2$$$.

Given $$$a$$$ and $$$b$$$, your task is to calculate the number of times $$$a!^b$$$ divides $$$p\big(a!^b\big)$$$. More formally, calculate the maximum value of $$$c$$$ such that $$$(a!^b)^c \mid p\big(a!^b\big)$$$.

Since $$$c$$$ can be quite large, print the sum of its digits in base $$$10$$$.

Input

The input consists of the two integers $$$a$$$ and $$$b$$$ $$$(2\leq a \leq 4 \times 10^5, 1 \leq b \leq 10^4)$$$ on a single line.

Output

Print the sum of digits of the maximum value of $$$c$$$ such that $$$(a!^b)^c$$$ divides $$$p\big(a!^b\big)$$$. In particular, we define the sum of digits of $$$c=0$$$ to be $$$0$$$.

Examples
Input
5 3
Output
6
Input
5 5
Output
10
Input
3 1
Output
0
Input
42069 6969
Output
78510
Note

In the first example, $$$5!^3=1728000$$$, and we can check that $$$(5!^3)^6 \mid p(5!^3)$$$ but $$$(5!^3)^7 \nmid p(5!^3)$$$, so the answer is $$$c=6$$$ and the sum of its digits is $$$6$$$.

In the second example, we get $$$c=28$$$, and the sum of its digits is $$$2+8=10$$$.

In the third example, $$$3!^1=6$$$, and $$$p(3!^1)=1$$$, so $$$c=0$$$.

C. Colorful Date
time limit per test
7 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

A lovely couple, Sergio and Ana, go out on a romantic evening to paint. They go to this place that has some very special paint colors, which are able to glow in the dark and some of which have some very funky effects! The event starts with the couple receiving $$$q$$$ blank canvases, and a whole variety of $$$k$$$ colors to choose from. The colors are numbered from $$$1$$$ to $$$k$$$. These can be divided into two: those who are weird and those who are permanent. The colors that are deemed weird have this special effect: if a person tries to color the canvas an even amount of times with the same weird color, every speck of it magically disappears! Permanent colors don't suffer from this (un)fortunate condition.

This painting gallery has some very strict rules on how to use the colors: the couple has $$$n$$$ color palettes to choose from, and once the palettes are chosen, they need to use every color contained in each one. As per guidelines, permanent colors can't be present in the same palette as weird ones. These palettes each contain each color at most once, and contains enough paint of each color to paint the entire canvas (the couple doesn't need to use the same palette twice). Every canvas is an independent query, so they have the complete set of palettes available.

For each $$$q_i$$$ canvas, Ana decides that she wants Sergio to paint it using a specific array of colors. As Sergio wants to paint each one without tiring himself too much, he wants to know for each canvas, what's the least amount of different palettes that he has to choose from in order to satisfy Ana.

Input

The first line contains three integers, $$$n$$$, $$$k$$$, $$$q$$$, $$$(1 \leq n \leq 2 \times 10^5)$$$, $$$(1 \leq k \leq 20)$$$, $$$(1 \leq q \leq 10^6)$$$ — the number of palettes, the number of distinct colors and the number of canvases.

The second line will contain a string $$$s$$$, $$$|s| = k$$$, containing only the letters W and P, indicating whether the $$$i^{th}$$$ color is weird or permanent.

The next $$$n$$$ lines will describe the color palettes that exist. Each line begins with an integer $$$m$$$, $$$(1 \leq m \leq k)$$$ indicating the amount of colors that are contained in this palette. In the next $$$m$$$ integers on the same line, $$$(1 \leq c_1, c_2, ..., c_m \leq k)$$$ — the colors in the palette. It is guaranteed that every color in the same palette is of the same type.

The next $$$q$$$ lines will describe the colors in the canvases that Ana desires. Each line begins with an integer $$$t$$$, $$$(1 \leq t \leq k)$$$, indicating the amount of colors that Ana desires in her canvas. In the next $$$t$$$ integers on the same line, $$$(1 \leq a_1, a_2, ..., a_t \leq k)$$$ — the colors that are desired.

Output

For each query, print out the minimum amount of palettes needed in order to get the colors that Ana desires. If it's not possible, print $$$-1$$$.

Examples
Input
3 5 2
WPWWP
2 1 3
2 2 5
3 1 3 4
5 1 2 3 4 5
1 1
Output
2
-1
Input
5 7 4
WWPPWPW
3 2 5 7
3 1 7 5
2 3 4
1 6
2 2 5
5 1 7 2 3 4
4 1 2 3 4
2 4 6
3 5 6 2
Output
3
3
-1
2
Note

In the first example:

For the first canvas, Sergio is able to color it using the second and third palettes. It is easy to see that for the second canvas, it is impossible to reach the color array desired.

In the second example:

The second canvas can be created using palettes 1, 2, 3. The fourth canvas can be created using palettes 4 and 5.

D. Deaga Loves Sequences
time limit per test
6.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Davi Holanda, usually known by his nickname, Deaga, has been fiddling around with arithmetic progressions, as it's something very important to know for the entrance exam of the Institute of Meaningful Equations, otherwise known as IME. He discovered the existence of third-order arithmetic progressions, which are arithmetic progressions that, given the first four elements, follow the construction rule: $$$a_{i + 3} - 3a_{i + 2} + 3a_{i + 1} - a_i = k$$$, where $$$k$$$ is some non-zero constant. The constant varies depending on the sequence.

In order to furthermore learn about this type of arithmetic sequence, he began playing around with making operations with it. He got an array $$$v$$$, of length $$$n$$$, and given the first four elements and two indices $$$i$$$ and $$$j$$$, $$$(i \lt j)$$$, he'd add the elements of the sequence onto the array. Of course he'd add them in order, with the first element being added to index $$$i$$$, and the $$$(j - i)^{th}$$$ element being added to index $$$j$$$. However, he'd perform these operations given some restrictions:

  • The constant $$$k$$$ of the given sequence is divisible by $$$6$$$.
  • The following is true: $$$(a_2 - a_0 - \frac{4k}{3}) \equiv 0 \mod 2$$$.

Deaga does this many times and ends up getting bored doing all the calculations, and asks for your help to speed things up! Based on the initially empty array, he performs $$$q$$$ queries, in which one will add a certain sequence to a certain range of the array, and another one which will ask the sum of elements of a certain range.

Such graphic depicts what a third-order arithmetic progression looks like
Input

The first line of the input contains two integers, $$$n$$$ and $$$q$$$, $$$(4 \leq n \leq 2 \times 10^5)$$$, $$$(1 \leq q \leq 2 \times 10^5)$$$ — the size of the empty array and the amount of queries.

The next $$$q$$$ lines will contain information about the $$$i^{th}$$$ query done by Deaga. Each line will start with an integer $$$t_i$$$, $$$(1 \leq t_i \leq 2)$$$ — the type of query.

  • For $$$t_i = 1$$$, 6 integers, $$$l$$$, $$$r$$$, $$$a_0$$$, $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, $$$(1 \leq l \lt r \leq n), (r - l \geq 3)$$$, $$$(1 \leq a_0, a_1, a_2, a_3 \leq 10^6)$$$ — the range of the update on the array and the first four elements of the sequence. It is guaranteed that this is a third-order arithmetic progression and that it follows the restrictions above.
  • For $$$t_i = 2$$$, 2 integers, $$$l$$$ and $$$r$$$, $$$(1 \leq l \leq r \leq n)$$$ — the range that Deaga wants to know the sum of. It is guaranteed that there's at least one query of this type.
Output

For every query in which $$$t_i = 2$$$, print out the sum of the elements in the range $$$[l, r]$$$. Since the sum can be very large, print out the sum modulo Deaga's favorite prime number: 1000696969.

Examples
Input
7 8
1 1 7 1 2 11 34
2 1 1
2 2 2
2 3 3
2 4 4
2 5 5
2 6 6
2 7 7
Output
1
2
11
34
77
146
247
Input
15 8
1 1 10 2 545 2224 5177
2 1 5
2 6 10
2 1 10
1 8 15 61 19 3 1
2 1 10
2 11 15
2 14 15
Output
17490
172265
189755
189838
1000696814
1000696821

E. Exotic Algorithm (Easy Version)
time limit per test
1.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

This is the easy version of the problem. The only difference between the two versions is the constraint on $$$|s|$$$ .

Navarro is (or was? I'm really not sure) a prominent student at the Institute of Making Ethanol, commonly known around the block as IME. He loves playing around with strings (of characters, not related to ropes) and so he also enjoys applying random algorithms to them. On one fine afternoon, he devised a simple algorithm to create a graph from a string with the following construction: two indices $$$i$$$ and $$$j$$$ are connected only if the substring $$$[i, j]$$$ is a palindrome. Seems simple right?

From the graph that's created, Navarro wants to find out the amount of connected components.

Input

The input consists of a single string $$$s$$$, $$$(1 \leq |s| \leq 10^4)$$$, containing only lowercase Latin letters.

Output

Output a single integer — the amount of connected components in the graph that's generated.

Examples
Input
abcacba
Output
4
Input
navarrolikestacocat
Output
14
Note

We can see that the resulting graph will have the following edges:

1-7

2-6

3-5

Thus, there will be 4 connected components.

F. Further Exotic Algorithm (Hard Version)
time limit per test
1.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

This is the hard version of the problem. The only difference between the two versions is the constraint on $$$|s|$$$ .

Navarro is (or was? I'm really not sure) a prominent student at the Institute of Making Ethanol, commonly known around the block as IME. He loves playing around with strings (of characters, not related to ropes) and so he also enjoys applying random algorithms to them. On one fine afternoon, he devised a simple algorithm to create a graph from a string with the following construction: two indices $$$i$$$ and $$$j$$$ are connected only if the substring $$$[i, j]$$$ is a palindrome. Seems simple right?

From the graph that's created, Navarro wants to find out the amount of connected components.

Input

The input consists of a single string $$$s$$$, $$$(1 \leq |s| \leq 10^6)$$$, containing only lowercase Latin letters.

Output

Output a single integer — the amount of connected components in the graph that's generated.

Examples
Input
abcacba
Output
4
Input
navarrolikestacocat
Output
14

G. Gifting Problems
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You live in a tree-shaped country with $$$n$$$ cities, and you conveniently live in city $$$1$$$. It's almost Christmas, so you want to send a gift to your friends. Luckily, you can use the famous post service called IMEmail. It's so quick that it only takes one day to move packages between two neighboring cities (wow! What a relief!). As always, there are some caveats when it comes to this: once a package travels through a city, it can't take a route that passes through that city again. In other words, there can't be a cycle in the path a package takes.

This year, you decided to gift all your friends headphones! You just finished buying the headphones, but now realized that they didn't come with a charging cable. Your friends will only be happy if they receive both the headphones and the charging cable exactly on Christmas day. Now, you decide to call many manufacturers to see how soon they can start distributing the cables across the country.

On the $$$i^{th}$$$ query, you'll send the headphones $$$x$$$ days before Christmas. You also find out that there's a manufacturer located in city $$$u$$$ that will send out the chargers $$$y$$$ days before Christmas. Given these various parameters, for each query, you want to know how many friends will be happy on Christmas Day.

Input

The first line contains two integers $$$n$$$, $$$q$$$, $$$(2 \leq n \leq 2 \times 10^5)$$$, $$$(1 \leq q \leq 2 \times 10^5)$$$ — the total number of cities and the number of queries, respectively.

The next line contains $$$n$$$ integers $$$f_1, f_2, \cdots, f_n\,(0 \leq f_i \leq 10^9)$$$ — the number of friends located in city $$$i$$$.

The following $$$n - 1$$$ lines each contain a pair of integers $$$u$$$, $$$v$$$, $$$(1\leq u, v\leq n,\, u \neq v)$$$ — indicating that cities $$$u$$$ and $$$v$$$ are connected. Of course, all the cities are somehow connected.

The next $$$q$$$ lines each contain three integers $$$u, x, y\, (2\leq u \leq n,\, 0 \leq x, y \leq n - 1)$$$ — indicating that there's a manufacturer located in city $$$u$$$ that will send the packages $$$y$$$ days before Christmas, while you send out the packages $$$x$$$ days before Christmas.

Output

For each query, print out the number of friends that will be happy on Christmas Day.

Example
Input
7 3
3 8 1 9 2 0 3
1 2
2 3
1 4
4 5
2 6
1 7
6 1 1
5 2 0
3 1 3
Output
8
2
12

H. House Rules
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Somewhere along a famous beach in Rio de Janeiro, $$$n$$$ friends share a house (because how can you afford rent alone?). They all follow one sacred rule: always leave your shoes by the door when entering.

Each friend has $$$k$$$ pairs of shoes, and since they are too poor to afford a decent shoe rack, they decided to just draw lines on the ground to create $$$nk$$$ slots to place their pairs of shoes. The slot $$$i$$$ is at a distance of $$$i$$$ from the door, and initially contains a shoe from the person $$$a_i$$$.

Every day, each person selects a random pair of shoes they own and leaves the house in the morning. They all arrive in the afternoon in a random order, and place their shoes on the first slot available (closest to the door).

You need to answer $$$q$$$ queries. On each query, given two integers $$$x$$$ and $$$d$$$, calculate the expected average distance of all the pairs of shoes of person $$$x$$$ to the door after $$$d$$$ days. It can be shown that this answer can always be expressed as a fraction $$$\frac{p}{q}$$$ where $$$p$$$ and $$$q$$$ are coprime integers. Calculate $$$p\cdot q^{-1}\mod (10^9+7)$$$.

Input

The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2\leq n \leq 10^5, 1\leq k \leq 10^5)$$$ — the number of friends and the number of pairs of shoes each of them owns. It is guaranteed that $$$n\cdot k \leq 5\cdot 10^5$$$.

The second line contains $$$n\cdot k$$$ integers $$$a_i$$$ $$$(1\leq a_i \leq n)$$$ — the owner of the shoe on the slot $$$i$$$ at the beginning.

The third line contains an integer $$$q$$$ $$$(1\leq q \leq 5\cdot 10^5)$$$ — the number of queries to answer.

The next $$$q$$$ lines contain each two integers $$$x$$$ and $$$d$$$ $$$(1\leq x\leq n, 0\leq d\leq 10^{18})$$$ — the person and the number of days for which you need to answer the query.

Output

For each query, print one integer — the expected average distance of the shoes of the person $$$x$$$ after $$$d$$$ days.

Examples
Input
3 1
1 2 3
5
1 0
2 6969694206969
1 420696969420
3 1
3 0
Output
1
2
2
2
3
Input
3 2
1 2 1 2 3 3
8
1 0
2 0
3 0
1 1
2 2
1 3
3 4
1 1000000000000000000
Output
2
3
500000009
750000008
375000006
812500009
625000008
763897687
Note

On the first example: We see that for $$$d = 0$$$, then the shoes will remain in the same spot, and so their distance to the door will remain the same. For the query $$$\{3\,\, 1\}$$$, when the people return to the house on day $$$1$$$, each shoe has an equal probability of staying in each position, hence the answer being $$$2$$$.

On the second example: For the query $$$\{1\,\, 0\}$$$, the shoes won't move, and so their average distance to the door is 2. For the query $$$\{1\,\, 1\}$$$, we can check that the answer is $$$\frac{11}{4} \equiv 750000008 \mod 10^9 + 7$$$. You can easily brute force all possibilites and verify this.

I. Intergalactic Conference
time limit per test
8 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

In a fictitious universe, there are two species of aliens that have agreed to peacefully coexist until the heat death of the universe. Once every 500 trillion years, the two parties hold a conference in some galaxy to discuss issues (and have some nice drinks). You are given the task to decide in which galaxy to hold this amazing conference (party).

There is one catch: intergalactic travel is expensive, so an alien from one species will be happy with the chosen place if and only if there exists an alien from the other species that has to travel the exact same distance to arrive at the conference. Luckily, the $$$n$$$ galaxies are located in a straight line, with a separation of $$$1$$$ megaparsec between adjacent ones.

Given the distribution of the aliens of both species in these galaxies, what is the maximum number of aliens you can make happy by choosing the position of the conference?

Input

The first line contains two integers $$$n_1$$$, $$$n_2$$$ $$$(1\leq n_1, n_2 \leq 5\cdot 10^5)$$$ — the number of galaxies that contain the aliens of the first species and second species respectively.

The next $$$n_1$$$ lines each contain two integers $$$g_{1,i}$$$, $$$p_{1,i}$$$ $$$(1\leq g_{1,i}\leq 5\cdot 10^5,\, 1\leq p_{1,i} \leq 10^9)$$$ — indicating that the galaxy $$$g_{1,i}$$$ contains $$$p_{1,i}$$$ aliens from the first species. It is guaranteed that $$$g_{1,i} \neq g_{1,j} \forall i \neq j$$$.

The next $$$n_2$$$ lines each contain two integers $$$g_{2,i}$$$, $$$p_{2,i}$$$ $$$(1\leq g_{2,i}\leq 5\cdot 10^5,\, 1\leq p_{2,i} \leq 10^9)$$$ — indicating that the galaxy $$$g_{2,i}$$$ contains $$$p_{2,i}$$$ aliens from the second species. It is guaranteed that $$$g_{2,i} \neq g_{2,j} \forall i \neq j$$$.

Output

The maximum number of aliens you will make happy if you choose the conference position optimally. The position must be in some galaxy, but this galaxy does not need to contain any aliens.

Examples
Input
2 2
1 2
4 6
1 3
6 5
Output
16
Input
3 2
2 3
4 5
6 1
3 2
5 1
Output
0
Input
4 4
7 1
10 1
5 1
1 3
8 2
9 3
1 4
4 3
Output
15
Note

For the first example, note that if the conference is placed at the $$$5^{th}$$$ galaxy, then the aliens from both species located at galaxy $$$1$$$ will be happy, as there'll be one from the other species that'll have to travel the same distance. In addition to that, the aliens from the first species at galaxy $$$4$$$ will be happy as aliens from the second species at galaxy $$$6$$$ will be happy as well; both will have to travel a distance of $$$1$$$ galaxy to reach galaxy $$$5$$$.

J. Juiceless Class
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Pedro Vieira is a student at the Institute of Many Equations, commonly referred to as IME. He's attending a class hosted by none other than the infamous Mr. Potato and since his class is soooooo boring (really, it is), Pedro begins making up random algorithms and running them in his mind (as one commonly does).

On one beautiful morning, he overheard Mr. Potato talking some nonsense about XOR operations, and so, tried to come up with an algorithm that does the following: given an array of integers, find the greatest value that can be obtained from XOR-ing a subset of this array. Since this is too easy, he reformulated the problem. Given an array $$$a$$$ of $$$n$$$ elements, for every $$$i$$$ from $$$1 ... n$$$, find the maximum XOR value of a subset of the array $$$(a_1, a_2, ..., a_i)$$$.

Input

The first line of the input contains a single integer, $$$n$$$, $$$(1 \leq n \leq 10^6)$$$ — the amount of elements in the array.

The second line contains $$$n$$$ integers $$$a_i$$$, $$$(0 \leq a_i \lt 2^{63})$$$ — the array $$$a$$$.

Output

Output $$$n$$$ integers in a single line, each one separated by a space, indicating the maximum value of applying the XOR operation over a subset of the array $$$(a_1, a_2, ..., a_i)$$$.

Examples
Input
5
4 3 5 2 19
Output
4 7 7 7 23
Input
7
392 17179869184 229 1128 44439 12283 1337
Output
392 17179869576 17179869576 17179870688 17179913623 17179913623 17179913643

K. kostka Loves Hashing
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

At the 2023 Brazilian Summer Camp, prominent Polish coder kostka told his students how hashing is the best thing in the world. It makes problems related to strings that much easier! As such, an easy question related to strings must be made in order to show how efficient it is. The task is quite simple: you are given a string $$$s$$$ and an integer $$$k$$$. You only need to print out how many substrings $$$t$$$ exist, such that $$$(|t| * freq(t) = k)$$$, where $$$freq(t)$$$ is the amount of times the substring appears in the string. If at least one substring fits the criteria, you should print out the lexicographically smallest and the lexicographically largest (note that they may be the same). Quite an easy task, right?

Lexicographical order is defined as follows: when ordering two strings $$$a$$$ and $$$b$$$, $$$a$$$ is lexicographically smaller than $$$b$$$ if and only if one of the following holds:

  • $$$a$$$ is a prefix of $$$b$$$, but $$$a \neq b$$$.
  • in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter of $$$b$$$.
Input

The first line of the input contains two integers $$$n$$$ and $$$k$$$, $$$(1 \leq n \leq 10^6)$$$, $$$(1 \leq k \leq 10^{12})$$$ — the length of the string and the desired value.

The second line of the input will contain the string $$$s$$$, containing only lowercase Latin letters.

Output

On the first line, output the amount of substrings that fit the criteria. Then, if substrings exist, print out the lexicographically smallest and then the lexicographically largest.

Examples
Input
6 12
aaaaaa
Output
2
aaa
aaaa
Input
5 3
zhdke
Output
3
dke
zhd
Input
6 20
kostka
Output
0

L. Little Gas Station
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The students of some programming summer camp love going to a meeting place known by all as the little gas station. For some reason, the city they currently live in can be thought of as having $$$n$$$ road intersections, with a total of $$$n - 1$$$ roads. From every intersection, you can reach every other one. They loved going to the same little gas station as always, which was originally located at the intersection whose label is $$$1$$$. However, the students sometimes complained that they had to walk too much in order to get to the original little gas station, as they could sometimes be on the other side of town.

Thus, a businessman from out of town appeared in order to help these poor students. Depending on the day, this businessman would build a replica of the original little gas station at some intersection $$$x$$$ (he would never build in an intersection that already contains a gas station). The owner of the original little gas station, furious with this new businessman, would, depending on the day, destroy one of the businessman's gas station replicas. However, the students simply didn't care enough about this fight between them, so given a day in which they wanted to hang out at a little gas station, they'd go to the nearest to the intersection they're currently at.

This fight lasted for $$$q$$$ days, and on each day, one of three events could occur: the businessman could build a replica at intersection $$$x$$$, the original owner could destroy a replica at intersection $$$y$$$, or the students, currently at intersection $$$z$$$, want to find out the minimum amount of roads they need to cover to reach any little gas station.

Input

The first line contains two integers, $$$n$$$ and $$$q$$$, $$$(1 \leq n \leq 2 \times 10^5)$$$, $$$(1 \leq q \leq 2 \times 10^5)$$$ — the amount of intersections and days the conflict lasted.

The next $$$n - 1$$$ lines describe the structure of the city; the $$$i^{th}$$$ line contains two integers, $$$a_i$$$, $$$b_i$$$, $$$(1 \leq a_i, b_i \leq n)$$$, $$$(a_i \neq b_i)$$$ — the roads $$$a_i$$$ and $$$b_i$$$ are connected via a road.

The next $$$q$$$ lines will contain information about the event that occurs on the $$$i^{th}$$$ day. Each line will start with an integer $$$t_i$$$, $$$(1 \leq t \leq 3)$$$ — the type of event that occurs on that day.

  • For $$$t_i = 1$$$, an integer $$$x$$$, $$$(2 \leq x \leq n)$$$ will be followed, indicating that a gas station will be built on the road with id $$$x$$$. It is guaranteed that the businessman doesn't try to build on an intersection that already has a gas station.
  • For $$$t_i = 2$$$, an integer $$$y$$$, $$$(2 \leq y \leq n)$$$ will be followed, indicating that the replica present at intersection $$$y$$$ will be destroyed. It is guaranteed that there is a replica at intersection $$$y$$$.
  • For $$$t_i = 3$$$, an integer $$$z$$$, $$$(1 \leq z \leq n)$$$ will be followed, indicating which intersection the students are at. For this type of query, print out the minimum amount of roads that need to be traversed in order to reach the closest gas station. It is guaranteed that there will be at least one day of this type.
Output

For every day in which $$$t_i = 3$$$, output the minimum amount of roads that need to be traversed, with each one being on separate lines.

Example
Input
7 5
1 2
1 3
2 4
2 5
3 6
3 7
1 2
1 3
3 7
2 3
3 7
Output
1
2
Note

When the first $$$t_i = 3$$$ query is made, the closest node to $$$7$$$ is $$$3$$$, and we can see that the distance is equal to one. Once this gas station is destroyed, the closest one to $$$7$$$ is $$$1$$$.

M. Inversion Test
time limit per test
1 second
memory limit per test
1024 MB
input
standard input
output
standard output

Schubert has recently been studying properties of arrays, and something that caught his attention is called inversions. From what he recalls from class, the number of inversions of an array $$$a$$$ of size $$$n$$$ is defined by the number of pairs $$$(i, j)$$$ such that $$$(1 \leq i \lt j \leq n)$$$, in which $$$(a_i \gt a_j)$$$. Schubert realized that given an array, there are plenty of subarrays whose number of inversions is the same as the original array. As such, he gave this simple task: given an array, find the size of the smallest subarray in which the number of inversions is the same as in the original array.

An array $$$b$$$ is a subarray of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

Input

The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$ — the number of test cases that Schubert gives to you.

The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 10^6)$$$ — the length of the array.

The second line of each test case contains $$$n$$$ integers $$$(a_1, a_2, ..., a_n)$$$ $$$(-10^9 \leq a_i \leq 10^9)$$$ — the elements of the array.

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^6$$$.

Output

For each test case, output a single integer in a separate line — the size of the smallest subarray in which the number of inversions is the same as the original one.

Examples
Input
2
4
49 0 2 -1
6
5 6 59 23 71 100
Output
4
2
Input
1
5
1 2 3 4 5
Output
0