国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[BZOJ4530][Bjoi2014][LCT維護(hù)子樹(shù)信息]大融合

2019-11-08 18:40:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

LCT練習(xí)題

第一次用指針打LCT,有蠻多意外的,不過(guò)確實(shí)感覺(jué)指針比數(shù)組打起來(lái)爽多了

#include <cstdio>#include <iostream>#include <algorithm>#define N 100010using namespace std;int n,m,x,y,tp,f[N],tot[N],G[N];char op;struct lef{ lef *fa,*ch[2]; int sz,isz,rev;}PR[N],*A[N],*sta[N];struct edge{ int t,nx;}E[N];inline void reaD(int &x){ char Ch=getchar();x=0; for(;Ch>'9'||Ch<'0';Ch=getchar()); for(;Ch>='0'&&Ch<='9';x=x*10+Ch-'0',Ch=getchar());}int fifa(int x){return f[x]==x?x:f[x]=fifa(f[x]);}inline int isl(lef *x){return x->fa&&x->fa->ch[1]==x;}inline int isr(lef *x){return !x->fa||(x->fa->ch[0]!=x&&x->fa->ch[1]!=x);}inline void updat(lef *x){ if(!x) return; x->sz=x->isz+1; if(x->ch[0]) x->sz+=x->ch[0]->sz; if(x->ch[1]) x->sz+=x->ch[1]->sz;}inline void pushdn(lef *x){ if(!(x&&x->rev)) return; swap(x->ch[0],x->ch[1]); if(x->ch[0]) x->ch[0]->rev^=1; if(x->ch[1]) x->ch[1]->rev^=1; x->rev=0;}inline void rot(lef *x){ lef *y=x->fa,*z=y->fa; int lor=isl(x); if(!isr(y)) z->ch[z->ch[1]==y]=x; x->fa=z; if(y->ch[lor]=x->ch[lor^1]) y->ch[lor]->fa=y; (x->ch[lor^1]=y)->fa=x; updat(y); updat(x);}inline void splay(lef *x){ sta[tp=1]=x; for(lef *i=x;!isr(i);i=i->fa) sta[++tp]=i->fa; for(;tp;--tp) pushdn(sta[tp]); for(;!isr(x);rot(x)) if(!isr(x->fa)) rot(isl(x)^isl(x->fa)?x:x->fa);}inline void access(lef *x){ lef *t=0; for(;x;x=x->fa){ splay(x); if(x->ch[1]) x->isz+=x->ch[1]->sz; if(t) x->isz-=t->sz; x->ch[1]=t; t=x; updat(x); }}inline void reverse(lef *x){ access(x); splay(x); x->rev^=1;}inline void link(lef *x,lef *y){ reverse(y); reverse(x); x->fa=y; y->isz+=x->sz; updat(y); access(x);}int main(){ freopen("1.in","r",stdin); freopen("1.out","w",stdout); reaD(n);reaD(m); for(int i=1;i<=n;i++) A[i]=&pr[i],A[i]->sz=1,tot[i]=1,f[i]=i; for(int i=1;i<=m;i++){ while((op=getchar())!='A'&&op!='Q'); if(op=='A'){ reaD(x); reaD(y); tot[fifa(y)]+=tot[fifa(x)]; f[fifa(x)]=fifa(y); link(A[x],A[y]); } else{ reaD(x);reaD(y); reverse(A[x]); access(A[y]); splay(A[x]); printf("%d/n",(A[y]->sz)*(A[x]->sz-A[y]->sz)); } }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎右旗| 维西| 铜山县| 福清市| 荣成市| 潮州市| 平江县| 乌兰察布市| 洛阳市| 大邑县| 临沭县| 阿鲁科尔沁旗| 弋阳县| 原阳县| 易门县| 望谟县| 武义县| 乌鲁木齐县| 景泰县| 汉阴县| 凤台县| 桑植县| 南城县| 县级市| 高邮市| 察隅县| 鄂托克旗| 蒙阴县| 隆回县| 全南县| 明水县| 大洼县| 南涧| 连州市| 庆云县| 开封市| 韶关市| 重庆市| 田林县| 龙南县| 宜宾市|