[BZOJ2594][Wc2006]水管局长数据加强版

原题地址

艾玛这题卡了我一晚上…

各种RE,WA,TLE,总结一下死因:

  1. splay()的清标记处写搓…
  2. 数组开小/开大(没看清范围)…
  3. 用map…

完蛋了完蛋了马上就要NOIP了还这种状态怎么办啊OLZ

AC code:

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100010;
const int M=1100010;
int n,m,q,cnt,tot;
int k[N],x[N],y[N],f[M],p1[M],p2[M],ans[N];

struct Edge{
    bool tag;
    int  u,v,w;

    friend bool operator<(Edge x,Edge y){
        if(x.u!=y.u) return x.u<y.u;
        return x.v<y.v;
    }
    friend bool operator==(Edge x,Edge y){
        return x.v==y.v&&x.u==y.u;
    }
}E[M];

struct nod{
    bool rev;
    int  num,val,mxv,mxn;
    nod  *pp,*pr,*ch[2];
};
nod poi[M];
nod *NIL;
nod *stk[M];

struct LCT{
    LCT(){
        NIL=&poi[0];
        for(int i=0;i<=n;i++) newnod(i,0);
    }

    void newnod(int num,int val){
        poi[num].rev=0;
        poi[num].num=poi[num].mxn=num;
        poi[num].val=poi[num].mxv=val;
        poi[num].pp=poi[num].pr=poi[num].ch[0]=poi[num].ch[1]=NIL;
    }
    void clear(nod *x){
        if(x==NIL||(!x->rev)) return ;
        nod *t=x->ch[0];x->ch[0]=x->ch[1];x->ch[1]=t;
        x->rev=0;x->ch[0]->rev^=1;x->ch[1]->rev^=1;
    }
    void update(nod *x){
        bool t=x->ch[0]->mxv<x->ch[1]->mxv;
        x->mxv=x->ch[t]->mxv;x->mxn=x->ch[t]->mxn;
        if(x->val>x->mxv) x->mxv=x->val,x->mxn=x->num;
    }
    void rotate(nod *x,bool t){
        nod *y=x->pr,*z=y->pr,*b=x->ch[t^1];
        b->pr=y;y->pr=x;x->pr=z;y->ch[t]=b;x->ch[t^1]=y;
        if(z->ch[0]==y) z->ch[0]=x;
        if(z->ch[1]==y) z->ch[1]=x;
        update(y);update(x);
    }
    void splay(nod *x){
        int t=0;
        nod *r=x;
        for(nod *i=x;i!=NIL;i=i->pr) stk[++t]=i;
        for(int i=t;i>0;i--) clear(stk[i]);
        while(x->pr!=NIL){
            nod *y=x->pr,*z=y->pr;
            r=(z==NIL?y:z);
            if(z==NIL&&y->ch[0]==x) rotate(x,0);
            else if(z==NIL&&y->ch[1]==x) rotate(x,1);
            else if(z->ch[0]==y&&y->ch[0]==x) {rotate(y,0);rotate(x,0);}
            else if(z->ch[1]==y&&y->ch[1]==x) {rotate(y,1);rotate(x,1);}
            else if(z->ch[1]==y&&y->ch[0]==x) {rotate(x,0);rotate(x,1);}
            else {rotate(x,1);rotate(x,0);}
        }
        x->pp=r->pp;
    }
    void access(nod *x){
        for(nod *u=x,*v=NIL;u!=NIL;v=u,u=u->pp)
            {splay(u);u->ch[1]->pp=u;u->ch[1]->pr=NIL;u->ch[1]=v;v->pr=u;update(u);}
    }
    void makeroot(nod *x){
        access(x);splay(x);x->rev^=1;
    }
    void link(int u,int v){
        nod *x=&poi[u],*y=&poi[v];
        makeroot(x);x->pp=y;
    }
    void cut(int u,int v){
        nod *x=&poi[u],*y=&poi[v];
        if(lca(u,v)==u) {nod *t=x;x=y;y=t;}
        access(x);splay(x);x->ch[0]->pr=x->ch[0]->pp=NIL;x->ch[0]=NIL;update(x);
    }
    int lca(int u,int v){
        nod *x=&poi[u],*y=&poi[v];
        access(x);access(y);splay(x);
        if(x->pp!=NIL) return x->pp->num;
        return x->num;
    }
    int getmxn(int u,int v){
        nod *x=&poi[u],*y=&poi[v];
        makeroot(x);access(y);splay(y);
        return y->mxn;
    }
};

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int find(int x){
    return f[x]==x?x:f[x]=find(f[x]);
}

void swap(int *x,int *y){
    int t=*x;*x=*y;*y=t;
}

int get(int u,int v){
    int  L=0,R=m+1,M;
    Edge tmp;
    tmp.u=u;tmp.v=v;
    while(L+1!=R){
        M=(L+R)>>1;
        if(E[M]<tmp||E[M]==tmp) L=M;
        else R=M;
    }
    return L;
}

int main(){
    n=read(),m=read(),q=read();
    for(int i=1;i<M;i++) f[i]=i;
    LCT T;
    for(int i=1;i<=m;i++){
        E[i].u=read(),E[i].v=read(),E[i].w=read();
        if(E[i].u>E[i].v) swap(&E[i].u,&E[i].v);
    }
    sort(E+1,E+m+1);
    for(int i=1;i<=q;i++){
        k[i]=read();x[i]=read();y[i]=read();
        if(x[i]>y[i]) swap(&x[i],&y[i]);
        if(k[i]==2) E[get(x[i],y[i])].tag=1;
    }
    for(int i=1;i<=m;i++){
        if(E[i].tag) continue;
        else{
            if(find(E[i].u)!=find(E[i].v)){
                T.newnod(++cnt+n,E[i].w);
                p1[cnt+n]=E[i].u;p2[cnt+n]=E[i].v;
                T.link(E[i].u,cnt+n);T.link(cnt+n,E[i].v);
                f[find(E[i].u)]=f[find(E[i].v)]=find(cnt+n);
            }
            else{
                int t=T.getmxn(E[i].u,E[i].v);
                if(poi[t].val>E[i].w){
                    T.cut(t,p1[t]);T.cut(t,p2[t]);
                    T.newnod(++cnt+n,E[i].w);
                    p1[cnt+n]=E[i].u;p2[cnt+n]=E[i].v;
                    T.link(E[i].u,cnt+n);T.link(cnt+n,E[i].v);
                }
            }
        }
    }
    for(int i=q;i>0;i--){
        if(k[i]==1) ans[++tot]=poi[T.getmxn(x[i],y[i])].val;
        else{
            int val=E[get(x[i],y[i])].w;
            if(find(x[i])!=find(y[i])){
                T.newnod(++cnt+n,val);
                p1[cnt+n]=x[i];p2[cnt+n]=y[i];
                T.link(x[i],cnt+n);T.link(cnt+n,y[i]);
                f[find(x[i])]=f[find(y[i])]=find(cnt+n);
            }
            else{
                int t=T.getmxn(x[i],y[i]);
                if(poi[t].val>val){
                    T.cut(t,p1[t]);T.cut(t,p2[t]);
                    T.newnod(++cnt+n,val);
                    p1[cnt+n]=x[i];p2[cnt+n]=y[i];
                    T.link(x[i],cnt+n);T.link(cnt+n,y[i]);
                }
            }
        }
    }
    for(int i=tot;i>0;i--) printf("%d\n",ans[i]);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值