bzoj4538[HNOI2016]网络
首先容易想到一个log^2的做法,就是这个答案是可以二分的,那么使用线段树套线段树,第一个线段树是权值线段树,第二个线段树记这些权值的路径在每个点的经过次数。那么询问就是在权值线段树上看右儿子的那个点是不是被右儿子的所有路径都覆盖到了,即那个点的经过次数等于右儿子的路径数,如果是那么就往左儿子走,否则往右儿子走。
显然这个空间复杂度不能接受,而且效率令人堪忧,于是用整体二分加bit即可,思路差不多。空间复杂度和常数都小很多,时间复杂度仍是\(O(nlog^2n)\)的,貌似在各大oj上都是前几名2333。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<ctime>
#define P puts("lala")
#define cp cerr<<"lala"<<endl
#define fi first
#define se second
#define ln putchar('\n')
#define pb push_back
#define shmem(x) cerr<<sizeof(x)/(1024*1024.0)<<"MB"<<endl
using namespace std;
inline int read()
{
char ch=getchar();int g=1,re=0;
while(ch<'0'||ch>'9'){if(ch=='-')g=-1; ch=getchar();}
while(ch<='9'&&ch>='0') re=(re<<1)+(re<<3)+(ch^48),ch=getchar();
return re*g;
}
typedef long long ll;
typedef pair<int,int> pii;
const int N=100050;
const int M=200050;
int head[N],cnt=0;
struct node
{
int to,next;
}e[N<<1];
inline void add(int x,int y)
{
e[++cnt]=(node){y,head[x]};head[x]=cnt;
e[++cnt]=(node){x,head[y]};head[y]=cnt;
}
int dfn[N],efn[N],clk=0,f[N][23],dep[N];
void dfs(int u,int fa,int d)
{
dfn[u]=++clk; f[u][0]=fa; dep[u]=d;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v==fa) continue;
dfs(v,u,d+1);
}
efn[u]=clk;
}
int lca(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
int d=dep[x]-dep[y];
for(int i=17;i>=0;--i) if(d&1<<i) x=f[x][i];
if(x==y) return x;
for(int i=17;i>=0;--i) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
struct qwe
{
int t,x,y,z,v,k;
qwe(int t=0,int x=0,int y=0,int z=0,int v=0,int k=0):t(t),x(x),y(y),z(z),v(v),k(k){ }
};
qwe upd[M],ask[M],A[M],B[M];
bool cmp1(qwe a,qwe b) {return a.t<b.t;}
int tt2[M],n,m,un,qn,Ans[M],bucket[M],tot=0;
namespace bit
{
int s[N];
inline void add(int x,int k)
{
if(!x) return ;
for(int i=x;i<=n;i+=(i&-i)) s[i]+=k;
}
inline int ask(int l,int r)
{
int ans=0;
for(int i=r;i;i-=(i&-i)) ans+=s[i];
for(int i=l-1;i;i-=(i&-i)) ans-=s[i];
return ans;
}
}
void solve(int l,int r,int ul,int ur,int ql,int qr)
{
if(l==r)
{
int p1=ul,now=0;
for(int i=ql;i<=qr;++i)
{
while(p1<=ur&&upd[p1].t<ask[i].t)
{
int x=upd[p1].x,y=upd[p1].y,z=upd[p1].z,k=upd[p1].k;
bit::add(dfn[x],k); bit::add(dfn[y],k);
bit::add(dfn[z],-k); bit::add(dfn[f[z][0]],-k);
now+=k;
p1++;
}
if(now-bit::ask(dfn[ask[i].x],efn[ask[i].x]))
Ans[ask[i].k]=bucket[l];
else Ans[ask[i].k]=-1;
}
for(int i=ul;i<p1;++i)
{
int x=upd[i].x,y=upd[i].y,z=upd[i].z,k=upd[i].k;
bit::add(dfn[x],-k); bit::add(dfn[y],-k);
bit::add(dfn[z],k); bit::add(dfn[f[z][0]],k);
}
return ;
}
int mid=l+r>>1,n1=0,n2=0;
for(int i=ul;i<=ur;++i)
if(upd[i].v<=mid) A[++n1]=upd[i];
else B[++n2]=upd[i];
int umid=ul+n1-1;
for(int i=ul;i<=umid;++i) upd[i]=A[i-ul+1];
for(int i=umid+1;i<=ur;++i) upd[i]=B[i-umid];
n1=0; n2=0;
int p1=umid+1,now=0;
for(int i=ql;i<=qr;++i)
{
while(p1<=ur&&upd[p1].t<ask[i].t)
{
int x=upd[p1].x,y=upd[p1].y,z=upd[p1].z,k=upd[p1].k;
bit::add(dfn[x],k); bit::add(dfn[y],k);
bit::add(dfn[z],-k); bit::add(dfn[f[z][0]],-k);
now+=k;
p1++;
}
if(now-bit::ask(dfn[ask[i].x],efn[ask[i].x]))
B[++n2]=ask[i];
else A[++n1]=ask[i];
//if(ask[i].t==10) cerr<<l<<' '<<r<<' '
//<<p1-umid-1-bit::ask(dfn[ask[i].x],efn[ask[i].x])<<endl;
}
for(int i=umid+1;i<p1;++i)
{
int x=upd[i].x,y=upd[i].y,z=upd[i].z,k=upd[i].k;
bit::add(dfn[x],-k); bit::add(dfn[y],-k);
bit::add(dfn[z],k); bit::add(dfn[f[z][0]],k);
}
int qmid=ql+n1-1;
for(int i=ql;i<=qmid;++i) ask[i]=A[i-ql+1];
for(int i=qmid+1;i<=qr;++i) ask[i]=B[i-qmid];
solve(l,mid,ul,umid,ql,qmid); solve(mid+1,r,umid+1,ur,qmid+1,qr);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);freopen("1.out","w",stdout);
#endif
n=read(); m=read();
for(int i=1;i<n;++i)
{
int x=read(),y=read();
add(x,y);
}
dfs(1,0,0);
for(int j=1;j<=17;++j) for(int i=1;i<=n;++i) f[i][j]=f[f[i][j-1]][j-1];
for(int i=1;i<=m;++i)
{
int typ=read();
if(typ==0)
{
++un;
int x=read(),y=read(),v=read();
int z=lca(x,y);
tt2[i]=un;
upd[un]=qwe(i,x,y,z,v,1);
bucket[++tot]=v;
}
else if(typ==1)
{
int w=tt2[read()];
++un;
upd[un]=upd[w];
upd[un].t=i; upd[un].k=-1;
}
else
{
++qn;
int x=read();
ask[qn]=qwe(i,x,0,0,0,qn);
}
}
sort(bucket+1,bucket+1+tot);
tot=unique(bucket+1,bucket+1+tot)-(bucket+1);
for(int i=1;i<=un;++i) upd[i].v=lower_bound(bucket+1,bucket+1+tot,upd[i].v)-bucket;
//have sorted by time
solve(1,tot,1,un,1,qn);
for(int i=1;i<=qn;++i) printf("%d\n",Ans[i]);
return 0;
}