[bzoj2959][动态树]长跑

11 篇文章 0 订阅

2959: 长跑

Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 807 Solved: 408
[Submit][Status][Discuss]
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 第二天

[Submit][Status][Discuss]


sol:

和4229有点像,突出表现了lct find_root函数有多慢。必须使用并查集维护连通性才能过。缩下点,多了个求链的权值和的操作。

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<map>
using namespace std;

inline int read()
{
    char c;
    bool pd=0;
    while((c=getchar())>'9'||c<'0')
    if(c=='-') pd=1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9')
    res=(res<<3)+(res<<1)+c-'0';
    return pd?-res:res;
}
const int N=200000;
int rev[N],lc[N],rc[N],fa[N],father[N];
int sum[N],val[N],v[N];
inline int find(int x)
{
    return x==father[x]?x:father[x]=find(father[x]);
}
inline void unit(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        val[fy]+=val[fx];
        father[fx]=fy;
    }
}
inline void updata(int x)
{
    sum[x]=sum[lc[x]]+sum[rc[x]]+val[x];
}
inline void tag_rev(int x)
{
    rev[x]=!rev[x];
    swap(lc[x],rc[x]);
}
inline void tag_down(int x)
{
    if(rev[x])
    {
        rev[x]=0;
        tag_rev(lc[x]);
        tag_rev(rc[x]);
    }
}
inline void rotate(int x)
{
    int y=fa[x],z=fa[y];
    int b=lc[y]==x?rc[x]:lc[x];
    if(b) fa[b]=y;
    fa[y]=x;fa[x]=z;
    if(z)
    {
        if(lc[z]==y) lc[z]=x;
        if(rc[z]==y) rc[z]=x;
    }
    if(lc[y]==x) lc[y]=b,rc[x]=y;
    else rc[y]=b,lc[x]=y;
    updata(y);
}
int sta[N];
inline bool is_root(int x)
{
    return lc[fa[x]]!=x&&rc[fa[x]]!=x;
}
inline void splay(int x)
{
    sta[sta[0]=1]=x;
    for(int y=x;!is_root(y);y=fa[y])
    {
        fa[y]=find(fa[y]);
        sta[++sta[0]]=fa[y];
    }
    while(sta[0]) tag_down(sta[sta[0]--]);
    while(!is_root(x))
    {
        fa[x]=find(fa[x]);
        if(!is_root(fa[x]))
        {
            fa[fa[x]]=find(fa[fa[x]]);
            if((lc[fa[fa[x]]]==fa[x])==(lc[fa[x]]==x)) rotate(fa[x]);
            else rotate(x);
        }
        rotate(x);
    }
    updata(x);
}
inline void access(int q)
{
    for(int p=0;q;p=q,q=fa[q])
    {
        splay(q);
        rc[q]=p;
        fa[q]=find(fa[q]);
        updata(q);
    }
}
inline void make_root(int x)
{
    access(x);
    splay(x);
    tag_rev(x);
}
inline void sc(int x,int y)
{
    if(!x) return;
    unit(x,y);
    if(lc[x]) sc(lc[x],y);
    if(rc[x]) sc(rc[x],y);
}
inline void link(int x,int y)
{
    make_root(x);
    fa[x]=y;
}
int fake[N];
inline int find_root(int x)
{
    return fake[x]==x?x:fake[x]=find_root(fake[x]);
}
inline void add(int x,int y)
{
    x=find(x);y=find(y);
    if(x==y) return;
    if(find_root(x)==find_root(y))
    {
        make_root(x);
        access(y);
        splay(x);
        sc(x,find(x));
        rc[x]=0;
        updata(x);
        return;
    }
    fake[find_root(y)]=find_root(x);
    link(x,y);
}
int n,m,q;
char sr[5];
struct cc
{
    int x,y,z;
}a[N],b[N];
int ans[N];
int main()
{
//  freopen("2959.in","r",stdin);
    n=read();
    m=read();
    for(int i=1;i<=n;++i) father[i]=fake[i]=i;
    for(int i=1;i<=n;++i) val[i]=v[i]=read();
    for(int i=1;i<=m;++i)
    {
        int tp,x,y;
        tp=read();
        x=read();
        y=read();
        if(tp==1) add(x,y);
        if(tp==2)
        {
            int z=find(x);
            splay(z);
            val[z]-=v[x];
            v[x]=y;
            val[z]+=v[x];
            updata(z);
        }
        if(tp==3)
        {
            x=find(x);
            y=find(y);
            if(find_root(x)!=find_root(y))
            {
                puts("-1");
                continue;
            }
            make_root(x);
            access(y);
            splay(x);
            printf("%d\n",sum[x]);
        }
    }
    for(int i=1;i<=q;++i)
    if(a[i].z) puts(ans[i]?"Yes":"No");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值