[BZOJ2959][清橙1379]长跑-LCT-并查集

长跑

Description

  某校开展了同学们喜闻乐见的阳光长跑活动。为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动。一时间操场上熙熙攘攘,摩肩接踵,盛况空前。
  为了让同学们更好地监督自己,学校推行了刷卡机制。
  学校中有n个地点,用1到n的整数表示,每个地点设有若干个刷卡机。
  有以下三类事件:
  1、修建了一条连接A地点和B地点的跑道。
  2、A点的刷卡机台数变为了B。
  3、进行了一次长跑。问一个同学从A出发,最后到达B最多可以刷卡多少次。具体的要求如下:
  当同学到达一个地点时,他可以在这里的每一台刷卡机上都刷卡。但每台刷卡机只能刷卡一次,即使多次到达同一地点也不能多次刷卡。
  为了安全起见,每条跑道都需要设定一个方向,这条跑道只能按照这个方向单向通行。最多的刷卡次数即为在任意设定跑道方向,按照任意路径从A地点到B地点能刷卡的最多次数。

Input

  输入的第一行包含两个正整数n,m,表示地点的个数和操作的个数。
  第二行包含n个非负整数,其中第i个数为第个地点最开始刷卡机的台数。
  接下来有m行,每行包含三个非负整数P,A,B,P为事件类型,A,B为事件的两个参数。
  最初所有地点之间都没有跑道。
  每行相邻的两个数之间均用一个空格隔开。表示地点编号的数均在1到n之间,每个地点的刷卡机台数始终不超过10000,P=1,2,3。

Output

  输出的行数等于第3类事件的个数,每行表示一个第3类事件。如果该情况下存在一种设定跑道方向的方案和路径的方案,可以到达,则输出最多可以刷卡的次数。如果A不能到达B,则输出-1。

Sample Input

9 31
10 20 30 40 50 60 70 80 90
3 1 2
1 1 3
1 1 2
1 8 9
1 2 4
1 2 5
1 4 6
1 4 7
3 1 8
3 8 8
1 8 9
3 8 8
3 7 5
3 7 3
1 4 1
3 7 5
3 7 3
1 5 7
3 6 5
3 3 6
1 2 4
1 5 5
3 3 6
2 8 180
3 8 8
2 9 190
3 9 9
2 5 150
3 3 6
2 1 210
3 3 6

Sample Output

-1
-1
80
170
180
170
190
170
250
280
280
270
370
380
580

HINT

  对于100%的数据,m<=5n,任意时刻,每个地点的刷卡机台数不超过10000。N<=1.5×105

Source

中国国家队清华集训 2012-2013 第二天


为了能在清橙上不TLE,怒学fread……
清橙这什么破评测机啊啊啊


思路:

想到类似图论题的通常套路:缩点,变成树上问题。

那么考虑维护一棵生成树。
可以发现,每次如果连出了一个环,那么这个环上的点只要有一个被访问了,那么其余环上的点均一定会在最优方案中被访问。
那么为什么不试试缩环呢???

因为是链上信息,考虑使用LCT维护这棵生成树。
每次连出一个环就新建一个节点代表这个环,权值设为这个环上所有点的权值和,并将这个环原来的虚边实边等全部连向这个点。

然而作为一颗普通的LCT,你是没法知道一个点有哪些虚儿子这种事的~
那么再维护一个并查集记录当前点所在的环编号,每次访问父亲时不直接访问,而是findfa一次。
这样就能解决问题了~

然而这应该不是正解……
毕竟清橙上咱是卡过的……

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=3e5+9;

inline ll read()
{
    ll x=0;char ch=getchar();
    while(ch<'0' || '9'<ch)ch=getchar();
    while('0'<=ch && ch<='9')x=x*10+(ch^48),ch=getchar();
    return x;
}

inline void writes(ll x){if(x>=10)writes(x/10);putchar(x%10+'0');}
inline void write(ll x){if(x<0)x=-x,putchar('-');writes(x);putchar('\n');}

int n,m;

namespace LCT
{
    int ch[N][2],fa[N],stk[N],fat[N],top,tot;
    ll val[N],sum[N];
    bool rev[N];
    inline void init()
    {
        tot=n;
        for(int i=1;i<=n;i++)
            fat[i]=i;
    }
    inline int findfa(int a)
    {
        if(fat[a]==a)return a;
        return fat[a]=findfa(fat[a]);
    }
    inline bool isroot(int x)
    {
        return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
    }
    inline void update(int x)
    {
        sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];
    }
    inline void push(int x)
    {
        if(rev[x])
        {
            rev[ch[x][0]]^=1;
            rev[ch[x][1]]^=1;
            swap(ch[x][0],ch[x][1]);
            rev[x]^=1;
        }
    }
    inline void rotate(int x)
    {
        int y=fa[x],z=fa[y];
        int l=(ch[y][1]==x);
        if(!isroot(y))ch[z][ch[z][1]==y]=x;
        fa[x]=z;fa[y]=x;fa[ch[x][l^1]]=y;
        ch[y][l]=ch[x][l^1];ch[x][l^1]=y;
        update(y);update(x);
    }
    inline void splay(int x)
    {
        stk[top=1]=x;
        for(int i=x;!isroot(i);i=fa[i])
            stk[++top]=fa[i];
        while(top)push(stk[top--]);
        while(!isroot(x))
        {
            int y=fa[x],z=fa[y];
            if(!isroot(y))
            {
                if(ch[z][1]==y^ch[y][1]==x)rotate(y);
                else rotate(x);
            }
            rotate(x);
        }
        update(x);
    }
    inline void access(int x)
    {
        for(int t=0;x;t=x,x=findfa(fa[x]))
        {
            splay(x);
            ch[x][1]=t;
            if(t)fa[t]=x;
            update(x);
        }
    }
    inline void makeroot(int x)
    {
        access(x);splay(x);
        rev[x]^=1;
    }
    inline void link(int x,int y)
    {
        makeroot(x);fa[x]=y;
    }
    inline bool connect(int x,int y)
    {
        access(x);access(y);
        while(fa[x])x=fa[x];
        while(fa[y])y=fa[y];
        return x==y;
    }
    inline ll query(int x,int y)
    {
        x=findfa(x),y=findfa(y);
        if(x==y)return val[x];
        if(!connect(x,y))return -1;
        makeroot(x);access(y);splay(y);
        return sum[y];
    }
    inline void dfs(int u)
    {
        fat[u]=tot;
        if(ch[u][0])dfs(ch[u][0]);
        if(ch[u][1])dfs(ch[u][1]);
    }
    inline void merge(int x,int y)
    {
        makeroot(x);access(y);splay(y);
        ++tot;fat[tot]=tot;
        val[tot]=sum[tot]=sum[y];
        dfs(y);
    }
}

namespace istreams
{
    #define L (1<<16)
    char buffer[N],*S,*T;
    char getchars()
    {
        if(S==T)
        {
            T=(S=buffer)+fread(buffer,1,L,stdin);
            if(S==T)return EOF;
        }
        return *S++;
    }
    int reads()
    {
        int x=0;char ch=getchars();
        while(ch<'0'|| ch>'9')ch=getchars();
        while('0'<=ch && ch<='9')x=x*10+(ch^48),ch=getchars();
        return x;
    }
}

using namespace LCT;
using namespace istreams;

int main()
{
    n=reads();m=reads();
    init();
    for(int i=1;i<=n;i++)
        val[i]=reads();
    while(m--)
    {
        int p=reads(),a=reads(),b=reads();
        if(p==1)
        {
            a=findfa(a),b=findfa(b);
            if(a!=b)
            {
                if(!connect(a,b))
                    link(a,b);
                else
                    merge(a,b);
            }
        }
        else if(p==2)
        {
            ll dlt=b-val[a];
            int faa=findfa(a);
            access(faa);splay(faa);
            val[faa]+=dlt;
            sum[faa]+=dlt;
            val[a]=b;
        }
        else
            write(query(a,b));
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值