2018.08.01 BZOJ4552: [Tjoi2016&Heoi2016]排序(二分+线段树)

传送门

线段树简单题。

二分答案+线段树排序。

实际上就是二分答案mid" role="presentation" style="position: relative;">midmid,然后把比mid" role="presentation" style="position: relative;">midmid小的全部变成0" role="presentation" style="position: relative;">00,不比mid" role="presentation" style="position: relative;">midmid小的全部变成1" role="presentation" style="position: relative;">11,然后用线段树维护01" role="presentation" style="position: relative;">0101排序。

时间效率O(mlogn)" role="presentation" style="position: relative;">O(mlogn)O(mlogn)。

代码:

#include<bits/stdc++.h>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define N 100005
using namespace std;
int n,m,a[N],b[N],l,r,pos;
struct Node{int l,r,cov,sum;}T[N<<2];
struct Query{int op,l,r;}q[N];
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
inline void pushup(int p){T[p].sum=T[lc].sum+T[rc].sum;}
inline void pushnow(int p,int v){T[p].cov=v,T[p].sum=(T[p].r-T[p].l+1)*v;}
inline void pushdown(int p){if(T[p].cov!=-1)pushnow(lc,T[p].cov),pushnow(rc,T[p].cov),T[p].cov=-1;}
inline void build(int p,int l,int r){
    T[p].l=l,T[p].r=r,T[p].cov=-1;
    if(l==r){T[p].sum=b[l];return;}
    build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int ql,int qr,int v){
    if(ql>T[p].r||qr<T[p].l)return;
    if(ql<=T[p].l&&T[p].r<=qr)return pushnow(p,v);
    pushdown(p);
    if(qr<=mid)update(lc,ql,qr,v);
    else if(ql>mid)update(rc,ql,qr,v);
    else update(lc,ql,mid,v),update(rc,mid+1,qr,v);
    pushup(p);
}
inline int query(int p,int ql,int qr){
    if(ql>T[p].r||qr<T[p].l)return 0;
    if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
    pushdown(p);
    if(qr<=mid)return query(lc,ql,qr);
    if(ql>mid)return query(rc,ql,qr);
    return query(lc,ql,mid)+query(rc,mid+1,qr);
}
inline bool check(int x){
    for(int i=1;i<=n;++i)b[i]=(a[i]>=x);
    build(1,1,n);
    for(int i=1;i<=m;++i){
        int num=query(1,q[i].l,q[i].r);
        if(!q[i].op)update(1,q[i].r-num+1,q[i].r,1),update(1,q[i].l,q[i].r-num,0);
        else update(1,q[i].l,q[i].l+num-1,1),update(1,q[i].l+num,q[i].r,0);
    }
    return query(1,pos,pos);
}
int main(){
    n=read(),m=read(),l=1,r=n;
    for(int i=1;i<=n;++i)a[i]=read();
    for(int i=1;i<=m;++i)q[i].op=read(),q[i].l=read(),q[i].r=read();
    pos=read();
    while(l<=r){
        int midd=l+r>>1;
        if(check(midd))l=midd+1;
        else r=midd-1;
    }
    if(check(r))printf("%d",r);
    else printf("%d",l);
    return 0;
}
上一篇:【struts2基础】配置详解


下一篇:OpensStack instance debug