Pagini recente » Cod sursa (job #969272) | Cod sursa (job #1713276) | Cod sursa (job #3319160) | Cod sursa (job #2584244) | Cod sursa (job #3327212)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("heavypath.in");
ofstream cout("heavypath.out");
int n,q,A[100005];
int idx,G[200005],top[100005],nxt[200005];
int sz[100005],fa[100005],head[100005],lvl[100005],hv_son[100005],pos[100005],tmp;
int aint[200005];
void add_edge(int x,int y)
{
idx++;
G[idx]=y;
nxt[idx]=top[x];
top[x]=idx;
}
void build()
{
for(int i=0;i<n;i++)
aint[n+i]=A[pos[i+1]];
for(int i=n-1;i>=1;i--)
aint[i]=max(aint[2*i],aint[2*i+1]);
}
void update(int x,int v)
{
x+=n-1;
aint[x]=v;
for(x>>=1;x;x>>=1)
aint[x]=max(aint[2*x],aint[2*x+1]);
}
int query(int l,int r)
{
int ans=0;
for(l+=n-1,r+=n;l<r;l>>=1,r>>=1)
{
if(l&1) ans=max(ans,aint[l++]);
if(r&1) ans=max(ans,aint[--r]);
}
return ans;
}
void dfs(int x,int tata)
{
int sz_max=0;
fa[x]=tata;
sz[x]=1;
lvl[x]=lvl[tata]+1;
for(int i=top[x];i;i=nxt[i])
{
int y=G[i];
if(y==tata)
continue;
dfs(y,x);
sz[x]+=sz[y];
if(sz[y]>sz_max)
{
sz_max=sz[y];
hv_son[x]=y;
}
}
}
void decompose(int x,int hd)
{
head[x]=hd;
pos[x]=++tmp;
if(hv_son[x])
decompose(hv_son[x],hd);
for(int i=top[x];i;i=nxt[i])
{
int y=G[i];
if(y==fa[x] || y==hv_son[x])
continue;
decompose(y,y);
}
}
int solve(int x,int y)
{
int ans=0;
while(head[x]!=head[y])
{
if(lvl[head[x]]<lvl[head[y]])
swap(x,y);
ans=max(ans,query(pos[head[x]],pos[x]));
x=fa[head[x]];
}
if(lvl[x]>lvl[y])
swap(x,y);
ans=max(ans,query(pos[x],pos[y]));
return ans;
}
int main()
{
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>A[i];
for(int i=1;i<=n-1;i++)
{
int x,y;
cin>>x>>y;
add_edge(x,y);
add_edge(y,x);
}
dfs(1,1);
decompose(1,1);
build();
int op,x,y;
while(q--)
{
cin>>op>>x>>y;
if(op==0)
update(pos[x],y);
if(op==1)
cout<<solve(x,y)<<"\n";
}
return 0;
}