萌新刚学树剖,样例,自己造的小样例都能过,交上去又wa又t,查错快查疯了
查看原帖
萌新刚学树剖,样例,自己造的小样例都能过,交上去又wa又t,查错快查疯了
178131
郁逸丶楼主2020/9/26 21:06
#include<iostream>
#define ll long long
#include<cstring>
#include<cstdio>
#define ls p<<1
#define rs p<<1|1
const int N=1e6+5; 
using namespace std;
ll n,m;
struct node{ll u,v,d,next;}a[N];
int tot,head[N];
int b[N],q;
void add(int u,int v)
{
	tot++;
	a[tot].u=u;
	a[tot].v=v;
	a[tot].next=head[u];
	head[u]=tot; 
}
inline int read()
{
	register int x=0,f=1;register char ch=getchar();
	while(ch<'0'||ch>'9') f=ch=='-'-1?:f,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}
struct tree{ll l,r,pre,size,add,maxn;}t[N];
ll son[N],f[N],deep[N],size[N];
void dfs1(ll u,ll fa,ll dep)
{
	deep[u]=dep;
	f[u]=fa;
	size[u]=1;
	for(int i=head[u];i;i=a[i].next)
	{
		ll v=a[i].v;
		if(v==fa)continue;
		dfs1(v,u,dep+1);
		size[u]+=size[v];
		if(size[u]>size[son[v]]) son[u]=v;
	}
}
ll top[N],id[N],cnt,rk[N];
void dfs2(ll u,ll topf)
{
	id[u]=++cnt;
	top[u]=topf;
	rk[cnt]=b[u];
	if(!son[u]) return ;
	dfs2(son[u],topf);
	for(int i=head[u];i;i=a[i].next)
	{
		ll v=a[i].v;
		if(!id[v]) dfs2(v,v);
	}
}
void update(ll p)
{
	t[p].pre=t[ls].pre+t[rs].pre;
	t[p].maxn=max(t[ls].maxn,t[rs].maxn);
}
void change1(ll p,ll x,ll y,ll z)
{
	if(x<=t[p].l&&y>=t[p].r)
	{
		t[p].pre=z;
		t[p].maxn=z;
		return ;
	}
	ll mid=t[p].l+t[p].r>>1;
	if(x<=mid) change1(ls,x,y,z);
	if(y>mid) change1(rs,x,y,z);
	update(p);
}
void change2(ll x,ll y,ll z)
{
	while(top[x]!=top[y])
	{
		if(deep[top[x]]<deep[top[y]]) swap(x,y);
		change1(1,id[top[x]],id[x],z);
		x=f[top[x]];
	}
	if(deep[x]>deep[y]) swap(x,y);
	change1(1,id[x],id[y],z);
}
ll ask1(ll p,ll x,ll y)
{
	if(x<=t[p].l&&y>=t[p].r) return t[p].pre;
	ll ans=0;
	ll mid=t[p].l+t[p].r>>1;
	if(x<=mid) ans+=ask1(ls,x,y);
	if(y>mid) ans+=ask1(rs,x,y);
	return ans;
}
ll ask3(ll p,ll x,ll y)
{
	if(x<=t[p].l&&y>=t[p].r) return t[p].maxn;
	ll ans=-N;
	ll mid=t[p].l+t[p].r>>1;
	if(x<=mid) ans=max(ans,ask3(ls,x,y));
	if(y>mid) ans=max(ans,ask3(rs,x,y));
	return ans;
}
ll ask2(ll x,ll y)
{
	int ans=0;
	while(top[x]!=top[y])
	{
		if(deep[top[x]]<deep[top[y]]) swap(x,y);
		ans+=ask1(1,id[top[x]],id[x]);
		x=f[top[x]];
	}
	if(deep[x]>deep[y]) swap(x,y);
	ans+=ask1(1,id[x],id[y]);
	return ans;
}
ll ask4(ll x,ll y)
{
	ll ans=-N;
	while(top[x]!=top[y])
	{
		if(deep[top[x]]<deep[top[y]]) swap(x,y);
		ans=max(ans,ask3(1,id[top[x]],id[x]));
		x=f[top[x]];
		//cout<<ans<<endl;
		
	}
	if(deep[x]>deep[y]) swap(x,y);
	ans=max(ans,ask3(1,id[x],id[y]));
	return ans;
}
void build(ll p,ll l,ll r)
{
	t[p].l=l;t[p].r=r;t[p].size=r-l+1;
	if(l==r)
	{
		t[p].pre=rk[l];
		t[p].maxn=rk[l];
		return ;
	} 
	ll mid=l+r>>1;
	if(l<=mid) build(ls,l,mid);
	if(r>mid) build(rs,mid+1,r);
	update(p); 
}
int main()
{
	cin>>n;
	for(int i=1;i<n;i++)
	{
		ll a,b;
		a=read();b=read();
		add(a,b);add(b,a);
	}
	for(int i=1;i<=n;i++)
	cin>>b[i];
	dfs1(1,0,1);
	dfs2(1,1);
	build(1,1,n);
	
	cin>>q;
	for(int i=1;i<=q;i++)
	{
		string s;int u,v;
		cin>>s;
		u=read();v=read();
		if(s=="QMAX") cout<<ask4(u,v)<<endl;
		if(s=="QSUM") cout<<ask2(u,v)<<endl;
		if(s=="CHANGE") change2(u,u,v);
	}
}
2020/9/26 21:06
加载中...