二分+线段树
本蒟蒻完全想不到二分啊。。。
二分答案x。设小于等于x的值为0,大于x的值为1。排序的时候我们只关心x是否会出现在P位置上,而x的位置只和区间内小于它的数和大于它的数有关,所以不关心其他的值具体是多少,直接线段树维护赋值查询即可。
#include<cstdio>
#define N 100005
using namespace std;
struct opt
{
int l, r,type;
}op[N];
struct SegmentTree
{
int l, r, v, lazy;//v为1的数量
}t[N*5];
int a[N], b[N], n, m, pos;
void pushdown(int x)
{
if(t[x].lazy==-1)return;
t[x<<1].v=t[x].lazy*(t[x<<1].r-t[x<<1].l+1);
t[x<<1|1].v=t[x].lazy*(t[x<<1|1].r-t[x<<1|1].l+1);
t[x<<1].lazy=t[x<<1|1].lazy=t[x].lazy;
t[x].lazy=-1;
}
void build(int x, int l, int r)
{
t[x].l=l;
t[x].r=r;
t[x].lazy=-1;
if(l==r)
{
t[x].v = b[l];
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
t[x].v=t[x<<1].v+t[x<<1|1].v;
}
int query(int x, int l, int r)
{
pushdown(x);
if(l<=t[x].l&&t[x].r<=r)
return t[x].v;
int ret=0,mid=(t[x].l+t[x].r)>>1;
if(l<=mid)ret+=query(x<<1,l,r);
if(mid+1<=r)ret+=query(x<<1|1,l,r);
return ret;
}
void update(int x, int l, int r, int v)
{
pushdown(x);
if(l<=t[x].l&&t[x].r<=r)
{
t[x].v=v*(t[x].r-t[x].l+1);
t[x].lazy=v;
return;
}
int mid=(t[x].l+t[x].r)>>1;
if(l<=mid)update(x<<1,l,r,v);
if(mid+1<=r)update(x<<1|1,l,r,v);
t[x].v=t[x<<1].v+t[x<<1|1].v;
}
bool check(int x)
{
for(int i = 1; i <= n; i++)
b[i]=a[i]>x?1:0;
build(1,1,n);
for(int i = 1; i <= m; i++)
{
int cnt = query(1,op[i].l,op[i].r);
if(op[i].type==0)
{
update(1,op[i].l,op[i].r-cnt,0);
update(1,op[i].r-cnt+1,op[i].r,1);
}
else
{
update(1,op[i].l,op[i].l+cnt-1,1);
update(1,op[i].l+cnt,op[i].r,0);
}
}
return query(1,pos,pos);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for(int i = 1; i <= m; i++)
scanf("%d%d%d",&op[i].type,&op[i].l,&op[i].r);
scanf("%d",&pos);
int l = 1, r = n;
while(l<r)
{
int mid=(l+r)>>1;
if(check(mid))l=mid+1;
else r=mid;
}
printf("%d\n",l);
}