102376: [AtCoder]ABC237 G - Range Sort Query

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

Given is a permutation $P=(P_1,P_2,\ldots,P_N)$ of $1,2,\ldots,N$, and an integer $X$.

Additionally, $Q$ queries are given. The $i$-th query is represented as a triple of numbers $(C_i,L_i,R_i)$. Each query does the following operation on the permutation $P$.

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

In the final permutation $P$ after executing all queries in the given order, find $i$ such that $P_i=X$.

Constraints

  • $1 \leq N \leq 2\times 10^5$
  • $1 \leq Q \leq 2\times 10^5$
  • $1 \leq X \leq N$
  • $(P_1,P_2,\ldots,P_N)$ is a permutation of $(1,2,\ldots,N)$.
  • $1 \leq C_i \leq 2$
  • $1 \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:

$N$ $Q$ $X$
$P_1$ $P_2$ $\ldots$ $P_N$
$C_1$ $L_1$ $R_1$
$C_2$ $L_2$ $R_2$
$\vdots$
$C_Q$ $L_Q$ $R_Q$

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

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

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

In the final permutation, we have $P_3=1$, so $3$ should be printed.


Sample Input 2

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

Sample Output 2

7

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

Input

题意翻译

## 题面 现有一个 $1 \sim N$ 的排列 $P = (P_1,P_2,\ldots,P_N)$ 和一个正整数 $X$。 接下来会进行 $Q$ 次操作,每次操作给出三个正整数 $(C_i,L_i,R_i)$: - 若 $C_i = 1$,则将 $P_{L_i},P_{L_i+1},\ldots,P_{R_i}$ 按升序排序; - 若 $C_i = 2$,则将 $P_{L_i},P_{L_i+1},\ldots,P_{R_i}$ 按降序排序。 请输出最后数 $X$ 所在的位置,即输出满足 $P_i = X$ 的正整数 $i$。 ## 输入 输入第一行,共三个正整数 $N,Q,X$。 接下来 $N$ 行,每行三个正整数 $C_i,L_i,R_i$,表示一次操作。 ## 输出 一行一个正整数表示答案。 ## 数据范围&提示 $1 \le N, Q \le 2 \times 10^5,1 \le X \le N\\[1.5ex] 1 \le C_i \le 2,1 \le L_i \le R_i \le N$。 保证输入全部是正整数。

Output

得分:600分

问题描述

给定一个整数序列 $P=(P_1,P_2,\ldots,P_N)$,其中包含 $1,2,\ldots,N$ 的全排列,以及一个整数 $X$。

另外给出了 $Q$ 个查询。每个查询用一个三元组 $(C_i,L_i,R_i)$ 表示。每个查询会对序列 $P$ 进行以下操作:

  • 如果 $C_i=1$:将 $P_{L_i},P_{L_i+1},\ldots,P_{R_i}$ 按升序排序。
  • 如果 $C_i=2$:将 $P_{L_i},P_{L_i+1},\ldots,P_{R_i}$ 按降序排序。

在按照给定顺序执行所有查询后的最终序列 $P$ 中,找出满足 $P_i=X$ 的 $i$ 的值。

限制条件

  • $1 \leq N \leq 2\times 10^5$
  • $1 \leq Q \leq 2\times 10^5$
  • $1 \leq X \leq N$
  • $(P_1,P_2,\ldots,P_N)$ 是 $(1,2,\ldots,N)$ 的全排列。
  • $1 \leq C_i \leq 2$
  • $1 \leq L_i \leq R_i \leq N$
  • 输入中的所有值都是整数。

输入

输入通过标准输入给出,格式如下:

$N$ $Q$ $X$
$P_1$ $P_2$ $\ldots$ $P_N$
$C_1$ $L_1$ $R_1$
$C_2$ $L_2$ $R_2$
$\vdots$
$C_Q$ $L_Q$ $R_Q$

输出

输出答案。


样例输入 1

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

样例输出 1

3

初始时,序列 $P$ 为 $[1,4,5,2,3]$。 经过查询后,它变为如下形式:

  • $1$-st 查询将第 $3$ 个到第 $5$ 个元素按升序排序,得到 $P=[1,4,2,3,5]$。
  • $2$-nd 查询将第 $1$ 个到第 $3$ 个元素按降序排序,得到 $P=[4,2,1,3,5]$。

在最终序列中,我们有 $P_3=1$,因此应输出 $3$。


样例输入 2

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

样例输出 2

7

最终序列是 $P=[1,2,6,5,7,4,3]$。

加入题单

算法标签: