Start with array a_i = i, apply up to 300000 queries that reverse or rotate subarrays and ask for range min, max, sum, value at index, or index of a value, then print the final array.
Hard9ArrayImplementationSegment treeMathNo attempts yetTime limit1sMemory limit512 MBBeomsu and Sangsu, two brothers, play with an array. Their array A=[a1,…,aN] has size N, and at the start ai=i for every i (1≤i≤N). The brothers receive Q queries and handle them one at a time in the given order. Each query is one of the four kinds below.
1 l r (1≤l≤r≤N): find the minimum, the maximum and the sum of al through ar. Then reverse al through ar. After the reversal the array looks like this.
[a1,…,al−1,ar,ar−1,…,al+1,al,ar+1,…,aN]
2 l r x (1≤l≤r≤N, −N<x<N): find the minimum, the maximum and the sum of al through ar. Then rotate al through ar to the right by x positions. If x is negative, rotate to the left by −x positions. When 0<x≤r−l, the array looks like this.
[a1,…,al−1,ar−x+1,…,ar−1,ar,al,al+1,…,ar−x,ar+1,…,aN]
The rotation stays inside the block [l,r]. A value pushed off one end of the block comes back in at the other end. Write m=r−l+1 for the length of the block. Rotating right by x has the same effect as rotating right by x+m, so the result is fixed even when x is negative or ∣x∣≥m.
3 i (1≤i≤N): find the value of ai.
4 x (1≤x≤N): find the index i with ai=x.
Once a query changes the array, every later query works on the changed array. Print the value found by each query, and print the array as it stands after all queries are handled.
The first line has the array size N and the number of queries Q, separated by a space. (1≤N≤300000, 1≤Q≤300000)
Each of the next Q lines has one query in one of the four formats above.
Print one line per query holding the values found, separated by spaces. A query of kind 1 or kind 2 prints the minimum, the maximum and the sum in that order. A query of kind 3 or kind 4 prints the single value found.
On the last line, line Q+1, print the values of the array after all queries are handled, separated by spaces.
In the first example the array changes in this order.
[1,2,3,4,5]→[1,4,3,2,5]→[1,4,5,3,2]→[5,1,4,3,2]