BZOJ 4552 [Tjoi2016&Heoi2016]排序

二分+线段树

本蒟蒻完全想不到二分啊。。。

二分答案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);
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值