Algorithms Thread Treaps Contest
A. Shandom Ruffle
time limit per test
8 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Those of you who use Java (or are subscribed to SecondThread) likely know that calling Arrays.sort() on an array of primitives in Java can lead to TLE verdicts on Codeforces contests because there are some very specific cases in which quicksort runs in $$$n^2$$$ time. There are a couple solutions to this: one is to use Collections.sort() on an ArrayList. This uses mergesort and is always $$$n*log(n)$$$.

Another solution is to shandomly ruffle [aka randomly shuffle] the array before sorting so that it is very very very unlikely that it is still in some undesirable order. One way of doing this is to do the following:


shandom_ruffle(int a, int b, int[] array) {
int bStart=b;
while (a<bStart && b<=array.length) {
swap(array[a], array[b]); //swap the values at indecies a and b
a++;
b++;
}
}

In Java and the psuedocode above, arrays are pass-by-reference. Suppose David starts with the array $$$[1, 2, 3, 4, ... n]$$$, and calls this method $$$n$$$ times on the array, the ith time passing in $$$a_i$$$ and $$$b_i$$$. What will the array look like after these $$$n$$$ method calls?

Input

The first line will contain a single integer $$$n$$$. ($$$1 \leq n \leq 5*10^5$$$)

The following $$$n$$$ lines will each contain two integers $$$a_i$$$ and $$$b_i$$$. ($$$1 \leq a, b \leq n$$$) Note that $$$b$$$ may be less than or equal to $$$a$$$, in which case the method will not do anything.

Output

Print a single line containing n space-separated integers: the array after it has been shandomly ruffled all $$$n$$$ times.

Examples
Input
4
3 1
1 3
3 2
2 3
Output
3 1 4 2 
Input
5
4 1
5 4
3 5
4 5
5 2
Output
1 2 5 3 4 
Note

There's a much easier way to shandom_ruffle that takes $$$O(n)$$$, but that makes for a less interesting problem.

B. Pear TreaP
time limit per test
10 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Have you ever heard of an eer-tree? It is a very special tree that does super cool stuff with palindromes. As you would expect of such a structure, the word eer-tree is a palindrome itself. Even cooler, if we do the same thing with a treap, we get a Pear Treap! What an epic palindrome!

What's that I hear? "Pear Treap isn't actually a palindrome," you say? Nonsense! I always write a and e together as one character, so it is totally a palindrome. Anyhow, here's your palindromic treap problem, as promised:

You have a string initially containing $$$n$$$ lowercase characters. Please perform the $$$q$$$ operations (that is, please solve each query before reading the next one) of the following types:

1. Delete all characters from position $$$l$$$ to position $$$r$$$ inclusive.

2. Insert the character $$$c$$$ at position $$$p$$$. After this query, the character at position $$$p$$$ will be $$$c$$$, and all characters initially with indecies $$$\geq p$$$ will be moved one space to the right.

3. Is the substring from position $$$l$$$ to position $$$r$$$ a palindrome? A substring from $$$l$$$ to $$$r$$$ is the continuous block of characters starting at position $$$l$$$, ending at position $$$r$$$.

Input

The first line will contain two integers: $$$n$$$ and $$$q$$$. ($$$1 \leq n, q \leq 3*10^5$$$)

The second line will contain a string of $$$n$$$ lowercase letters. It will not contain the character that is $$$a$$$ and $$$e$$$ combined into one character.

The following $$$q$$$ lines each contain three integers, an are of one of the following forms:

- $$$1$$$ $$$l$$$ $$$r$$$. In this case $$$1 \leq l \leq r \leq currentStringLen$$$. This query will not appear if the string is empty.

- $$$2$$$ $$$c$$$ $$$p$$$. In this case $$$1 \leq p \leq currentStringLen+1$$$. $$$c$$$ is a lowercase character.

- $$$3$$$ $$$l$$$ $$$r$$$. In this case $$$1 \leq l \leq r \leq currentStringLen$$$. This query will not appear if the string is empty.

Output

For each query of type 3, please print either "yes" or "no" depending on whether that substring is a palindrome or not.

Examples
Input
4 4
aaaa
1 3 4
3 1 1
3 1 1
2 a 3
Output
yes
yes

Input
5 5
aaaaa
2 b 3
1 1 1
3 5 5
1 5 5
1 3 3
Output
yes

Note

For the full Pear Treap experience, you can pretend that I am forcing you to solve this problem ONLINE. There is no such requirement as this is just practice and that would make the statement more annoying, but it could easily show up in a real contest or as a subproblem.

C. Sneetches and Speeches 3
time limit per test
15 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The sneetches are back on the beaches, with the great Sylvester McMonkey McBean! The $$$i$$$th sneetch has $$$a_i$$$ stars on his belly ($$$0 \leq a_i \leq 1$$$). Just like normal, many [$$$l...r$$$] ranges of sneetches will go through Sylvesters xor-with-1-inator. Formally, after an operation, for all positions between $$$l$$$ and $$$r$$$ inclusive, the sneetch at that position will have a star on his belly after if and only if he didn't have a star on his belly immediately before walking through the machine. You need to know the largest continuous range of sneetches that all have the same type of belly at each step. Easy stuff, right?

But wait! There is a very important detail still unaddressed: we are on a beach! And the great orator Demosthenes practices his speeches on a beach. Demosthenes is giving an inspiring speech which may at times incite the following behavior, doing one of the three things $$$q$$$ times:

1. Do the usual operation: make the sneetches in positions $$$[l..r]$$$ go through the machine. $$$l \leq r$$$

2. Talk to sneetches $$$[l..r]$$$ and convince them to reverse their order. After moving, the sneetches are relabeled $$$1$$$ to $$$n$$$ from left to right.

3. Sort the range of sneetches $$$[l..r]$$$ by the number of stars on their bellies, in nondecreasing order.

After each operation, Sylvester needs you to report the size of the largest continuous range of sneetches that have the same number of stars on their belly.

Input

The first line will contain two integers: $$$n$$$ and $$$q$$$ representing the number of sneetches and the number of queries. $$$1 \leq n, q \leq 3*10^5$$$

The second line will contain a string of 0s and 1s of length $$$n$$$. The $$$i$$$th character represents the number of stars on the $$$i$$$th sneech's belly initially.

$$$q$$$ lines follow, each of the form $$$t$$$ $$$l$$$ $$$r$$$ where $$$t$$$ is 1, 2, or 3 representing the query type as described in the statement, and $$$1 \leq l \leq r \leq n$$$.

Output

Print $$$q$$$ lines, each with a single integer: the length of the longest run of sneetches with the same belly type after each query.

Examples
Input
8 8
00000000
1 1 3
2 2 7
2 2 4
2 5 6
2 5 5
3 1 8
1 4 5
3 6 8
Output
5
4
4
3
3
5
5
5
Input
7 7
0111111
3 3 7
1 1 7
2 1 4
2 2 6
1 1 6
2 1 2
1 2 7
Output
6
6
3
3
3
3
2
Note

This problem was inspired by the problem Sneetches, originally written by Matt Fontaine. The original problem only had queries of type 1.

D. The Grim Treaper
time limit per test
15 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Happy Halloween! Farmer John is rather weary: he has lots of harvesting still unfinished, and his Deere sweet tractor just broke down, so he must finish the harvesting with nothing more than his scythe. He is rather depressed knowing the total amount of harvesting he has left. To make things even worse, Bessie and friends don't know how to use a toilet, so they keep leaving... fertilizer... all over the grass, causing the wheat to grow even more! So much reaping of wheat needs to be done! How grim!

Initially, FJ has $$$n$$$ blades of grass numbered $$$1..n$$$ in a row. Initially blade $$$i$$$ has height $$$a_i$$$. $$$q$$$ times, one of the following will happen:

1. FJ will slice his scythe from $$$l$$$ to $$$r$$$ at height $$$h$$$, cutting all blades from $$$l$$$ to $$$r$$$ that are strictly longer than height $$$h$$$ to height $$$h$$$.

2. FJ will transplant range $$$l..r$$$ of his field to the end of the field. He will move all grass blades forwards so, in total, positions $$$1..n$$$ are still occupied after this operation.

3. Bessie and friends will take a dump on blades of grass from $$$l$$$ to $$$r$$$. Each of them will grow by $$$x$$$ units where $$$x$$$ is the stinkiness of the... fertilizer.

After each operation, Farmer John needs to know how much work he has left to do. In particular, he would like to know the sum of heights of all blades of grass.

Input

The first line will contain two integers $$$n$$$ and $$$q$$$: the number of blades of grass, and the number of events. ($$$1 \leq n, q \leq 3*10^5$$$)

The second line will contain $$$n$$$ integers: the initial heights of grass blades. ($$$1 \leq a_i \leq 10^6$$$).

The following $$$q$$$ lines will describe each query, and be of one of the following forms:

- $$$1$$$ $$$l$$$ $$$r$$$ $$$h$$$. In this case $$$1 \leq l \leq r \leq n$$$ and $$$1 \leq h \leq 10^9$$$.

- $$$2$$$ $$$l$$$ $$$r$$$. In this case $$$1 \leq l \leq r \leq n$$$.

- $$$3$$$ $$$l$$$ $$$r$$$ $$$x$$$. In this case $$$1 \leq l \leq r \leq n$$$ and $$$1 \leq x \leq 10^5$$$.

Note that grass may grow to more than $$$2 * 10^9$$$ units.

Output

After each query, print a single line describing the total height of all blades of grass after that query.

Examples
Input
3 3
125987 264237 288891
3 2 3 30851
2 2 3
3 1 2 88689
Output
740817
740817
918195
Input
5 5
3 3 1 8 7
1 4 5 9
2 1 3
3 1 2 4
2 2 5
3 3 3 5
Output
22
22
30
30
35

E. Sneetches and Speeches 2
time limit per test
15 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Note: In this version of the problem, queries of type 3 will not appear.

The sneetches are back on the beaches, with the great Sylvester McMonkey McBean! The $$$i$$$th sneetch has $$$a_i$$$ stars on his belly ($$$0 \leq a_i \leq 1$$$). Just like normal, many [$$$l...r$$$] ranges of sneetches will go through Sylvesters xor-with-1-inator. Formally, after an operation, for all positions between $$$l$$$ and $$$r$$$ inclusive, the sneetch at that position will have a star on his belly after if and only if he didn't have a star on his belly immediately before walking through the machine. You need to know the largest continuous range of sneetches that all have the same type of belly at each step. Easy stuff, right?

But wait! There is a very important detail still unaddressed: we are on a beach! And the great orator Demosthenes practices his speeches on a beach. Demosthenes is giving an inspiring speech which may at times incite the following behavior, doing one of the three things $$$q$$$ times:

1. Do the usual operation: make the sneetches in positions $$$[l..r]$$$ go through the machine. $$$l \leq r$$$

2. Talk to sneetches $$$[l..r]$$$ and convince them to reverse their order. After moving, the sneetches are relabeled $$$1$$$ to $$$n$$$ from left to right.

3. Queries of type 3 do not appear in this version.

After each operation, Sylvester needs you to report the size of the largest continuous range of sneetches that have the same number of stars on their belly.

Input

The first line will contain two integers: $$$n$$$ and $$$q$$$ representing the number of sneetches and the number of queries. $$$1 \leq n, q \leq 3*10^5$$$

The second line will contain a string of 0s and 1s of length $$$n$$$. The $$$i$$$th character represents the number of stars on the $$$i$$$th sneech's belly initially.

$$$q$$$ lines follow, each of the form $$$t$$$ $$$l$$$ $$$r$$$ where $$$t$$$ is 1, or 2 representing the query type as described in the statement, and $$$1 \leq l \leq r \leq n$$$.

Output

Print $$$q$$$ lines, each with a single integer: the length of the longest run of sneetches with the same belly type after each query.

Examples
Input
8 8
00000000
1 1 3
2 2 7
1 2 4
1 5 6
2 5 5
2 1 8
2 4 5
1 6 8
Output
5
4
4
5
5
5
5
3
Input
7 7
0111111
1 3 7
1 1 7
2 1 4
1 2 6
2 1 6
1 1 2
2 2 7
Output
5
5
4
3
3
2
3
Note

This problem was inspired by the problem Sneetches, originally written by Matt Fontaine. The original problem only had queries of type 1.

Y. Sneetches and Speeches 1
time limit per test
15 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The only difference between this problem and the other two is that this problem ONLY HAS QUERIES OF TYPE 1!

The sneetches are back on the beaches, with the great Sylvester McMonkey McBean! The $$$i$$$th sneetch has $$$a_i$$$ stars on his belly ($$$0 \leq a_i \leq 1$$$). Just like normal, many [$$$l...r$$$] ranges of sneetches will go through Sylvesters xor-with-1-inator. Formally, after an operation, for all positions between $$$l$$$ and $$$r$$$ inclusive, the sneetch at that position will have a star on his belly after if and only if he didn't have a star on his belly immediately before walking through the machine. You need to know the largest continuous range of sneetches that all have the same type of belly at each step. Easy stuff, right?

But wait! There is a very important detail still unaddressed: we are on a beach! And the great orator Demosthenes practices his speeches on a beach. Demosthenes is giving an inspiring speech which may at times incite the following behavior, doing one of the three things $$$q$$$ times:

1. Do the usual operation: make the sneetches in positions $$$[l..r]$$$ go through the machine. $$$l \leq r$$$

2. Queries of type 2 will not appear in this problem.

3. Queries of type 3 will not appear either.

After each operation, Sylvester needs you to report the size of the largest continuous range of sneetches that have the same number of stars on their belly.

Input

The first line will contain two integers: $$$n$$$ and $$$q$$$ representing the number of sneetches and the number of queries. $$$1 \leq n, q \leq 3*10^5$$$

The second line will contain a string of 0s and 1s of length $$$n$$$. The $$$i$$$th character represents the number of stars on the $$$i$$$th sneech's belly initially.

$$$q$$$ lines follow, each of the form $$$t$$$ $$$l$$$ $$$r$$$ where $$$t$$$ is always 1 [for this problem] representing the query type as described in the statement, and $$$1 \leq l \leq r \leq n$$$.

Output

Print $$$q$$$ lines, each with a single integer: the length of the longest run of sneetches with the same belly type after each query.

Examples
Input
8 8
00000000
1 1 3
1 2 7
1 2 4
1 5 6
1 5 5
1 1 8
1 4 5
1 6 8
Output
5
4
3
3
3
3
4
4
Input
7 7
0111111
1 3 7
1 1 7
1 1 4
1 2 6
1 1 6
1 1 2
1 2 7
Output
5
5
3
2
3
4
3
Note

This problem was inspired by the problem Sneetches, originally written by Matt Fontaine. The original problem only had queries of type 1, and is essentially the same as this easiest version.

Z. Trick or Treap
time limit per test
8 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Halloween is almost here! Here in the United States, it is a tradition for young children to dress up as demons and super heros wonder around in the dark very late at night, visit random strangers in their houses, and ask for candy.

Alice and Bob just solved a problem that required them to eat about $$$10^9$$$ candies and are very full now, so instead, they will ask their neighbors to either show them a trick, or give them a brand new treap ("trick or treap")! $$$q$$$ times, one of the following will happen:

- 1. This neighbor will given them a new treap node $$$i$$$, with value $$$y$$$. Here, $$$i$$$ is the query number.

- 2. This neighbor will perform a trick: if the nodes $$$y$$$ and $$$z$$$ are not in the same group, he will merge them into the same group, with $$$y$$$'s group to the left of $$$z$$$'s group. Otherwise, he will teach them how to juggle.

- 3. This neighbor will perform a different trick: If the group containing node $$$y$$$ contains more than $$$z$$$ nodes, he will split the first $$$z$$$ nodes from the rest of them. Otherwise, he will teach them Kotlin.

- 4. Alice's and Bob's overprotective mother will call and ask them about the sum of values in the group that $$$y$$$ is in.

Can you write a bot to pretend to answer the queries for Alice so that she can continue to bother her neighbors in the middle of the night without interruption?

Input

The first line will contain a single integer $$$q$$$. $$$1 \leq q \leq 5*10^5$$$ $$$q$$$ lines follow. They will look like one of the following:

- $$$1$$$ $$$y$$$. In this case $$$0 \leq y \leq 10^5$$$.

- $$$2$$$ $$$y$$$ $$$z$$$. Nodes $$$y$$$ and $$$z$$$ will already exist.

- $$$3$$$ $$$y$$$ $$$z$$$. Node $$$y$$$ will already exist. $$$0 \lt z$$$.

- $$$4$$$ $$$y$$$. Node $$$y$$$ will already exist.

Output

For each query of type 4, print the sum of the values in the queried group.

Examples
Input
10
1 38788
3 1 1
3 1 2
1 56200
3 1 2
3 1 2
4 4
3 4 4
4 1
3 4 6
Output
56200
38788
Input
8
1 60420
3 1 1
3 1 1
1 49164
2 1 1
2 4 1
2 1 4
1 24036
Output