#ABC237G. [ABC237G] Range Sort Query

[ABC237G] Range Sort Query

Score : 600600 points

Problem Statement

Given is a permutation P=(P1,P2,,PN)P=(P_1,P_2,\ldots,P_N) of 1,2,,N1,2,\ldots,N, and an integer XX.

Additionally, QQ queries are given. The ii-th query is represented as a triple of numbers (Ci,Li,Ri)(C_i,L_i,R_i). Each query does the following operation on the permutation PP.

  • If Ci=1C_i=1: sort PLi,PLi+1,,PRiP_{L_i},P_{L_i+1},\ldots,P_{R_i} in ascending order.
  • If Ci=2C_i=2: sort PLi,PLi+1,,PRiP_{L_i},P_{L_i+1},\ldots,P_{R_i} in descending order.

In the final permutation PP after executing all queries in the given order, find ii such that Pi=XP_i=X.

Constraints

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1Q2×1051 \leq Q \leq 2\times 10^5
  • 1XN1 \leq X \leq N
  • (P1,P2,,PN)(P_1,P_2,\ldots,P_N) is a permutation of (1,2,,N)(1,2,\ldots,N).
  • 1Ci21 \leq C_i \leq 2
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ XX

P1P_1 P2P_2 \ldots PNP_N

C1C_1 L1L_1 R1R_1

C2C_2 L2L_2 R2R_2

\vdots

CQC_Q LQL_Q RQR_Q

Output

Print the answer.

5 2 1
1 4 5 2 3
1 3 5
2 1 3
3

Initially, the permutation is P=[1,4,5,2,3]P=[1,4,5,2,3]. The queries change it as follows.

  • 11-st query sorts the 33-rd through 55-th elements in ascending order, making P=[1,4,2,3,5]P=[1,4,2,3,5].
  • 22-nd query sorts the 11-st through 33-rd elements in descending order, making P=[4,2,1,3,5]P=[4,2,1,3,5].

In the final permutation, we have P3=1P_3=1, so 33 should be printed.

7 3 3
7 5 3 1 2 4 6
1 1 7
2 3 6
2 5 7
7

The final permutation is P=[1,2,6,5,7,4,3]P=[1,2,6,5,7,4,3].