[LCT 动态最大生成树] HDU 5398 GCD Tree

30 篇文章 0 订阅
19 篇文章 0 订阅

一个数肯定是和自己约数连更优

那么就是动态维护最大生成树

两个log


#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=300005;

struct Splay{  
  struct node{  
    int size,idx,rev;  
    int val,minimum;  
    node *p,*ch[2],*fat,*mpos;  
    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;  
      minimum=val; mpos=this;  
      if (ch[0]->minimum<minimum)  
	minimum=ch[0]->minimum,mpos=ch[0]->mpos;  
      if (ch[1]->minimum<minimum)  
	minimum=ch[1]->minimum,mpos=ch[1]->mpos;  
    }  
    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->minimum=(1<<30)+(1<<29); null->mpos=null; }  
  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; x=x->fat;  
    }  
    return y;  
  }  
  void Link(node *x,node *y){  
    if (Jud(x,y)) return;  
    Access(x)->reverse(); splay(x); x->fat=y; Access(x);  
  }  
  void Cut(node *x){  
    Access(x); splay(x); x->ch[0]->p=null; x->ch[0]=null; x->fat=null; x->update();  
  }  
  void Cut(node *x,node *y){  
    Access(x)->reverse(); Cut(y);  
  }  
  node *Root(node *x){  
    Access(x); splay(x);  
    node *y=x;  
    while (y->ch[0]!=null) y->pushdown(null),y=y->ch[0];  
    return y;  
  }  
  bool Jud(node *x,node *y){  
    return Root(x)==Root(y);  
  }  
  int Query(node *x,node *y){  
    Access(x)->reverse();  
    return Access(y)->minimum;  
  }  
  node *Road(node *x,node *y){  
    Access(x)->reverse();  
    return Access(y)->mpos;  
  }  
}LCT;  
  
Splay::node *pos[N];  
  
inline void Init(){  
  for (int i=1;i<=N-5;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=1<<30; pos[i]->minimum=1<<30; pos[i]->mpos=pos[i];  
    pos[i]->idx=i; pos[i]->size=1;  
  }  
}

const int MAXN=1e5;
vector<int> d[MAXN+5];
ll Ans[MAXN+5];
int n=MAXN,m;
int icnt;

int u[N],v[N];

stack<int> S;

inline void Memery(){
  for (int i=100000;i;i--) S.push(i);
}

inline int New(){
  int i=S.top(); S.pop(); i+=n;
  pos[i]->p=pos[i]->ch[0]=pos[i]->ch[1]=pos[i]->fat=LCT.null;   
  pos[i]->val=1<<30; pos[i]->minimum=1<<30; pos[i]->mpos=pos[i]; pos[i]->size=1;
  return i-n;
}

inline void Del(int x){
  S.push(x);
}

int main(){
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  Init(); Memery();
  for (int i=2;i<=n;i++) for (int j=i+i;j<=n;j+=i) d[j].push_back(i);
  ll ret=0;
  Ans[1]=ret;
  for (int i=2;i<=n;i++){
    icnt=New(); u[icnt]=1; v[icnt]=i;
    pos[n+icnt]->val=1; pos[n+icnt]->update();
    LCT.Link(pos[n+icnt],pos[i]);  
    LCT.Link(pos[n+icnt],pos[1]);
    ret+=1;
    for (int j=0;j<(signed)d[i].size();j++){
      int d=::d[i][j];  
      Splay::node *road=LCT.Road(pos[d],pos[i]);  
      if (road->val<d){
	icnt=New(); u[icnt]=d; v[icnt]=i;
	pos[n+icnt]->val=d; pos[n+icnt]->update();
	LCT.Cut(road,pos[u[road->idx-n]]);  
	LCT.Cut(road,pos[v[road->idx-n]]);  
	LCT.Link(pos[n+icnt],pos[i]);  
	LCT.Link(pos[n+icnt],pos[d]);
	ret+=d-road->val;
	Del(road->idx-n);
      }
    }
    Ans[i]=ret;
  }
  int _n;
  while (read(_n))
    printf("%lld\n",Ans[_n]);
  return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值