[HEOI / TJOI2016] Sort [segment tree 01]

[HEOI2016 / TJOI2016] Sort]

luogu2824 HEOI2016 / TJOI2016] Sort BZOJ4552

== obviously can not read the title of the first row directly out of the reaction first off the inquiry kept down

This is a full two points aligned position (Q \) \ numbers on

Each \ (check (mid) \) to build an array \ (B \) to change the point value if the record \ (a [i]> mid \) then \ (b [i] = 1 \) Other \ (B [ i] = 0 \)

Of interval \ ([l, r] \ ) in ascending order will interval \ ([l, r-cnt ] \) whole to 1, the interval \ ([r-cnt + 1 , r] \) whole to 0 \ ((CNT = number of the interval [l, r] in 1) \) descending Similarly

then.....

01 This method is really useful! Very clever!

#include<bits/stdc++.h>
using namespace std;
#define ls (o<<1)
#define rs (o<<1|1)
const int N=1e5+5,M=32000+5,inf=0x3f3f3f3f;
int n,m,Q,a[N],b[N];
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct node{int sum,tag;}t[N<<2];
struct Node{int opt,l,r;}q[N];
void pup(int o){
    t[o].sum=t[ls].sum+t[rs].sum;
}
void updnode(int o,int l,int r,int k){t[o].tag=k,t[o].sum=(r-l+1)*k;}
void pudw(int o,int l,int r){
    int mid=l+r>>1;
    if(t[o].tag!=-1) updnode(ls,l,mid,t[o].tag),updnode(rs,mid+1,r,t[o].tag),t[o].tag=-1;
}
void mdf(int o,int l,int r,int x,int y,int k){
    if(l>y||r<x) return;
    if(x<=l&&r<=y){updnode(o,l,r,k);return;}
    pudw(o,l,r);
    int mid=l+r>>1;
    mdf(ls,l,mid,x,y,k),mdf(rs,mid+1,r,x,y,k);
    pup(o);
}
int query(int o,int l,int r,int x,int y){
    if(l>y||r<x) return 0;
    if(x<=l&&r<=y) return t[o].sum;
    pudw(o,l,r);
    int mid=l+r>>1,ans=0;
    ans+=query(ls,l,mid,x,y)+query(rs,mid+1,r,x,y);
    pup(o);
    return ans;
}
void build(int o,int l,int r){
    t[o].tag=-1;
    if(l==r){t[o].sum=b[l];return;}
    int mid=l+r>>1;
    build(ls,l,mid),build(rs,mid+1,r);
    pup(o);
}

bool check(int mid){
    for(int i=1;i<=n;++i) b[i]=(a[i]>mid);
    build(1,1,n);
    for(int i=1;i<=m;++i){
        int opt=q[i].opt,l=q[i].l,r=q[i].r,cnt;
        cnt=query(1,1,n,l,r);
        if(!opt){
            if(cnt==r-l+1||!cnt) continue;
            mdf(1,1,n,l,r-cnt,0),mdf(1,1,n,r-cnt+1,r,1);
        }
        else{
            if(cnt==r-l+1||!cnt) continue;
            mdf(1,1,n,l,l+cnt-1,1),mdf(1,1,n,l+cnt,r,0);
        }
    }
    return query(1,1,n,Q,Q);
}

int main(){
    freopen("in.txt","r",stdin);
//  freopen("numbers.out","w",stdout);
    rd(n),rd(m);
    for(int i=1;i<=n;++i) rd(a[i]);
    for(int i=1;i<=m;++i) rd(q[i].opt),rd(q[i].l),rd(q[i].r);
    rd(Q);
    int l=0,r=n,mid;
    while(l<r){
        mid=l+r>>1;
        if(check(mid)) l=mid+1;
        else r=mid;
    }
    printf("%d\n",r);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11447592.html