bzoj4552: [Tjoi2016&Heoi2016]排序

链接

  http://www.lydsy.com/JudgeOnline/problem.php?id=4552

填坑

  今天七道题(6/7)。(看来是做不完了)

题解

  二分答案 mid ,问题就成了判断第Q个数是不是大于等于 mid ,把小于 mid 的变成0,大于 mid 的变成1,排序过程就简化了,直接用线段树的区间赋值操作模拟就好了。最后check的结果就是Q这个位置上的数是否是1。

代码

//线段树
#include <cstdio>
#include <algorithm>
#define maxn 100010
using namespace std;
int N, M, opt[maxn][3], ndtot, num[maxn], c[maxn], Q;
struct segtree
{
    int l, r, set, c0, c1;
    segtree *ch[2];
}pool[maxn<<2], *root;
inline void pushdown(segtree *p)
{
    if(p->set==0)p->c0=p->r-p->l+1, p->c1=0;
    else if(p->set==1)p->c0=0,p->c1=p->r-p->l+1;
    else return;
    if(p->ch[0])p->ch[0]->set=p->ch[1]->set=p->set;
    p->set=-1;
}
inline void pushup(segtree *p)
{
    if(!p->ch[0])return;
    pushdown(p->ch[0]),pushdown(p->ch[1]);
    p->c0=p->ch[0]->c0+p->ch[1]->c0;
    p->c1=p->ch[0]->c1+p->ch[1]->c1;
}
void segset(segtree *p, int l, int r, int c)
{
    pushdown(p);
    int mid=(p->l+p->r)>>1;
    if(l<=p->l and r>=p->r){p->set=c;return;}
    if(l<=mid)segset(p->ch[0],l,r,c);
    if(r>mid)segset(p->ch[1],l,r,c);
    pushup(p);
}
int segcnt(segtree *p, int l, int r, int c)
{
    pushdown(p);
    int mid=(p->l+p->r)>>1, ans=0;
    if(l<=p->l and r>=p->r)return c?p->c1:p->c0;
    if(l<=mid)ans+=segcnt(p->ch[0],l,r,c);
    if(r>mid)ans+=segcnt(p->ch[1],l,r,c);
    return ans;
}
void build(segtree *p, int l, int r)
{
    int mid=(l+r)>>1;
    p->l=l,p->r=r;p->set=-1;
    if(l==r)
    {
        if(c[l]==0)p->c0=1,p->c1=0;
        else p->c0=0,p->c1=1;
        return;
    }
    build(p->ch[0]=pool+ ++ndtot,l,mid);
    build(p->ch[1]=pool+ ++ndtot,mid+1,r);
    pushup(p);p->set=-1;
}
inline int read(int x=0)
{
    char c=getchar();
    while(c<48 or c>57)c=getchar();
    while(c>=48 and c<=57)x=(x<<1)+(x<<3)+c-48,c=getchar();
    return x;
}
inline void input()
{
    int i;
    N=read(), M=read();
    for(i=1;i<=N;i++)num[i]=read();
    for(i=1;i<=M;i++)opt[i][0]=read(), opt[i][1]=read(), opt[i][2]=read();
    Q=read();
}
inline bool check(int x)
{
    int i, l, r, type, a, b;
    for(i=1;i<=N;i++)c[i]=num[i]>=x;
    build(root=pool+(ndtot=1),1,N);
    for(i=1;i<=M;i++)
    {
        type=opt[i][0], l=opt[i][1], r=opt[i][2];
        a=segcnt(root,l,r,0), b=segcnt(root,l,r,1);
        if(type==0)
        {
            if(a)segset(root,l,l+a-1,0);if(b)segset(root,r-b+1,r,1);
        }
        else
        {
            if(b)segset(root,l,l+b-1,1);if(a)segset(root,r-a+1,r,0);
        }
    }
    return (bool)segcnt(root,Q,Q,1);
}
int main()
{
    int l, r, mid;
    input();
    for(l=1,r=N;l<r;)
    {
        mid=(l+r+1)>>1;
        if(check(mid))l=mid;
        else r=mid-1;
    }
    printf("%d",l);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值