[LCT 动态最大生成树] HDU 5398 GCD Tree
来源:互联网 发布:gson解析json为list 编辑:程序博客网 时间:2024/04/23 15:37
一个数肯定是和自己约数连更优
那么就是动态维护最大生成树
两个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
- [LCT 动态最大生成树] HDU 5398 GCD Tree
- hdu 5398 GCD Tree 2015多校联合训练赛#9 LCT,动态生成树
- 【LCT维护最大生成树】[HDU5389]GCD Tree
- hdu 5398:GCD Tree lct
- HDU 5398 GCD TREE LCT维护贪心
- LCT(GCD Tree,HDU 5398)
- [GCD最小生成树 LCT] HDU5398 .GCD TREE
- Hdu 4718 The LCIS on the Tree 动态树 LCT
- LCT 动态树 hdu 5002
- HDU 5398 GCD Tree
- hdu 5398 GCD Tree
- poj 3237 Tree 树链剖分 动态树 LCT
- bzoj 3282 Tree 动态树LCT
- hdu5398(lct维护最大生成树)
- 【动态树】 HDOJ 5398 GCD Tree
- hdu 5002 Tree (LCT)
- hdu 5002 Tree (LCT)
- 【HDU】4010 Query on The Trees 动态树之Link Cut Tree(LCT)
- kaggle初体验
- python sdk 发一个新浪微博
- keras学习笔记(1)CNN网络
- Eclipse常用快捷键(不定时更新)
- NetworkX使用手册
- [LCT 动态最大生成树] HDU 5398 GCD Tree
- 欧拉计划 1
- springMVC源码分析--SimpleUrlHandlerMapping(四)
- static的使用
- 用bs爬取安居客优秀经理人信息
- Codeforces Round #387 (Div. 2)C. Servers(模拟)
- MAVEN项目标准目录结构
- Android_AS多渠道打包和代码混淆教程
- ztree操作同一level下的结点