BZOJ5194 雪地靴

排序后dsu维护

#pragma GCC optimize("-Ofast")
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define N 400007
using namespace std;
int fa[N],l[N],r[N],p[N],n,b,tot=1,tj,usd[N],ans,anw[N];
pii u[N],f[N];
int gf(int x){
    return x^fa[x]?fa[x]=gf(fa[x]):x;
}
template <class T>
inline void read(T &x){
    static char c;
    for (c=getchar();!isdigit(c);c=getchar());
    for (x=0;isdigit(c);c=getchar())x=x*10+c-48;
}
void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
signed main () {
    read(n); read(b);
    for (int i=1;i<=n;i++) {
       read(f[i].fi); 
       f[i].se=i; fa[i]=i;
    }
    for (int i=1;i<=b;i++) {
        read(u[i].fi); read(p[i]);
        u[i].se=i;
    }
    sort(u+1,u+b+1);
    sort(f+1,f+n+1); 
    tot=n;
    for (int i=b;i;i--) {
        while (u[i].fi<f[tot].fi) {
            l[f[tot].se]=r[f[tot].se]=f[tot].se;
            if (usd[f[tot].se-1]) {
                tj=gf(f[tot].se-1);
                fa[tj]=f[tot].se; 
                l[f[tot].se]=l[tj];
            } 
            if (usd[f[tot].se+1]) {
                tj=gf(f[tot].se+1);
                fa[tj]=f[tot].se; 
                r[f[tot].se]=r[tj];
            } usd[f[tot].se]=1;
            ans=max(ans,r[f[tot].se]-l[f[tot].se]+1);
            tot--;
        } 
        anw[u[i].se]=ans<p[u[i].se];
    }
    for (int i=1;i<=b;i++) writeln(anw[i]);
    return 0;
}

 

posted @ 2018-09-04 20:59  泪寒之雪  阅读(220)  评论(0编辑  收藏  举报