洛谷P2824:[HEOI2016]排序 (二分答案+线段树)

题目传送门:https://www.luogu.org/problemnew/show/P2824


题目分析:这题是我上生物课的时候花20分钟想到的。

我们可以像整体二分那样,先枚举一个数作为标准,然后只关心a序列中的每个数比这个标准大还是小,小于等于记为0,大于记为1。假设某一次操作要让一个区间升序排序,我们就用线段树先查一下这个区间有几个0和1,然后用懒惰操作将0放在区间左边,1放在区间右边。降序排序同理。

最后查一下第q位的数是0还是1,就可以知道最后这一位的数是比当前标准大还是小。这样我们就可以将最外层的枚举标准改为二分,时间复杂度 O(nlog2(n))

虽然这个思想和整体二分时候的思想差不多,但我觉得这题不能支持多组询问整体二分,因为单词操作的时间是和m有关的。


CODE:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxn=30100;

struct Tnode
{
    int num,lazy;
} tree[maxn<<2];

struct data
{
    int id,l,r;
} work[maxn];

int a[maxn];
int n,m,q;

void Build(int root,int L,int R,int val)
{
    tree[root].lazy=-1;
    if (L==R)
    {
        if (a[L]<=val) tree[root].num=0;
        else tree[root].num=1;
        return;
    }

    int mid=(L+R)>>1;
    int Left=root<<1;
    int Right=Left|1;

    Build(Left,L,mid,val);
    Build(Right,mid+1,R,val);

    tree[root].num=tree[Left].num+tree[Right].num;
}

void Down(int root,int L,int R)
{
    if (tree[root].lazy!=-1)
    {
        int mid=(L+R)>>1;
        int Left=root<<1;
        int Right=Left|1;

        tree[Left].lazy=tree[root].lazy;
        tree[Left].num=tree[Left].lazy*(mid-L+1);

        tree[Right].lazy=tree[root].lazy;
        tree[Right].num=tree[Right].lazy*(R-mid);

        tree[root].lazy=-1;
    }
}

int Query(int root,int L,int R,int x,int y)
{
    if ( y<L || R<x ) return 0;
    if ( x<=L && R<=y ) return tree[root].num;

    Down(root,L,R);

    int mid=(L+R)>>1;
    int Left=root<<1;
    int Right=Left|1;

    int vl=Query(Left,L,mid,x,y);
    int vr=Query(Right,mid+1,R,x,y);
    return (vl+vr);
}

void Update(int root,int L,int R,int x,int y,int v)
{
    if ( y<L || R<x ) return;
    if ( x<=L && R<=y )
    {
        tree[root].lazy=v;
        tree[root].num=(R-L+1)*v;
        return;
    }

    Down(root,L,R);

    int mid=(L+R)>>1;
    int Left=root<<1;
    int Right=Left|1;

    Update(Left,L,mid,x,y,v);
    Update(Right,mid+1,R,x,y,v);

    tree[root].num=tree[Left].num+tree[Right].num;
}

bool Judge(int val)
{
    Build(1,1,n,val);

    for (int i=1; i<=m; i++)
    {
        int L=work[i].l,R=work[i].r;
        int cnt=Query(1,1,n,L,R);

        if (work[i].id)
        {
            if (cnt) Update(1,1,n,L,L+cnt-1,1);
            if (cnt<R-L+1) Update(1,1,n,L+cnt,R,0);
        }
        else
        {
            cnt=(R-L+1)-cnt;
            if (cnt) Update(1,1,n,L,L+cnt-1,0);
            if (cnt<R-L+1) Update(1,1,n,L+cnt,R,1);
        }
    }

    int cnt=Query(1,1,n,q,q);
    return cnt;
}

int Binary()
{
    int L=0,R=n;
    while (L+1<R)
    {
        int mid=(L+R)>>1;
        if ( Judge(mid) ) L=mid;
        else R=mid;
    }
    return R;
}

int main()
{
    freopen("2824.in","r",stdin);
    freopen("2824.out","w",stdout);

    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",&work[i].id,&work[i].l,&work[i].r);
    scanf("%d",&q);

    int ans=Binary();
    printf("%d\n",ans);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值