2019.01.20【TJOI2015】【BZOJ3999】【洛谷P3976】旅游(树链剖分)(线段树)

版权声明:转载请声明出处,谢谢配合。 https://blog.csdn.net/zxyoi_dreamer/article/details/86561881

BZOJ传送门

洛谷传送门


解析:

这个LCT和链剖都能做的,不过链剖空间常数大,而LCT空间常数小而时间常数大。

思路:

考虑重链剖分, D F S DFS 优先重儿子构造出DFS序,建立线段树,同时维护由DFS序大的向DFS序小的走的答案,和DFS序小的向DFS序大的走的答案。

还要维护最大值和最小值。剩下的就是无脑跳轻重链了。


代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define re register
#define gc get_char
#define pc putchar
#define cs const
#define int ll

namespace IO{
	namespace IOONLY{
		cs int Rlen=1<<18|1;
		char buf[Rlen],*p1,*p2;
	}
	inline char get_char(){
		using namespace IOONLY;
		return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
	}
	
	inline ll getint(){
		re ll num;
		re char c;
		while(!isdigit(c=gc()));num=c^48;
		while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
		return num;
	}
	inline void outint(ll a){
		static char ch[23];
		if(a==0)pc('0');
		while(a)ch[++ch[0]]=a-a/10*10,a/=10;
		while(ch[0])pc(ch[ch[0]--]^48);
	}
}
using namespace IO;

cs int N=50004;
int n;
int last[N],nxt[N<<1],to[N<<1],ecnt;
inline void addedge(int u,int v){
	nxt[++ecnt]=last[u],last[u]=ecnt,to[ecnt]=v;
	nxt[++ecnt]=last[v],last[v]=ecnt,to[ecnt]=u;
}

int fa[N],son[N],top[N],dep[N],pos[N],in[N],out[N],dfs_clock;
int siz[N];
void dfs1(int u){
	siz[u]=1;
	for(int re e=last[u],v=to[e];e;v=to[e=nxt[e]]){
		if(v==fa[u])continue;
		fa[v]=u;
		dep[v]=dep[u]+1;
		dfs1(v);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])son[u]=v;
	}
}

void dfs2(int u){
	pos[in[u]=++dfs_clock]=u;
	if(son[u]){
		top[son[u]]=top[u];
		dfs2(son[u]);
	}
	for(int re e=last[u],v=to[e];e;v=to[e=nxt[e]]){
		if(v==fa[u]||v==son[u])continue;
		top[v]=v;
		dfs2(v);
	}
	out[u]=dfs_clock;
}

inline void tree_dissection(){
	dfs1(1);
	top[1]=1;
	dfs2(1);
}

struct node{
	ll mn,mx,best,rbest;
	node(cs ll &_mn=0,cs ll &_mx=0,cs ll &_be=0,cs ll &_rbe=0):mn(_mn),mx(_mx),best(_be),rbest(_rbe){}
	node rev()cs{return node(mn,mx,rbest,best);}
	friend node operator+(cs node &a,cs node &b){
		return node(min(a.mn,b.mn),max(a.mx,b.mx),
			max(max(a.best,b.best),a.mx-b.mn),max(max(a.rbest,b.rbest),b.mx-a.mn));
	}
}nd[N<<2];
ll val[N];
ll add[N<<2];
int p[N];
inline void pushadd(int k,ll c){
	nd[k].mn+=c;
	nd[k].mx+=c;
	add[k]+=c;
}

inline void pushdown(int k){
	if(add[k]){
		pushadd(k<<1,add[k]);
		pushadd(k<<1|1,add[k]);
		add[k]=0;
	}
}

void build(int k,int l,int r){
	if(l==r){
		p[pos[l]]=k;
		nd[k]=node(val[pos[l]],val[pos[l]],0,0);
		return ;
	}
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	nd[k]=nd[k<<1]+nd[k<<1|1];
}

node query(int k,int l,int r,cs int &ql,cs int &qr){
	if(ql<=l&&r<=qr)return nd[k];
	pushdown(k);
	int mid=(l+r)>>1;
	if(qr<=mid)return query(k<<1,l,mid,ql,qr);
	if(mid+1<=ql)return query(k<<1|1,mid+1,r,ql,qr);
	return query(k<<1,l,mid,ql,qr)+query(k<<1|1,mid+1,r,ql,qr);
}

void modifyadd(int k,int l,int r,cs int &ql,cs int &qr,cs ll &val){
	if(ql<=l&&r<=qr)return pushadd(k,val);
	pushdown(k);
	int mid=(l+r)>>1;
	if(ql<=mid)modifyadd(k<<1,l,mid,ql,qr,val);
	if(mid<qr)modifyadd(k<<1|1,mid+1,r,ql,qr,val);
	nd[k]=nd[k<<1]+nd[k<<1|1];
}

node query(int u,int v){
	if(top[u]==top[v]){
		if(dep[u]>dep[v])return query(1,1,n,in[v],in[u]).rev();
		else return query(1,1,n,in[u],in[v]);
	}
	if(dep[top[u]]>dep[top[v]])return query(1,1,n,in[top[u]],in[u]).rev()+query(fa[top[u]],v);
	else return query(u,fa[top[v]])+query(1,1,n,in[top[v]],in[v]);
}

void modify(int u,int v,ll c){
	while(top[u]!=top[v]){
		if(dep[top[u]]>dep[top[v]])swap(u,v);
		modifyadd(1,1,n,in[top[v]],in[v],c);
		v=fa[top[v]];
	}
	if(dep[u]>dep[v])swap(u,v);
	modifyadd(1,1,n,in[u],in[v],c);
}

signed main(){
	n=getint();
	for(int re i=1;i<=n;val[i++]=getint());
	for(int re i=1;i++<n;addedge(getint(),getint()));
	tree_dissection();
	build(1,1,n);
	for(int re Q=getint();Q--;pc('\n')){
		int u=getint(),v=getint();
		outint(query(v,u).best);
		modify(u,v,getint());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zxyoi_dreamer/article/details/86561881