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]$.

发现 \(x\) 一直是确定的,所以这么多数,其实在我们眼里,只有三种。大于 \(x\) 的,等于 \(x\) 的,小于 \(x\) 的。可以把他们分别标为 \(-1\),\(0\),\(1\)。

可以用线段树去维护区间中 \(-1\) 的数量,\(0\) 的数量,\(1\) 的数量就好了。正着排序就是数区间中 \(-1,0,1\) 的数量,然后把用线段树区间覆盖去维护。如果更改到\(0\)的位置,那就更新维护答案。

#include<cstdio>
const int N=2e5+5;
int n,q,x,p,tag[N<<2],ret,op,l,r;
struct node{
int cj,c0,c1;
void operator=(const node&n){
cj=n.cj;
c0=n.c0;
c1=n.c1;
}
node operator+(const node&n)const{
return (node){cj+n.cj,c0+n.c0,c1+n.c1};
}
}tr[N<<2];
void pushup(int o,int l,int r,int z)
{
if(z==-1)
tr[o].cj=r-l+1,tr[o].c0=tr[o].c1=0;
else if(!z)
tr[o].cj=tr[o].c1=0,tr[o].c0=1;
else
tr[o].c1=r-l+1,tr[o].cj=tr[o].c0=0;
tag[o]=z;
}
void pushdown(int o,int l,int r)
{
int md=l+r>>1;
if(tag[o]==3)
return;
pushup(o<<1,l,md,tag[o]);
pushup(o<<1|1,md+1,r,tag[o]);
tag[o]=3;
}
void update(int o,int l,int r,int x,int y,int z)
{
if(x>y)
return;
if(x<=l&&r<=y)
{
pushup(o,l,r,z);
return;
}
int md=l+r>>1;
pushdown(o,l,r);
if(md>=x)
update(o<<1,l,md,x,y,z);
if(md<y)
update(o<<1|1,md+1,r,x,y,z);
tr[o]=tr[o<<1]+tr[o<<1|1];
}
node query(int o,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
return tr[o];
int md=l+r>>1;
pushdown(o,l,r);
node ret=(node){0,0,0};
if(md>=x)
ret=ret+query(o<<1,l,md,x,y);
if(md<y)
ret=ret+query(o<<1|1,md+1,r,x,y);
return ret;
}
int main()
{
scanf("%d%d%d",&n,&q,&x);
for(int i=0;i<(N<<2);i++)
tag[i]=3;
for(int i=1;i<=n;i++)
{
scanf("%d",&p);
if(p>x)
update(1,1,n,i,i,1);
else if(p==x)
update(1,1,n,i,i,0),ret=i;
else
update(1,1,n,i,i,-1);
}
while(q--)
{
scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
node k=query(1,1,n,l,r);
update(1,1,n,l,l+k.cj-1,-1);
if(k.c0)
{
ret=l+k.cj;
update(1,1,n,l+k.cj,l+k.cj,0);
}
update(1,1,n,r-k.c1+1,r,1);
}
else
{
node k=query(1,1,n,l,r);
update(1,1,n,l,l+k.c1-1,1);
if(k.c0)
{
ret=l+k.c1;
update(1,1,n,l+k.c1,l+k.c1,0);
}
update(1,1,n,r-k.cj+1,r,-1);
}
}
printf("%d\n",ret);
}

[ABC237G] Range Sort Query的更多相关文章

  1. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  2. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  3. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  5. AOJ DSL_2_E Range Add Query (RAQ)

    Range Add Query 数列 A = {a1,a2,...,an} に対し.次の2つの操作を行うプログラムを作成せよ. add(s,t,x): as,as+1,...,at にxを加算する. ...

  6. AOJ DSL_2_D Range Update Query (RUQ)

    Range Update Query 数列 A = {a0,a1 ,...,an−1} に対し.次の2つの操作を行うプログラムを作成せよ. update(s,t,x): as,as+1,...,at  ...

  7. AOJ DSL_2_A Range Minimum Query (RMQ)

    Range Minimum Query (RMQ) Write a program which manipulates a sequence A = {a0,a1,...,an−1} with the ...

  8. range for query

    static void range_test(Args _args) { Query                   Query; QueryRun                QueryRun ...

  9. Range Sum Query 2D - Mutable & Immutable

    Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...

  10. LeetCode Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

随机推荐

  1. Django模板(请用Django2.0版本完成)

    1. 在learn目录下新建一个templates文件夹,里面新建一个home.html (1) 很简单的,就直接右键learn,新建文件夹,完成后,继续右键templates,创建文档,后缀名为ht ...

  2. 《CTFshow-Web入门》04. Web 31~40

    @ 目录 web31 题解 原理 web32 题解 原理 web33 题解 web34 题解 web35 题解 web36 题解 web37 题解 原理 web38 题解 原理 web39 题解 we ...

  3. Java 配置 HTTP/Socks 代理竟能如此简单

    在网络请求过程中,使用代理是一种常见的需求.代理服务器可以帮助我们隐藏真实的 IP 地址.加速访问速度.访问公司特定内网等等要求.在 Java 中,我们可以通过一些库或框架来实现代理的设置和使用. 但 ...

  4. Python条件控制和循环语句(if while for )

    Python条件控制和循环语句(if while for ) 条件控制 概念:Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块 结构 1. 顺序结构 ...

  5. C# winform 无边框窗口 移动

    给自己留个笔记, 在用wke做界面的时候. 往往需要把winform窗口设置成无边框 但是WebUI也需要移动窗口, 所以才把以前在易语言中用的方法翻译过来使用 第零步: 设置无边框窗口 form属性 ...

  6. LeetCode买卖股票之一:基本套路(122)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 关于<LeetCode买卖股票>系列 在L ...

  7. Web端上传数据到OSS

    阿里云文档:参考文献 更正第三点:用户带着从服务器获取的数据签名和文件上传到OSS,这样做可以保证安全性.减轻服务器负担. 1.操作步骤 ①新建Bucket ②创建后更改跨域设置 这一步是保证跨域请胯 ...

  8. UM 百度富文本编辑器上传报错

    看下报错信息 这三张图记录了当时,上传图片 遇见的报错信息 最终解决方案 我把UM下jsp文件下的ueditor-mini.jar包拷贝到WEB_INF下的lib文件夹下,就成功了. maven的话把 ...

  9. 实用工具、01 效率篇 | 几个操作快速提升 Typora 使用体验

    本篇文章旨在提高大家记笔记的效率,分享的工具请按个人需求安装 Typora-plugins 为 Typora 添加更多新功能,我最喜欢的是多标签页管理 obgnail/typora_plugin: T ...

  10. redis基本数据类型 Hash

    Hash 类型 Hash类型的常见命令 HSET key field value: 添加或者修改hash类型key的field的值HGET key field: 获取一个hash类型key的field ...