CSES-solutions/CSES Problem Set/Path Queries.cpp
2025-02-12 18:15:05 -03:00

213 lines
3.9 KiB
C++

/* Problem URL: https://cses.fi/problemset/task/1138/ */
#include <bits/stdc++.h>
using namespace std;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
vvi graph;
vi sizes;
vi parent;
vi head;
vl v;
vi pos;
vl seg;
int t = 0;
void dfs(int i, int p)
{
parent[i] = p;
sizes[i] = 1;
for (auto &u : graph[i]) {
if (u == p) {
continue;
}
dfs(u, i);
if (sizes[graph[i][0]] < sizes[u]) {
swap(graph[i][0], u);
}
sizes[i] += sizes[u];
}
}
void f(int i, int p, vl &values)
{
pos[i] = t;
v[t] = values[i];
t++;
for (auto u : graph[i]) {
if (u == p) {
continue;
}
if (u == graph[i][0]) {
head[u] = head[i];
} else {
head[u] = u;
}
f(u, i, values);
}
}
ll seg_query(int i, int l, int r, int la, int ra)
{
if (l > ra || r < la) {
return 0;
}
if (l >= la && r <= ra) {
return seg[i];
}
int mid = (l + r) / 2;
return seg_query(i * 2, l, mid, la, ra) + seg_query(i * 2 + 1, mid + 1, r, la, ra);
}
ll query(int a, int b)
{
if (pos[a] > pos[b]) {
swap(a, b);
}
if (head[a] == head[b]) {
return seg_query(1, 0, v.size() - 1, pos[a], pos[b]);
}
return seg_query(1, 0, v.size() - 1, pos[head[b]], pos[b]) + query(parent[head[b]], a);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vl values(n);
cin >> values;
graph.resize(n);
sizes.resize(n);
parent.resize(n);
head.resize(n);
v.resize(n);
pos.resize(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(0, -1);
f(0, -1, values);
while (__builtin_popcountll(v.size()) != 1) {
v.push_back(0);
}
size_t sz = v.size();
seg.resize(sz * 2);
for (size_t i = sz; i < sz * 2; i++) {
seg[i] = v[i - sz];
}
for (size_t i = sz - 1; i > 0; i--) {
seg[i] = seg[i * 2] + seg[i * 2 + 1];
}
while (q--) {
int op;
cin >> op;
if (op == 1) {
ll a, v;
cin >> a >> v;
a--;
seg[sz + pos[a]] = v;
for (size_t i = (sz + pos[a]) / 2; i > 0; i /= 2) {
seg[i] = seg[i * 2] + seg[i * 2 + 1];
}
continue;
}
int num;
cin >> num;
cout << query(0, num - 1) << '\n';
}
}