From 48169fb6f09cd1554fe139a47c0fe8b98f880fea Mon Sep 17 00:00:00 2001 From: Segcolt <9hmbzr275@mozmail.com> Date: Wed, 12 Feb 2025 18:15:05 -0300 Subject: [PATCH] Add another problem --- CSES Problem Set/Path Queries.cpp | 212 ++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 CSES Problem Set/Path Queries.cpp diff --git a/CSES Problem Set/Path Queries.cpp b/CSES Problem Set/Path Queries.cpp new file mode 100644 index 0000000..0bd2529 --- /dev/null +++ b/CSES Problem Set/Path Queries.cpp @@ -0,0 +1,212 @@ +/* Problem URL: https://cses.fi/problemset/task/1138/ */ + +#include + +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; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &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 +auto operator>>(istream &is, vector> &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'; + } +}