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. 玄 - 利用DLL通知回调函数注入shellcode(上)

    序 偶然看到某国外大佬发布新技术-"Threadless"进程注入技术,据说可过EDR(确实可),总结该技术原理 - 在远程目标进程中利用DLL通知回调机制执行shellcode, ...

  2. 【Ubuntu】Ubuntu 配置记录

    目录 系统文件夹改回英文 Ubuntu 镜像 pypi 镜像 临时使用 设为默认 Doxygen + Graphviz 分析代码并画图 Graphviz 安装 Doxygen 安装 配置 运行 系统文 ...

  3. Pytorch构建超分辨率模型——常用模块

    Import required libraries: import torch import torch.nn as nn import torch.optim as optim from torch ...

  4. 1Nginx基础及编译安装

    1Nginx基础 1.Nginx概述 Nginx 功能介绍 Nginx(发音为"engine-x")是一个开源的高性能 HTTP和反向代理服务器.它具有以下功能: 1.静态文件服务 ...

  5. 循环神经网络RNN完全解析:从基础理论到PyTorch实战

    在本文中,我们深入探讨了循环神经网络(RNN)及其高级变体,包括长短时记忆网络(LSTM).门控循环单元(GRU)和双向循环神经网络(Bi-RNN).文章详细介绍了RNN的基本概念.工作原理和应用场景 ...

  6. .NET API 中的 FromRoute、FromQuery、FromBody 用法

    原文链接:https://www.cnblogs.com/ysmc/p/17663663.html 最近技术交流群里,还有不少小伙伴不知道 FromRoute.FromQuery.FromBody 这 ...

  7. 一个.NET 7 + DDD + CQRS +React+Vite的实战项目

    项目简介 基于SignalR实现聊天通信,支持横向扩展,可支撑上万用户同时在线聊天 后端架构 后端技术栈采用 .NET 7 PostgreSQL (业务数据库) Redis(用于存放热点数据,和支持S ...

  8. 2.9 PE结构:重建导入表结构

    脱壳修复是指在进行加壳保护后的二进制程序脱壳操作后,由于加壳操作的不同,有些程序的导入表可能会受到影响,导致脱壳后程序无法正常运行.因此,需要进行修复操作,将脱壳前的导入表覆盖到脱壳后的程序中,以使程 ...

  9. docker bridge网络类型研究

    bridge模式是docker的默认网络模式,使用docker run -p时,docker实际是在iptables做了DNAT规则,实现端口转发功能.可以使用iptables -t nat -vnL ...

  10. CAS中ABA问题的解决

    转自(here)   CAS问题的产生 在运用CAS做Lock-Free操作中有一个经典的ABA问题: 线程1准备用CAS将变量的值由A替换为B,在此之前,线程2将变量的值由A替换为C,又由C替换为A ...