[LCT 边双连通分量缩点] BZOJ 2959 长跑

19 篇文章 0 订阅
7 篇文章 0 订阅

一个边双连通分量肯定可以定向成一个环 那么就缩点

每次连接 要是本来不连通 就连起来

连通的话就把树上路径全缩起来

询问就是树上路径的值


#include<cstdio>    
#include<cstdlib>    
#include<cstring>    
#include<vector>  
#include<stack>  
#include<algorithm>    
using namespace std;    
typedef pair<int,int> abcd;  
typedef long long ll;  
    
inline char nc(){    
  static char buf[100000],*p1=buf,*p2=buf;    
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }    
  return *p1++;    
}    
    
inline bool read(int &x){    
  char c=nc(),b=1;    
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; else if (c==EOF) return 0;   
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;  return 1;  
}  

const int N=900005;  

int fat[N];
inline void init(int n){
  for (int i=1;i<=n;i++) fat[i]=i;
}
inline int Fat(int u){
  return fat[u]==u?u:fat[u]=Fat(fat[u]);
}


struct Splay{    
  struct node{    
    int size,idx,rev;
    ll sum,val;
    node *p,*ch[2],*fat;    
    void setc(node *c,int d) { ch[d]=c; c->p=this; }    
    bool dir() { return p->ch[1]==this; }    
    void update() {    
      size=ch[0]->size+ch[1]->size+1;
      sum=val+ch[0]->sum+ch[1]->sum;
    }    
    void reverse() { rev^=1; swap(ch[0],ch[1]); }    
    void pushdown(node *null){    
      if (rev){ if (ch[0]!=null) ch[0]->reverse(); if (ch[1]!=null) ch[1]->reverse(); rev=0; }    
    }    
  }*null,Mem[N];    
  Splay() { null=Mem; null->p=null->ch[0]=null->ch[1]=null->fat=null; null->size=0; null->sum=0; }    
  void rot(node *x){    
    if (x==null || x->p==null) return ;    
    bool d=x->dir(); node *p=x->p;    
    if (p->p!=null) p->p->setc(x,p->dir()); else x->p=null;    
    p->setc(x->ch[d^1],d); x->setc(p,d^1); p->update(); x->update(); swap(x->fat,p->fat);    
  }    
  node *sta[N];    
  void splay(node *x){    
    int pnt=0; node *y=x;    
    while (y!=null) sta[++pnt]=y,y=y->p;    
    for (int i=pnt;i;i--) sta[i]->pushdown(null);    
    while (x->p!=null)    
      if (x->p->p==null)    
    rot(x);    
      else    
    x->dir()==x->p->dir()?(rot(x->p),rot(x)):(rot(x),rot(x));    
  }    
  node *Access(node *x){    
    node *y=null;    
    while (x!=null){    
      splay(x);    
      x->ch[1]->p=null; x->ch[1]->fat=x;    
      x->setc(y,1); y->fat=null;    
      x->update();    
      y=x;
      if (x->fat==null) break;
      x=Mem+Fat(x->fat->idx);    
    }    
    return y;    
  }
  node *Modify(node *x,node *np){    
    node *y=null;    
    while (x!=null){    
      splay(x);    
      x->ch[1]->p=null; x->ch[1]->fat=np;    
      x->setc(y,1); y->fat=null;    
      x->update();  
      y=x;
      if (x->fat==null) break;
      x=Mem+Fat(x->fat->idx);   
    }    
    return y;    
  }
  void Link(node *x,node *y){
    Access(x)->reverse(); splay(x); x->fat=y; Access(x);    
  }
  ll Query(node *x,node *y){    
    Access(x)->reverse();    
    return Access(y)->sum;    
  }    
}LCT;

namespace Tset{
  int fat[N];
  inline void init(int n){
    for (int i=1;i<=n;i++) fat[i]=i;
  }
  inline int Fat(int u){
    return fat[u]==u?u:fat[u]=Fat(fat[u]);
  }
  inline int Merge(int x,int y){
    x=Fat(x); y=Fat(y); if (x==y) return 0;
    fat[x]=y; return 1;
  }
}

int n,val[N];

int ncnt;
Splay::node *pos[N];
    
inline void Init(){    
  for (int i=1;i<=n;i++){    
    pos[i]=LCT.Mem+i;     
    pos[i]->p=pos[i]->ch[0]=pos[i]->ch[1]=pos[i]->fat=LCT.null;     
    pos[i]->val=pos[i]->sum=val[i]; pos[i]->idx=i; pos[i]->size=1;    
  }    
}

inline void Road(Splay::node *x){
  fat[x->idx]=ncnt;
  if (x->ch[0]!=LCT.null) Road(x->ch[0]);
  if (x->ch[1]!=LCT.null) Road(x->ch[1]);
}

int main(){
  int Q; int order,x,y;
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  read(n); read(Q);
  for (int i=1;i<=n;i++) read(val[i]);
  Init(); init(n); Tset::init(n); ncnt=n;
  while (Q--){
    read(order); read(x); read(y);
    if (order==1){
      if (Tset::Merge(x,y)){
	x=Fat(x); y=Fat(y);
	LCT.Link(pos[x],pos[y]);
      }else{
	x=Fat(x); y=Fat(y);
	if (x==y) continue;
	++ncnt; fat[ncnt]=ncnt;
	pos[ncnt]=LCT.Mem+ncnt; pos[ncnt]->p=pos[ncnt]->ch[0]=pos[ncnt]->ch[1]=pos[ncnt]->fat=LCT.null;
	pos[ncnt]->idx=ncnt; pos[ncnt]->size=1; 
	pos[ncnt]->val=pos[ncnt]->sum=LCT.Query(pos[x],pos[y]);
	LCT.Access(pos[x])->reverse();
	Road(LCT.Modify(pos[y],pos[ncnt]));
      }
    }else if (order==2){
      int f=Fat(x);
      LCT.Access(pos[f]);
      LCT.splay(pos[f]);
      pos[f]->val+=y-val[x]; val[x]=y; pos[f]->update();
    }else{
      if (Tset::Fat(x)!=Tset::Fat(y))
	printf("-1\n");
      else{
	x=Fat(x); y=Fat(y);
	printf("%lld\n",LCT.Query(pos[x],pos[y]));
      }
    }
  }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值