Circular Swap

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 4.0s
Memory limit: 512M

Problem type

Given a circular sequence A with n integers, denoted as a1,a2,,an in clockwise order. Bob is going to perform q operations. Each operation will give an interval [L,R] and an integer x. Bob will modify the sequence as follows.

Copy
for (int i = L; i <= R; i++) {
    if (a[i] > x) swap(a[i], x);
}

Since the sequence is circular, the given R may be less than L. Bob will perform the above operation from L to R in clockwise order. After each operation, Bob wants to know the value of x. Can you write a program to help him?

Input Specification

The first line of input contains two integers n and q (1n400000, 1q25000), indicating the length of the sequence and the number of operations.

Each of the following n lines contains one integer ai (1ai109), the i-th element in the sequence.

Each of the following q lines contains three integers L, R, and x (1L,Rn, 1x109), indicating an operation.

Output Specification

Output q lines. Each line contains one integer, the final value of x after the operation.

Constraints

Subtask Points Additional constraints
1 15 n2000, q2000.
2 15 L=1, R=N.
2 70 No additional constraints.

Sample Input 1

Copy
6 7
8
6
7
4
5
9
2 4 5
4 1 4
6 2 7
1 5 2
3 4 8
4 3 1
3 1 3

Sample Output 1

Copy
7
9
8
7
8
6
5

Explanation

  • The initial sequence is like [8,6,7,4,5,9].
  • After the 1st operation, it's like [8,5,6,4,5,9] and x=7.
  • After the 2nd operation, it's like [8,5,6,4,4,5] and x=9.
  • After the 3rd operation, it's like [7,5,6,4,4,5] and x=8.

Sample Input 2

Copy
4 2
5
2
4
7
1 4 3
1 4 1

Sample Output 2

Copy
7
5

Comments

There are no comments at the moment.