BZOJ 3083 遥远的国度 树链剖分+线段树
有换根的树链剖分的裸题.
在换根的时候注意讨论.
注意数据范围要开unsigned int或longlong
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctime> #include<string> #include<iomanip> #include<algorithm> #include<map> using namespace std; #define LL long long #define FILE "dealing" #define up(i,j,n) for(LL i=j;i<=n;++i) #define db double #define uint unsigned int #define eps 1e-12 #define pii pair<LL,LL> LL read(){ LL x=0,f=1,ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return f*x; } const LL maxn=800010,maxm=20000,limit=1e6,mod=(LL)(7+1e9+0.1); const db inf=(1e18); template<class T>bool cmax(T& a,T b){return a<b?a=b,true:false;} template<class T>bool cmin(T& a,T b){return a>b?a=b,true:false;} template<class T>T min(T& a,T& b){return a<b?a:b;} template<class T>T max(T& a,T& b){return a>b?a:b;} LL n,m,root; LL v[maxn],id[maxn],pre[maxn],low[maxn],dfs_clock=0,top[maxn],dep[maxn],fa[maxn][30]; struct node{ LL y,next; }e[maxn]; LL len,linkk[maxn]; void insert(LL x,LL y){ e[++len].y=y; e[len].next=linkk[x]; linkk[x]=len; } namespace Seg_tree{ LL Min[maxn],L,R,key,flag[maxn],f[maxn]; void updata(LL x){ Min[x]=min(Min[x<<1],Min[x<<1|1]); } void add(LL x,LL d){ Min[x]=d; f[x]=1; flag[x]=d; } void pushdown(LL x){ if(f[x]){ f[x]=0; add(x<<1,flag[x]); add(x<<1|1,flag[x]); flag[x]=0; } } void change(LL l,LL r,LL x){ if(l>R||r<L)return; if(l>=L&&r<=R){add(x,key);return;} LL mid=(l+r)>>1; pushdown(x); change(l,mid,x<<1); change(mid+1,r,x<<1|1); updata(x); } void Change(LL l,LL r,LL K){ L=l,R=r,key=K; change(1,n,1); } LL query(LL l,LL r,LL x){ if(l>R||r<L)return (LL)(1e12); if(l>=L&&r<=R)return Min[x]; LL mid=(l+r)>>1; pushdown(x); return min(query(l,mid,x<<1),query(mid+1,r,x<<1|1)); } LL Query(LL Lef,LL Rig){ L=Lef,R=Rig; return query(1,n,1); } void build(LL l,LL r,LL x){ if(l==r){ Min[x]=v[id[l]]; return; } LL mid=(l+r)>>1; build(l,mid,x<<1); build(mid+1,r,x<<1|1); updata(x); } } namespace shupou{ LL son[maxn],siz[maxn]; void dfs1(LL x){ siz[x]=1; for(LL i=linkk[x];i;i=e[i].next){ if(e[i].y==fa[x][0])continue; fa[e[i].y][0]=x; dep[e[i].y]=dep[x]+1; dfs1(e[i].y); siz[x]+=siz[e[i].y]; if(siz[e[i].y]>siz[son[x]])son[x]=e[i].y; } } void dfs2(LL x){ pre[x]=++dfs_clock; if(son[x]){ top[son[x]]=top[x]; dfs2(son[x]); } for(LL i=linkk[x];i;i=e[i].next){ if(e[i].y==fa[x][0]||e[i].y==son[x])continue; top[e[i].y]=e[i].y; dfs2(e[i].y); } low[x]=dfs_clock; } void solve(){ dfs1(1); top[1]=1; dfs2(1); } } namespace QUERY{ void change(LL x,LL y,LL key){//将x-y改成key while(true){ if(dep[x]>dep[y])swap(x,y); LL f1=top[x],f2=top[y]; if(f1==f2){ Seg_tree::Change(pre[x],pre[y],key); return; } if(dep[f1]>dep[f2])swap(x,y),swap(f1,f2); Seg_tree::Change(pre[f2],pre[y],key); y=fa[f2][0]; } } LL query(LL x){ if(x==root)return Seg_tree::Min[1]; if(pre[x]>=pre[root]&&pre[x]<=low[root])return Seg_tree::Query(pre[x],low[x]); if(dep[x]>=dep[root])return Seg_tree::Query(pre[x],low[x]); LL y=root; for(LL i=23;i>=0;i--)if(dep[y]-(dep[x]+1)>=(1<<i))y=fa[y][i]; if(fa[y][0]==x)return min(Seg_tree::Query(1,pre[y]-1),Seg_tree::Query(low[y]+1,n)); return Seg_tree::Query(pre[x],low[x]); } }; namespace sol{ void solve(){ n=read(),m=read(); up(i,2,n){ LL x=read(),y=read(); insert(x,y);insert(y,x); } up(i,1,n)v[i]=read(); root=read(); shupou::solve(); up(j,1,23)up(i,1,n)fa[i][j]=fa[fa[i][j-1]][j-1]; up(i,1,n)id[pre[i]]=i; Seg_tree::build(1,n,1); up(i,1,m){ LL ch=read(); if(ch==1)root=read(); if(ch==2){ LL x=read(),y=read(),v=read(); QUERY::change(x,y,v); } if(ch==3){ LL x=read(); LL d=0; printf("%lld\n",d=QUERY::query(x)); } } } } int main(){ freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); sol::solve(); return 0; }