zoukankan      html  css  js  c++  java
  • [清华集训] 温暖会指引我们前行

    同样是LCT维护一个类似最大生成树的东西。
    题目链接:戳我
    emmm其实我在uoj上过不去,加的数据我TLE了。。。。。。
    但是bzoj上还是可以的。。。。。

    关于push_up的小trick:初始化的时候给0节点也初始化成最大值,然后push_up的时候不用管自己的左右儿子是否为空,直接返回左右儿子中比较小的一个就可以了,然后再和自己作比较。qwqwq

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define MAXN 400010
    using namespace std;
    int n,m,tot;
    int s[MAXN];
    char cur[10];
    struct Edge{int ff,val,rev,minn,sum,tmp,ch[2];}t[MAXN];
    struct Edge2{int id,u,v,a,b;}edge[MAXN];
    inline int ls(int x){return t[x].ch[0];}
    inline int rs(int x){return t[x].ch[1];}
    inline void push_up(int x)
    {   
        t[x].minn=(t[t[ls(x)].minn].tmp<t[t[rs(x)].minn].tmp?t[ls(x)].minn:t[rs(x)].minn);
        t[x].minn=(t[x].tmp<t[t[x].minn].tmp)?x:t[x].minn;
        t[x].sum=t[t[x].ch[0]].sum+t[t[x].ch[1]].sum+t[x].val;
    }
    inline bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
    inline bool cmp(struct Edge2 x,struct Edge2 y){return x.b>y.b;}
    inline void rotate(int x)
    {
        int y=t[x].ff;
        int z=t[y].ff;
        int k=t[y].ch[1]==x;
        if(!isroot(y)) t[z].ch[t[z].ch[1]==y]=x; t[x].ff=z;
        t[y].ch[k]=t[x].ch[k^1]; t[t[x].ch[k^1]].ff=y;
        t[x].ch[k^1]=y; t[y].ff=x;
        push_up(y),push_up(x);
    }
    inline void push_down(int x)
    {
        if(t[x].rev)
        {
            if(t[x].ch[0]) t[t[x].ch[0]].rev^=1;
            if(t[x].ch[1]) t[t[x].ch[1]].rev^=1;
            swap(t[x].ch[0],t[x].ch[1]);
            t[x].rev^=1;
        }
    }
    inline void splay(int x)
    {
        s[tot=1]=x;
        for(int i=x;!isroot(i);i=t[i].ff) s[++tot]=t[i].ff;
        while(tot) push_down(s[tot--]);
        while(!isroot(x))
        {
            int y=t[x].ff;
            int z=t[y].ff;
            if(!isroot(y))
                ((t[y].ch[0]==x)^(t[z].ch[0]==y))?rotate(x):rotate(y);
            rotate(x);
        }
    }
    inline void access(int x)
    {
        for(int y=0;x;y=x,x=t[x].ff)
            splay(x),t[x].ch[1]=y,push_up(x);
    }
    inline void makeroot(int x){access(x);splay(x);t[x].rev^=1;}
    inline void split(int x,int y){makeroot(x);access(y);splay(y);}
    inline void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;}
    inline void link(int x,int y){makeroot(x);t[x].ff=y;}
    inline int findroot(int x)
    {
        access(x);splay(x);
        while(t[x].ch[0]) push_down(x),x=t[x].ch[0];
        return x;
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("ce.in","r",stdin);
        #endif
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++) t[i].tmp=1e9;
        for(int i=1;i<=m;i++)
        {
            scanf("%s",cur);
            if(cur[0]=='f')
            {
                scanf("%d%d%d%d%d",&edge[i].id,&edge[i].u,&edge[i].v,&edge[i].a,&edge[i].b);
                edge[i].id++,edge[i].u++,edge[i].v++;
                int u=edge[i].u,v=edge[i].v;
                if(findroot(u)==findroot(v))
                {
                    split(u,v);
                    int cur=t[v].minn;
                    if(edge[i].a<=t[cur].tmp) continue;
                    cut(u,cur),cut(v,cur);
                }
                t[edge[i].id+n].tmp=edge[i].a; 
                t[edge[i].id+n].val=edge[i].b;
                link(u,edge[i].id+n),link(v,edge[i].id+n);
            }
            else if(cur[0]=='m')
            {
                int u,v;
                scanf("%d%d",&u,&v);
                u++,v++;
                if(findroot(u)!=findroot(v)) printf("-1
    ");
                else split(u,v),printf("%d
    ",t[v].sum);
            }
            else
            {
                int x,p;
                scanf("%d%d",&x,&p);
                x++;
                makeroot(x+n);
                t[x+n].val=p;
                push_up(x+n);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Python time
    Python List/Tutle/Set/Str/Dict
    python os
    Python 学习之九九乘法表
    Pycharm配置
    python 打包exe:
    Linux 安装PHP扩展过程
    tkinter在循环中创建按钮以传递命令参数,闭包的坑
    tkinter
    设置greenplum用户和密码访问:
  • 原文地址:https://www.cnblogs.com/fengxunling/p/10290020.html
Copyright © 2011-2022 走看看