• The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I


    传送门

    因为某些原因,所以我就去学了 $LCT$ 维护直径, $LCT$ 维护直径我上一个博客讲得很详细了:传送门

    这里维护虚儿子用的是 $multiset$ ,没写可删堆

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<set>
    #include<queue>
    using namespace std;
    typedef long long ll;
    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<<1)+(x<<3)+(ch^48); ch=getchar(); }
        return x*f;
    }
    const int N=8e5+7;
    const ll INF=1e18;
    int n,m;
    struct edge {
        int x,y;
    }e[N];
    namespace LCT {
        int c[N][2],fa[N],sz[N],val[N];
        ll sum[N],lmx[N],rmx[N],mxs[N];
        bool rev[N];
        multiset <ll> H[N],P[N];
        inline void ins(int u,int v) { H[u].insert(lmx[v]); P[u].insert(mxs[v]); }
        inline void del(int u,int v) { H[u].erase(H[u].find(lmx[v])); P[u].erase(P[u].find(mxs[v])); }
        inline ll fir(multiset <ll> &S) { return S.size() ? *S.rbegin() : -INF; }
        inline ll sec(multiset <ll> &S) { return S.size()>1 ? *(++S.rbegin()) : -INF; }
        inline void pushup(int x)
        {
            int &lc=c[x][0],&rc=c[x][1];
            sum[x]=sum[lc]+sum[rc]+val[x];
            ll t=max(0ll,fir(H[x])), L=max(t,rmx[lc])+val[x], R=max(t,lmx[rc])+val[x];
            lmx[x]=max(lmx[lc], sum[lc]+R ); rmx[x]=max(rmx[rc], sum[rc]+L );
            mxs[x]=max( max( rmx[lc]+R , lmx[rc]+L ) , max(mxs[lc],mxs[rc]) );
            mxs[x]=max(mxs[x],fir(P[x])); mxs[x]=max(mxs[x], t+val[x] );
            mxs[x]=max(mxs[x], t+val[x]+sec(H[x]) );
        }
        inline void pushdown(int x)
        {
            if(!x||!rev[x]) return;
            int &lc=c[x][0],&rc=c[x][1]; rev[x]=0;
            swap(lc,rc); swap(lmx[x],rmx[x]);
            if(lc) rev[lc]^=1;
            if(rc) rev[rc]^=1;
        }
        inline void rever(int x) { rev[x]=1; pushdown(x); }
        inline bool noroot(int x) { return (c[fa[x]][0]==x)|(c[fa[x]][1]==x); }
        inline void rotate(int x)
        {
            int y=fa[x],z=fa[y],d=(c[y][1]==x);
            if(noroot(y)) c[z][c[z][1]==y]=x;
            fa[x]=z; fa[y]=x; fa[c[x][d^1]]=y;
            c[y][d]=c[x][d^1]; c[x][d^1]=y;
            pushup(y);
        }
        void push_rev(int x)
        {
            if(noroot(x)) push_rev(fa[x]);
            else pushdown(x);
            pushdown(c[x][0]); pushdown(c[x][1]);
        }
        inline void splay(int x)
        {
            push_rev(x);
            while(noroot(x))
            {
                int y=fa[x],z=fa[y];
                if(noroot(y))
                    (c[y][0]==x ^ c[z][0]==y) ? rotate(x) : rotate(y);
                rotate(x);
            } pushup(x);
        }
        inline void access(int x)
        {
            for(int y=0;x;y=x,x=fa[x])
            {
                splay(x); if(y) del(x,y);
                if(c[x][1]) ins(x,c[x][1]);
                c[x][1]=y; pushup(x);
            }
        }
        inline void makeroot(int x) { access(x); splay(x); rever(x); }
        inline int findroot(int x)
        {
            access(x); splay(x); pushdown(x);
            while(c[x][0]) x=c[x][0],pushdown(x);
            splay(x); return x;
        }
        inline void split(int x,int y) { makeroot(x); access(y); splay(y); }
        inline void link(int x,int y)
        {
            makeroot(x); if(findroot(y)==x) return;
            makeroot(y); fa[x]=y; ins(y,x); pushup(y);
        }
        inline void cut(int x,int y)
        {
            makeroot(x);
            if(findroot(y)!=x||fa[y]!=x||c[y][0]) return;
            fa[y]=c[x][1]=0; pushup(x);
        }
        inline void Link(int k) { link(e[k].x,n+k); link(e[k].y,n+k); }
        inline void Cut(int k) { cut(e[k].x,n+k); cut(e[k].y,n+k); }
        inline ll query(int x) { access(x); splay(x); return rmx[x]; }
    }
    int main()
    {
        n=read();
        for(int i=1;i<n;i++)
            e[i].x=read(),e[i].y=read(),LCT::val[n+i]=read();
        for(int i=1;i<n;i++) LCT::Link(i);
        m=read(); char s[7]; int a;
        for(int i=1;i<=m;i++)
        {
            scanf("%s",s); a=read();
            if(s[0]=='Q') printf("%lld
    ",LCT::query(a));
            else LCT::Cut(a),LCT::val[n+a]=read(),LCT::Link(a);
        }
        return 0;
    }
  • 相关阅读:
    C#三种模拟自动登录和提交POST信息的实现方法
    关于c#调用C++代码的一些应用的体会
    c# 路径空格---ProcessStartInfo参数问题
    用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器
    使用C#开发纽曼USB来电小秘书客户端小结
    C#传递参数到线程的n个方法
    C# : 操作Word文件的API
    c# 导出Excel
    使用C#: 自动切换鼠标的左右手习惯
    Linux 系统的文件类型
  • 原文地址:https://www.cnblogs.com/LLTYYC/p/11534195.html
Copyright © 2020-2023  润新知