-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhld.cpp
More file actions
179 lines (154 loc) · 3.07 KB
/
Copy pathhld.cpp
File metadata and controls
179 lines (154 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vf first
#define vs second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
const int N = 200001;
vector<int> adj[N];
int heavy[N] , tin[N], timer, depth[N] , head[N] , parent[N];
int dfs(int node ,int par){
depth[node] = depth[par] + 1;
heavy[node]=-1;
parent[node] = par;
int mx = 0;
int pos = -1;
int total = 1;
for(int i : adj[node]){
if(i!=par){
int cur = dfs(i , node);
total += cur;
if(cur > mx){
mx = cur;
pos = i;
}
}
}
heavy[node] = pos;
return total;
}
void decompose(int node, int heavy_par){
tin[node] = timer++;
head[node] = heavy_par;
if(heavy[node] != -1){
decompose(heavy[node] , heavy_par);
}
for(int i : adj[node]){
if(i != parent[node] && i != heavy[node]){
decompose(i , i);
}
}
}
struct Seg {
int siz;
vector<int> v;
void init(int n){
siz=1;
while(siz < n)siz *= 2;
v.assign(siz*2,0);
}
void build(vector<int>& a , int x , int lx ,int rx){
if(rx - lx == 1){
if(lx < (int)a.size())v[x] = a[lx];
return;
}
int m = (lx + rx)/2;
build(a , 2 * x + 1 , lx , m);
build(a , 2 * x + 2 , m , rx);
v[x] = max(v[2 * x + 1] , v[2 * x + 2]);
}
void build(vector<int>& a){
build(a , 0 , 0 , siz);
}
void set(int pos , int val , int x, int lx ,int rx){
if(rx - lx == 1){
v[x] = val;
return;
}
int m = (lx + rx)/2;
if(pos < m){
set(pos , val , 2 * x + 1 , lx , m);
}
else set(pos , val , 2 * x + 2 , m , rx);
v[x] = max(v[2 * x + 1] , v[2 * x + 2]);
}
void set(int pos , int val){
set(pos, val , 0 , 0 , siz);
}
int range(int l , int r , int x , int lx , int rx){
if(l >= rx || lx >= r)return 0;
if(lx >= l && rx <= r)return v[x];
int m = (lx + rx)/2;
return max(range(l , r , 2 * x + 1 , lx , m) , range(l , r , 2 * x + 2 , m , rx));
}
int range(int l , int r){
return range(l , r , 0 , 0 , siz);
}
};
void solve(){
int n, q;
cin >> n >> q;
int arr[n];
for(int i = 0; i < n; i++)cin >> arr[i];
for(int i = 0; i < n-1; i++){
int a , b;
cin >> a >> b;
a--;
b--;
adj[a].pb(b);
adj[b].pb(a);
}
vector<int> v(n);
depth[0] = 0;
timer = 0;
dfs(0,0);
decompose(0 , 0);
for(int i = 0; i < n; i++){
v[tin[i]] = arr[i];
}
Seg st;
st.init(n);
st.build(v);
while(q--){
int type;
cin >> type;
if(type == 1){
int pos , x;
cin >> pos >> x;
pos--;
st.set(tin[pos] , x);
}
else {
int x , y;
cin >> x >> y;
x--;
y--;
int res = 0;
while(head[x] != head[y]){
//cout << head[x] << ' ' << head[y] << endl;
if(depth[head[x]] < depth[head[y]]){
swap(x,y);
}
res = max(res , st.range(tin[head[x]] , tin[x] + 1));
x = head[x];
x = parent[x];
//cout << x << ' ' << res << '\n';
}
if(depth[x] > depth[y]){
swap(x,y);
}
//cout << x << ' ' << y << ' ' << st.range(tin[x] , tin[y] + 1) << '\n';
res = max(res , st.range(tin[x] , tin[y] + 1));
cout << res << ' ';
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;
//cin >> t;
// while(t--)
solve();
}