【LCT+并查集】BZOJ2959[长跑]题解

9 篇文章 0 订阅
8 篇文章 0 订阅

题目概述

CHNJZ可以在 n 个地方虐场,每次虐场可以踩若干个人。一个地方的人被踩后就不能再踩了(心态已爆炸)。

m 个事件:1.地点 x 到地点 y 新建了一条边。2.地点 x 能踩的人变成了 y 。3.询问从 x y 最多能踩多少人。

解题报告

显然是动态树问题,可以用LCT解决。由于图中会出现边双,不能重复计算,所以需要将边双缩点。

考虑合并 x y ,我们先取出 x y 的路径,然后将路径上所有权值都加到 y 上。接下来唯一的问题就是虚边的 Path Father 发生了大量变化,很容易想到用并查集来解决这个问题。

示例程序

namespace:>9000ms,差点TLE。struct:6800ms。

为什么namespace这么慢啊QAQ!

#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=150000;

int n,m,w[maxn+5],val[maxn+5];LL sum[maxn+5];
int son[maxn+5][2],fa[maxn+5];bool flip[maxn+5];
struct Dsu{
    int fa[maxn+5];
    void Clear() {for (int i=1;i<=n;i++) fa[i]=i;}
    int getfa(int x) {if (fa[x]==x) return x;return fa[x]=getfa(fa[x]);}
}A,B;

#define Eoln(x) ((x)==10||(x)==13||(x)==EOF)
inline char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF;return *l++;
}
inline int readi(int &x){
    int tot=0,f=1;char ch=readc(),lst='+';
    while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}
    if (lst=='-') f=-f;
    while (isdigit(ch)) tot=(tot<<1)+(tot<<3)+(ch^48),ch=readc();
    return x=tot*f,Eoln(ch);
}
#define fa(p) (B.getfa(fa[p]))
#define is_ro(p) ((p)!=son[fa(p)][0]&&(p)!=son[fa(p)][1])
#define Son(p) ((p)==son[fa(p)][1])
inline void Pushup(int p) {sum[p]=sum[son[p][0]]+val[p]+sum[son[p][1]];}
inline void Rotate(int t){
    int p=fa(t),d=Son(t);son[p][d]=son[t][d^1];son[t][d^1]=p;
    Pushup(p);Pushup(t);if (!is_ro(p)) son[fa(p)][Son(p)]=t;
    if (son[p][d]) fa[son[p][d]]=p;fa[t]=fa(p);fa[p]=t;
}
inline void Addflip(int p) {swap(son[p][0],son[p][1]);flip[p]^=1;}
inline void Pushdown(int p) {if (flip[p]) flip[p]^=1,Addflip(son[p][0]),Addflip(son[p][1]);}
inline void Splay(int p){
    static int top,stk[maxn+5];stk[top=1]=p;
    for (int i=p;!is_ro(i);i=fa(i)) stk[++top]=fa(i);
    while (top) Pushdown(stk[top--]);
    for (int pre=fa(p);!is_ro(p);Rotate(p),pre=fa(p))
        if (!is_ro(pre)) Rotate(Son(p)==Son(pre)?pre:p);
}
inline void Access(int p) {for (int lst=0;p;lst=p,p=fa(p)) Splay(p),son[p][1]=lst,Pushup(p);}
inline void Makero(int x) {Access(x);Splay(x);Addflip(x);}
void Dfs(int x,int y){
    if (son[x][0]) val[y]+=val[son[x][0]],Dfs(son[x][0],y);
    if (son[x][1]) val[y]+=val[son[x][1]],Dfs(son[x][1],y);
    B.fa[x]=y;son[x][0]=son[x][1]=0;
}
inline void Link(int x,int y){
    int fx=A.getfa(x),fy=A.getfa(y);
        if (fx!=fy) {Makero(x);fa[x]=y;A.fa[fx]=fy;return;}
    Makero(x);Access(y);Splay(y);Dfs(y,y);Pushup(y);
}
inline LL Sum(int x,int y){
    if (A.getfa(x)!=A.getfa(y)) return -1;
    Makero(x);Access(y);Splay(y);return sum[y];
}
int main(){
    freopen("program.in","r",stdin);
    freopen("program.out","w",stdout);
    readi(n);readi(m);A.Clear();B.Clear();
    for (int i=1;i<=n;i++) readi(w[i]),val[i]=w[i],Pushup(i);
    for (int td,x,y;m;m--){
        readi(td);readi(x);readi(y);int fx=B.getfa(x);
            if (td==1) x=fx,y=B.getfa(y),Link(x,y);
        else if (td==2) Makero(fx),val[fx]+=y-w[x],w[x]=y,Pushup(fx);
        else x=fx,y=B.getfa(y),printf("%lld\n",Sum(x,y));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值