【LOJ #6073】「2017 山东一轮集训 Day5」距离(主席树 / 树链剖分)

传送门

首先若p[i]=ip[i]=i时且离线时可以直接用LNOI2014]Lca的做法

在线的话可以用主席树对每个点维护到根上的所有pp到根的路径+1+1修改后的dfsdfs
然后差分一下答案即可

如果标记永久化时空常数都会小很多

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline ll readll(){
    char ch=gc();
    ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=200005;
char xxx;
vector<pii> e[N];
ll dis[N],dep[N],s[N];
int in[N],out[N],siz[N],son[N],fa[N],top[N],del[N],idx[N],dfn;
int n,q,p[N],rt[N];ll last;
namespace Seg{
	cs int N=::N*100;
	int lc[N],rc[N],tag[N],tot;
	ll s[N],coef[N];
	#define mid ((l+r)>>1)
	inline int copy(int r1){
		int u=++tot;
		lc[u]=lc[r1],rc[u]=rc[r1],s[u]=s[r1],coef[u]=coef[r1],tag[u]=tag[r1];
		return u;
	}
	void build(int &u,int l,int r){
		u=++tot;
		if(l==r){coef[u]=del[idx[l]];return;}
		build(lc[u],l,mid),build(rc[u],mid+1,r);
		coef[u]=coef[lc[u]]+coef[rc[u]];
	}
	inline void pushnow(int u,int k){
		s[u]+=coef[u]*k,tag[u]+=k;
	}
	inline void pushup(int u){
		s[u]=s[lc[u]]+s[rc[u]]+coef[u]*tag[u];
	}
	void update(int &u,int l,int r,int st,int des,int k){
		u=copy(u);
		if(st<=l&&r<=des){return pushnow(u,k);}
		if(st<=mid)update(lc[u],l,mid,st,des,k);
		if(mid<des)update(rc[u],mid+1,r,st,des,k);
		pushup(u);
	}
	typedef pair<ll,ll> pll;
	inline pll operator +(cs pll &a,cs pll &b){
		return pll(a.fi+b.fi,a.se+b.se);
	}
	pll query(int u,int l,int r,int st,int des){
		if(st<=l&&r<=des)return pll(s[u],coef[u]);
		pll res(0,0);
		if(st<=mid)res=res+query(lc[u],l,mid,st,des);
		if(mid<des)res=res+query(rc[u],mid+1,r,st,des);
		res.fi+=res.se*tag[u];
		return res;
	}
	#undef mid
}
char yyy;
void dfs1(int u){
	siz[u]=1;
	for(pii &x:e[u]){
		if(x.fi==fa[u])continue;
		fa[x.fi]=u,dep[x.fi]=dep[u]+1,dis[x.fi]=dis[u]+x.se,del[x.fi]=x.se;
		dfs1(x.fi),siz[u]+=siz[x.fi];
		if(siz[x.fi]>siz[son[u]])son[u]=x.fi;
	}
}
void dfs2(int u,int tp){
	in[u]=++dfn,top[u]=tp,idx[dfn]=u;
	if(son[u])dfs2(son[u],tp);
	for(pii &x:e[u]){
		if(x.fi==fa[u]||x.fi==son[u])continue;
		dfs2(x.fi,x.fi);
	}
	out[u]=dfn;
}
inline int Lca(int u,int v){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		u=fa[top[u]];
	}
	return dep[u]<dep[v]?u:v;
}
inline void update(int &r1,int u,int k){
	while(top[u]){
		Seg::update(r1,1,n,in[top[u]],in[u],k);
		u=fa[top[u]];
	}
}
inline ll query(int &r1,int u){
	ll res=0;
	while(top[u]){
		res+=Seg::query(r1,1,n,in[top[u]],in[u]).fi;
		u=fa[top[u]];
	} 
	return res;
}
inline ll calc(int u,int k){
	return dis[k]*dep[u]+s[u]-2ll*query(rt[u],k);
}
void dfs3(int u){
	s[u]+=dis[p[u]];
	update(rt[u],p[u],1);
	for(pii &x:e[u]){
		if(x.fi==fa[u])continue;
		s[x.fi]+=s[u],rt[x.fi]=rt[u];
		dfs3(x.fi);
	}
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	int type=read();
	n=read(),q=read();
	for(int i=1;i<n;i++){
		int u=read(),v=read(),w=read();
		e[u].pb(pii(v,w)),e[v].pb(pii(u,w));
	}dep[1]=1;
	for(int i=1;i<=n;i++)p[i]=read();
	dfs1(1),dfs2(1,1);
	Seg::build(rt[1],1,n),dfs3(1);
//	for(int i=1;i<=n;i++)cout<<s[i]<<" ";puts("");
	while(q--){
		int u=readll()^(last*type),v=readll()^(last*type),k=readll()^(last*type);
		cout<<(last=calc(u,k)+calc(v,k)-calc(fa[Lca(u,v)],k)-calc(Lca(u,v),k))<<'\n';
	//	cout<<calc(u,k)<<" "<<calc(v,k)<<" "<<calc(fa[Lca(u,v)],k)<<" "<<calc(Lca(u,v),k)<<'\n';
	}
}
posted @ 2020-02-18 19:59  Stargazer_cykoi  阅读(165)  评论(0编辑  收藏  举报