luogu_P1712 [NOI2016] Interval

Portal: https://www.luogu.org/problem/P1712

I learned a universal group is said to be thinking of - take-foot law. (Specifically forgot ...... what seems to be a very good method of enumeration.

This question is the first section in accordance with a sequence length of the row interval (it seems to have many problems monotonicity very simple

Then the intervals were calculated to answer in (what is calculated to contribute in? This is the point where the range is covered by the number ++

And when there appeared to reach a point m times and then update the answer

Prior to joining the interval contribution then subtracting (similar queue .......... Why can directly contribute minus? Because useless ....... interval length behind only longer while it is shorter than the others ...... so useless right

This question is then discrete look, maintain the maximum number of times covered with tree line on it

#include<cstdio>
#include<algorithm>
#define R register
#define ls(p) p<<1
#define rs(p) p<<1|1
using namespace std;
int n,m,lsh[1001000],mx,ans=1e9;
struct qqq{
    int l,r,len;
    inline bool operator <(const qqq i)  const{
        return len<i.len;
    }
}ln[500100];
struct ddd{
    int l,r,lazy,cnt;
}t[4001000];
inline void down(int p){
    if(t[p].lazy){
        t[ls(p)].cnt+=t[p].lazy;
        t[ls(p)].lazy+=t[p].lazy;
        t[rs(p)].cnt+=t[p].lazy;
        t[rs(p)].lazy+=t[p].lazy;
        t[p].lazy=0;
    }
}
inline void update(int p){
    t[p].cnt=max(t[ls(p)].cnt,t[rs(p)].cnt);
    return;
}
inline void build(int p,int l,int r){
    t[p].l=l;t[p].r=r;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(ls(p),l,mid);
    build(rs(p),mid+1,r);
    return;
}
inline void change(int p,int l,int r,int v){
    if(t[p].l>=l&&t[p].r<=r){
        t[p].cnt+=v;
        t[p].lazy+=v;
        return;
    }
    down(p);
    int mid=(t[p].l+t[p].r)>>1;
    if(mid>=l) change(ls(p),l,r,v);
    if(mid< r) change(rs(p),l,r,v);
    update(p);
}
int main (){
    scanf("%d%d",&n,&m);
    for(R int i=1;i<=n;i++){
        scanf("%d%d",&ln[i].l,&ln[i].r);
        lsh[++mx]=ln[i].l;lsh[++mx]=ln[i].r;
        ln[i].len=ln[i].r-ln[i].l;
    }
    sort(lsh+1,lsh+1+mx);
    mx=unique(lsh+1,lsh+1+mx)-lsh-1;
    build(1,1,mx);
    for(R int i=1;i<=n;i++){
        ln[i].l=lower_bound(lsh+1,lsh+1+mx,ln[i].l)-lsh;
        ln[i].r=lower_bound(lsh+1,lsh+1+mx,ln[i].r)-lsh;
    }
    sort(ln+1,ln+1+n);
    int li=0;
    for(R int i=1;i<=n;i++){
        change(1,ln[i].l,ln[i].r,1);
        if(t[1].cnt==m){
            while(t[1].cnt==m){
                li++;
                change(1,ln[li].l,ln[li].r,-1);
            }
            ans=min(ans,ln[i].len-ln [li] .len);    
        } 
    } 
    Printf ( " % d \ n " , 1e9 years == - 1 : years);
    return  0 ; 
}
View Code

 

Guess you like

Origin www.cnblogs.com/coclhy/p/11693958.html