蒟蒻只能过样例代码求调教
  • 板块P4114 Qtree1
  • 楼主_AyachiNene
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/5/17 20:50
  • 上次更新2023/10/23 15:29:15
查看原帖
蒟蒻只能过样例代码求调教
754502
_AyachiNene楼主2023/5/17 20:50
#include<bits/stdc++.h>
#define ls root*2
#define rs root*2+1
#define mid (t[root].l+t[root].r)/2
#define maxn 114514
using namespace std;
int head[maxn],cnt1,w[maxn],n;
struct node
{
	int nxt,to,val;
}e[maxn*2];
void add_edge(int u,int v,int val)
{
	e[++cnt1].val=val;
	e[cnt1].to=v;
	e[cnt1].nxt=head[u];
	head[u]=cnt1;
}
struct node1
{
	int l,r,max_;
}t[maxn];
//--------------------------------
void bld(int l,int r,int root)
{
	t[root].l=l;
	t[root].r=r;
	if(l==r)
	{
		t[root].max_=w[l];
		return;
	}
	bld(l,mid,ls);
	bld(mid+1,r,rs);
	t[root].max_=max(t[ls].max_,t[rs].max_);
}
void add(int x,int root,int k)
{
	if(t[root].l==t[root].r)
	{
		t[root].max_=k;
		return;
	}
	if(x<=mid)
		add(x,ls,k);
	else
		add(x,rs,k);
	t[root].max_=max(t[ls].max_,t[rs].max_);
}
int _max(int x,int y,int root)
{
	int ans=0;
	if(t[root].l>=x&&t[root].r<=y)
		return t[root].max_;
	if(x<=mid)
		ans=max(_max(x,y,ls),ans);
	if(y>mid)
		ans=max(_max(x,y,rs),ans);
	return ans; 
}
//--------------------------------
int cnt,dfn[maxn],top[maxn],dep[maxn],f[maxn],size[maxn],son[maxn],a[maxn];
void dfs1(int u,int fa)
{
	size[u]=1;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v!=fa)
		{
			a[v]=e[i].val;
			dep[v]=dep[u]+1;
			f[v]=u;
			dfs1(v,u);
			size[u]+=size[v];
			if(size[son[u]]<size[v])
				son[u]=v;
		}
	}
}
void dfs2(int u,int t)
{
	dfn[u]=++cnt;
	w[cnt]=a[u];
	top[u]=t;
	if(son[u])
		dfs2(son[u],t);
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v!=f[u]&&v!=son[u])
			dfs2(v,v);
	}
}
int query(int x,int y)
{
	int ans=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]])
			swap(x,y);
		ans=max(ans,_max(dfn[top[x]],dfn[x],1));
		x=f[top[x]];
	}
	if(dep[x]>dep[y])
		swap(x,y);
	ans=max(ans,_max(dfn[x]+1,dfn[y],1));
	return ans;
}
int main()
{
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int x,y,z;
		cin>>x>>y>>z;
		add_edge(x,y,z);
		add_edge(y,x,z);
	}
	dfs1(1,0);
	dfs2(1,1);
	bld(1,n,1);
	while(114514)
	{
		string s;
		cin>>s;
		if(s[0]=='D')
			return 0;
		else if(s[0]=='Q')
		{
			int x,y;
			cin>>x>>y;
			if(x==y)
				cout<<0<<endl;
			else
				cout<<query(x,y)<<endl;
		}
		else
		{
			int x,t;
			cin>>x>>t;
			add(dfn[x]+1,1,t);
		}
	}
}
2023/5/17 20:50
加载中...