[bzoj3545]Peaks

题目大意

一个图,有边权和点权。若干个询问形如询问从点x出发只能走边权不超过y的边,走到所有点点权第k大是多少。

离线大法好

显然按照边权从小到大排序,用平衡树启发式合并做就好了。
TLE了,不开森。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<ctime>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=500000+10;
int key[maxn],father[maxn],tree[maxn][2],size[maxn],ans[maxn],fa[maxn],dl[maxn];
int b[11];
struct dong{
    int type,x,y,z,id;
};
dong ask[maxn*2];
int i,j,k,l,t,n,m,q,top;
bool cmp(dong a,dong b){
    if (a.z<b.z) return 1;
    else if (a.z==b.z&&a.type<b.type) return 1;
    else return 0;
}
int getfa(int x){
    return fa[x]?fa[x]=getfa(fa[x]):x;
}
int pd(int x){
    return tree[father[x]][1]==x;
}
void update(int x){
    size[x]=size[tree[x][0]]+size[tree[x][1]]+1;
}
void insert(int &x,int y){
    if (!x){
        x=y;
        return;
    }
    if (key[y]>key[x]){
        insert(tree[x][0],y);
        father[tree[x][0]]=x;
    }
    else{
        insert(tree[x][1],y);
        father[tree[x][1]]=x;
    }
    update(x);
}
void rotate(int x){
    int y=father[x],z=pd(x);
    father[x]=father[y];
    if (father[y]) tree[father[y]][pd(y)]=x;
    tree[y][z]=tree[x][1-z];
    if (tree[x][1-z]) father[tree[x][1-z]]=y;
    tree[x][1-z]=y;
    father[y]=x;
    update(y);
    update(x);
}
void splay(int x,int y){
    while (father[x]!=y){
        if (father[father[x]]!=y)
            if (pd(x)==pd(father[x])) rotate(father[x]);else rotate(x);
        rotate(x);
    }
}
void merge(int x,int y){
    if (size[x]>size[y]) swap(x,y);
    dl[1]=x;
    k=l=1;
    int now;
    while (k<=l){
        now=dl[k++];
        father[tree[now][0]]=father[tree[now][1]]=0;
        if (tree[now][0]) dl[++l]=tree[now][0];
        if (tree[now][1]) dl[++l]=tree[now][1];
        tree[now][0]=tree[now][1]=0;
        insert(y,now);
        splay(now,0);
        y=now;
    }
}
int kth(int x,int y){
    if (y==size[tree[x][0]]+1) return x;
    else if (y<=size[tree[x][0]]) return kth(tree[x][0],y);
    else return kth(tree[x][1],y-size[tree[x][0]]-1);
}
int read(){
    int x=0;
    char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
void write(int x){
    if (x<0){
        printf("-");
        x=-x;
    }
    top=0;
    while (x) b[++top]=x%10,x/=10;
    while (top) putchar(b[top]+'0'),top--;
    printf("\n");
}
int main(){
    freopen("3545.in","r",stdin);freopen("3545.out","w",stdout);
    n=read();m=read();q=read();
    fo(i,1,n){
        key[i]=read();
        size[i]=1;
    }
    fo(i,1,m)
        ask[i].x=read(),ask[i].y=read(),ask[i].z=read(),ask[i].type=1;
    fo(i,1,q)
        ask[i+m].x=read(),ask[i+m].z=read(),ask[i+m].y=read(),ask[i+m].type=2,ask[i+m].id=i;
    sort(ask+1,ask+m+q+1,cmp);
    fo(i,1,m+q){
        if (ask[i].type==1){
            if (getfa(ask[i].x)!=getfa(ask[i].y)){
                j=ask[i].x;
                k=ask[i].y;
                splay(j,0);
                splay(k,0);
                merge(j,k);
                fa[getfa(ask[i].y)]=getfa(ask[i].x);
            }
        }
        else{
            j=ask[i].x;
            splay(j,0);
            if (size[j]<ask[i].y) ans[ask[i].id]=-1;
            else ans[ask[i].id]=key[kth(j,ask[i].y)];
        }
    }
    fo(i,1,q) write(ans[i]);
    //printf("\n%d",clock());
    fclose(stdin);fclose(stdout);
    return 0;
}

你可能感兴趣的:([bzoj3545]Peaks)