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. VictoriaLogs:一款超低占用的 ElasticSearch 替代方案

    背景 前段时间我们想实现 Pulsar 消息的追踪流程,追踪实现的效果图如下: 实现其实比较简单,其中最重要的就是如何存储消息. 消息的读取我们是通过 Pulsar 自带的 BrokerInterce ...

  2. Go 如何正确关闭通道

    序言 Go 在通道这一块,没有内置函数判断通道是否已经关闭,也没有可以直接获取当前通道数量的方法.所以对于通道,Go 显示的不是那么优雅.另外,如果对通道进行了错误的使用,将会直接引发系统 panic ...

  3. numpy_tricks

    Numpy Tricks 这篇文章不定期更新,主要是记录在使用numpy过程中一些有效的tricks(或者重要的API) import numpy as np numpy.where() numpy. ...

  4. Solution -「洛谷 P5659」「CSP-S 2019」树上的数

    Description Link. 联赛原题应该都读过吧-- Solution Part 0 大致思路 主要的思路就是逐个打破,研究特殊的数据得到普通的结论. Part 1 暴力的部分分 暴力的部分分 ...

  5. Pisces.IM.Mood 前言

    关于 Pisces.IM.Mood Mood Pisces.IM.Mood 一款基于TCP协议的即时通讯开源系统 多个客户端目前支持以下功能: 支持文字,图片,文件,emoji表情的发送 文件限制为5 ...

  6. C++的动态分派在HotSpot VM中的重要应用

    众所周知,多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定.C++ 和 Java 作为当前最为流行的两种面向对象编程语言,其内部对于多态的支持 ...

  7. ⭐volatile⭐ 用volatile关键字则会从内存中直接读取变量的值

  8. Dubbo3应用开发—协议(Dubbo协议、REST协议 、gRPC协议、Triple协议)

    协议 协议简介 什么是协议 Client(Consumer端)与Server(Provider端)在传输数据时双方的约定. Dubbo3中常见的协议 1.dubbo协议[前面文章中使用的都是dubbo ...

  9. Go基础之变量和常量

    Go基础之变量和常量 目录 Go基础之变量和常量 一. 标识符.关键字.内置类型和函数 1.1 标识符 1.2 关键字 1.3 保留字 1.4 内置类型 1.4.1 值类型: 1.4.2 引用类型:( ...

  10. matlab关于阶梯图和图窗操作

    1阶梯信号绘制 Matlab 中绘制阶梯图函数:stairs x = [30 33 37 40 37 33 30 27 23 20 23 27 30 30]'; StepNum = length(x) ...