题目
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
const int maxn = 1e5 + 5, inf = 1e18, maxm = 4e4 + 5;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const int N = 1e6;
// int a[505][5005];
// bool vis[505][505];
// char s[505][505];
int a[maxn], b[maxn];
bool vis[maxn];
string s;
int n, m;
int siz[maxn];
struct Node{
// int val, id;
// bool operator<(const Node &u)const{
// return val < u.val;
// }
int siz, id;
bool operator<(const Node &u)const{
// if(siz[id] != siz[u.id])
// return siz[id] > siz[u.id];
// return id < u.id;不能用利用全局数组比较,因为siz数组也在更新,可能出现set排序不及时的情况
if(siz != u.siz){
return siz > u.siz;
}
return id < u.id;
}
}c[maxn];
int ans[maxn];
// priority_queue<Node> G[maxn];
set<Node> G[maxn];
int f[maxn], fa[maxn];
vector<int> g[maxn];
void dfs(int u, int p){
fa[u] = p;
f[u] = a[u];
siz[u] = 1;
for(auto v : g[u]){
// int v = z.id;
if(v == p) continue;
dfs(v, u);
G[u].insert({siz[v], v});
// G[u].insert({v});
siz[u] += siz[v];
f[u] += f[v];
}
}
void solve(){
int res = 0;
int q, k;
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 1);
for(int tt = 1; tt <= m; tt++){
int op, x;
cin >> op >> x;
if(op == 1){
cout << f[x] << '\n';
}
else{
if(G[x].size() == 0) continue;
int par = fa[x];
int son = G[x].begin() -> id;
int tmp;
G[x].erase(Node{siz[son], son});
G[par].erase(Node{siz[x], x});
tmp = siz[x];
siz[x] -= siz[son];
siz[son] += tmp - siz[son];
G[son].insert(Node{siz[x], x});
G[par].insert(Node{siz[son], son});
tmp = f[x];
f[x] -= f[son];
f[son] += tmp - f[son];
fa[son] = par;
fa[x] = son;
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}