标签:ble new dep 可见 利用 strong typedef 距离 rebuild
#include<iostream> #include<cstdio> #include<vector> #include<cstdlib> #define ls tr[p].l #define rs tr[p].r #define N 100002 using namespace std; typedef long long ll; int topp,q[N*32],cnt,head[N],tot,p[N][20],dep[N],size[N],siz[N],dp[N],sum,root,rt[N],n,st[N],top,T[N]; ll deep[N],dis[N],r[N],ans; bool vis[N],tag[N]; vector<int>vec[N],E[N]; struct edge{int n,to;ll l;}e[N<<1]; inline void add(int u,int v,ll l){e[++tot].n=head[u];e[tot].to=v;head[u]=tot;e[tot].l=l;} const double pha=0.75; const int mod=1e9; struct node{int size,cnt,l,r,rd;ll val;}tr[N*32]; inline void pushup(int p){tr[p].size=tr[ls].size+tr[rs].size+tr[p].cnt;} inline int newnode(ll x){ int y=topp?q[topp--]:++cnt; tr[y].l=tr[y].r=0;tr[y].val=x;tr[y].rd=rand();tr[y].size=tr[y].cnt=1; return y; } inline void re(int x){q[++topp]=x;} inline void lturn(int &p){ int x=tr[p].r;tr[p].r=tr[x].l;tr[x].l=p; tr[x].size=tr[p].size;pushup(p);p=x; } inline void rturn(int &p){ int x=tr[p].l;tr[p].l=tr[x].r;tr[x].r=p; tr[x].size=tr[p].size;pushup(p);p=x; } inline void ins(int &p,ll x){ if(!p){p=newnode(x);return;} tr[p].size++; if(tr[p].val==x){tr[p].cnt++;return;} if(x>tr[p].val){ ins(rs,x); if(tr[p].rd>tr[rs].rd)lturn(p); } else{ ins(ls,x); if(tr[p].rd>tr[ls].rd)rturn(p); } } inline ll get_rank(int p,ll x){ if(!p)return 0; if(tr[p].val<=x)return tr[ls].size+tr[p].cnt+get_rank(rs,x); return get_rank(ls,x); } void bl(int p){if(ls)bl(ls);printf("%lld ",tr[p].val);if(rs)bl(rs);} inline ll rd(){ int x=0;char c=getchar();bool f=0; while(!isdigit(c)){if(c==‘-‘)f=1;c=getchar();} while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();} return f?-x:x; } inline int getlca(int a,int b){ if(dep[a]<dep[b])swap(a,b); for(int i=18;i>=0;--i)if(dep[a]-(1<<i)>=dep[b])a=p[a][i]; if(a==b)return a; for(int i=18;i>=0;--i)if(p[a][i]!=p[b][i])a=p[a][i],b=p[b][i]; return p[a][0]; } inline ll dist(int a,int b){return dis[a]+dis[b]-2*dis[getlca(a,b)];} void getsize(int u,int fa){ siz[u]=1; for(int i=head[u];i;i=e[i].n)if(e[i].to!=fa&&vis[e[i].to]&&!tag[e[i].to]){ int v=e[i].to;getsize(v,u);siz[u]+=siz[v]; } } void getroot(int u,int fa){ dp[u]=0;siz[u]=1; for(int i=head[u];i;i=e[i].n)if(vis[e[i].to]&&e[i].to!=fa&&!tag[e[i].to]){ int v=e[i].to;getroot(v,u); siz[u]+=siz[v];dp[u]=max(dp[u],siz[v]); } dp[u]=max(sum-siz[u],dp[u]); if(dp[u]<dp[root])root=u; } void getdeep(int u,int fa,int top,int tt){ ins(rt[top],deep[u]-r[u]);E[top].push_back(u); ins(T[tt],deep[u]-r[u]);vec[u].push_back(top); size[top]++; for(int i=head[u];i;i=e[i].n)if(e[i].to!=fa&&vis[e[i].to]&&!tag[e[i].to]){ int v=e[i].to;deep[v]=deep[u]+e[i].l; getdeep(v,u,top,tt); } } void solve(int u){ tag[u]=1; size[u]=1;E[u].push_back(u); ins(rt[u],-r[u]); for(int i=head[u];i;i=e[i].n)if(vis[e[i].to]&&!tag[e[i].to]){ int v=e[i].to; sum=siz[v];root=n+1; getroot(v,u);getsize(root,0); deep[v]=e[i].l;getdeep(v,u,u,root); solve(root); } } void efs(int p){if(!p)return;if(ls)efs(ls);if(rs)efs(rs);re(p);} inline void rebuild(int u){ int ma=E[u].size(); for(int i=0;i<ma;++i){ int v=E[u][i]; st[++top]=v;vis[v]=1; } ma=vec[u].size(); int king=ma?vec[u].back():0; for(int i=1;i<=top;++i){ int x=st[i];size[x]=0; E[x].clear(); while(vec[x].size()&&vec[x].back()!=king)vec[x].pop_back(); efs(rt[x]);efs(T[x]);rt[x]=T[x]=0; } root=n+1;sum=top;dp[root]=n+1; getroot(u,0);getsize(root,0);int haha=root; solve(root); if(king){ for(int i=1;i<=top;++i){ int x=st[i]; ll d=dist(x,king); ins(T[haha],d-r[x]); } } while(top){ int x=st[top]; vis[st[top]]=0; tag[st[top]]=0; top--; } } inline void check(int u){ int ma=vec[u].size(); for(int i=1;i<ma;++i){ int v=vec[u][i],v1=vec[u][i-1]; if(size[v1]*pha<size[v]){ rebuild(v1); break; } } } inline void work(int u,int fa){ int ma=vec[fa].size(); for(int i=0;i<ma;++i)vec[u].push_back(vec[fa][i]); vec[u].push_back(fa); ins(rt[u],-r[u]);size[u]=1;E[u].push_back(u); ma++; for(int i=ma-1;i>=0;--i){ int v=vec[u][i];ll d=dist(u,v); size[v]++;E[v].push_back(u); int nex=i==ma-1?u:vec[u][i+1]; ins(rt[v],d-r[u]);ins(T[nex],d-r[u]); ans+=get_rank(rt[v],r[u]-d)-get_rank(T[nex],r[u]-d); } check(u); } int main(){ srand(3783252); n=rd();n=rd(); ll a=rd(),c=rd();r[1]=rd(); size[1]=1;ins(rt[1],-r[1]); E[1].push_back(1); printf("%lld\n",ans); for(int i=2;i<=n;++i){ a=rd();c=rd();r[i]=rd(); a=a^(ans%mod); add(i,a,c);add(a,i,c); dep[i]=dep[a]+1;dis[i]=dis[a]+c;p[i][0]=a; for(int j=1;(1<<j)<=dep[i];++j)p[i][j]=p[p[i][j-1]][j-1]; work(i,a); printf("%lld\n",ans); } return 0; }
标签:ble new dep 可见 利用 strong typedef 距离 rebuild
原文地址:https://www.cnblogs.com/ZH-comld/p/10050759.html