[ABC237G] Range Sort Query
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的更多相关文章
- [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 ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [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 ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 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を加算する. ...
- 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 ...
- 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 ...
- range for query
static void range_test(Args _args) { Query Query; QueryRun QueryRun ...
- 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 ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
随机推荐
- API接口开发管理平台--多领域企业数字化管理解决方案
随着数字化时代的到来,企业需要进行数字化转型才能更好地适应市场需求和用户需求.而API接口则是数字化转型中的重要组成部分,可以帮助企业更好地管理信息,提高效率.本文将介绍挖数据解决方案--API接口开 ...
- 如何平息WPS for linux启动后,笔记本风扇的怒吼
create:2022-09-06 20:02:45 WPS启动后,点击菜单栏右上角[未同步]按钮,不登录,直接关掉.几秒后,笔记本风扇嗷嗷叫.桌面conky显示wpscloudsvr进程CPU占用8 ...
- CentOS 8 无痕升级到 Rocky Linux
CentOS 8 无痕升级到 Rocky Linux 1.升级当前系统 dnf upgrade -y 2.重启当前系统: reboot 3.下载脚本: CentOS 8 到 Rocky Linux 8 ...
- 【krpano】多分类缩略图及多分类地图案例
该案例提供了场景多分类缩略图展示以及多地图展示,效果如下截图: 下载地址:http://pan.baidu.com/s/1hsA5ta8 感谢群内小伙伴H·T·T的分享 ...
- 噢耶!字节后端Offer,拿到了
很多同学反馈多搞点面经,说来就来! 今天分享一位拿到字节跳动实习Offer的面经,没错,Java转Go. 别问我选Java还是选Go,成年人不做选择题.先搞定一个语言,再学第二语言从来不是难事. 无论 ...
- 文心一言 VS 讯飞星火 VS chatgpt (101)-- 算法导论9.3 7题
七.用go语言,设计一个 O(n)时间的算法,对于一个给定的包含n个互异元素的集合 S 和一个正整数k≤n,该算法能够确定 S 中最接近中位数的k个元素. 文心一言: 要设计一个 O(n) 时间的算法 ...
- destoon9.0游戏自媒体类型综合资讯门户模板
随着时代发展,自媒体资讯适合当前的互联网情形.呕心沥血开发的一套自媒体综合门户网站模板,本模板采用纯手写开发,带会员中心.首页,列表页,内容页,搜索页面精心编写,非常大气,并配移动端.注意:模板目前只 ...
- umich cv-2-1
UMICH CV Linear Classifiers 对于使用线性分类器来进行图片分类,我们可以给出这样的参数化方法: 而对于这样一个式子,我们怎么去理解呢? 首先从代数的角度,这个f(x,W)就是 ...
- WEB项目开发流程介绍
web开发流程 web开发流程图 一.需求分析阶段 在需求分析阶段,即上图所述 "需求明确"之前的阶段 产品经理PM召集需要项目相关人员,开需求讨论会.讲解原型 相关人员需要以此了 ...
- PostgreSQL学习笔记-1.基础知识:创建、删除数据库和表格
PostgreSQL 创建数据库 PostgreSQL 创建数据库可以用以下三种方式:1.使用 CREATE DATABASE SQL 语句来创建.2.使用 createdb 命令来创建.3.使用 p ...