Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <stack>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #include <bitset>
- #include <random>
- #include <list>
- #include <unordered_map>
- #include <unordered_set>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef string str;
- //typedef __int128 ultraint;
- #define endl "\n"
- #define sqrt sqrtl
- #define F first
- #define S second
- #define all(vc666) vc666.begin(), vc666.end()
- //#define pow fast_pow
- //#define Vec Point
- const ll inf = (ll)1e18 + 7;
- ld EPS = 1e-9;
- ld Pi = 3.1415926535897932384;
- struct segtree {
- struct node {
- ll val = 0, push = 0;
- };
- vector<node> t;
- void build(ll n) {
- t.resize(4 * n);
- }
- void push(ll v, ll tl, ll tr) {
- if (!t[v].push) {
- return;
- }
- t[v].val += (tr - tl + 1) * t[v].push;
- if (v * 2 + 1 >= (ll)t.size()) {
- t[v].push = 0;
- return;
- }
- t[v * 2].push += t[v].push;
- t[v * 2 + 1].push += t[v].push;
- t[v].push = 0;
- }
- void update(ll v, ll tl, ll tr, ll l, ll r, ll x) {
- push(v, tl, tr);
- if (tl > r || tr < l) {
- return;
- }
- if (tl >= l && tr <= r) {
- t[v].push += x;
- push(v, tl, tr);
- return;
- }
- ll tm = (tl + tr) / 2;
- update(v * 2, tl, tm, l, r, x);
- update(v * 2 + 1, tm + 1, tr, l, r, x);
- t[v].val = t[v * 2].val + t[v * 2 + 1].val;
- }
- ll get_sum(ll v, ll tl, ll tr, ll l, ll r) {
- push(v, tl, tr);
- if (tl > r || tr < l) {
- return 0;
- }
- if (tl >= l && tr <= r) {
- return t[v].val;
- }
- ll tm = (tl + tr) / 2;
- return get_sum(v * 2, tl, tm, l, r) + get_sum(v * 2 + 1, tm + 1, tr, l, r);
- }
- };
- const ll sz2 = 18;
- vector <vector <ll> > g;
- vector <pair <ll, ll> > ti;// tin and tout
- vector <vector <ll> > up;
- vector <bool> u;
- vector <ll> color;
- segtree d;
- void dfs_timer(ll v, ll& time, ll p, ll sum) {
- u[v] = true;
- sum += color[v];
- ti[v].first = time;
- d.update(1, 0, u.size() - 1, ti[v].first, ti[v].first, sum);
- time++;
- up[v][0] = max(p, (ll)0);
- for (int i = 1; i <= sz2; i++) {
- up[v][i] = up[up[v][i - 1]][i - 1];
- }
- for (int i = 0; i < g[v].size(); i++) {
- if (!u[g[v][i]]) {
- dfs_timer(g[v][i], time, v, sum);
- }
- }
- ti[v].second = time;
- return;
- }
- bool pred(ll a, ll b) {
- return ti[a].first <= ti[b].first && ti[b].first < ti[a].second;
- }
- ll lca(ll a, ll b) {
- if (pred(a, b)) {
- return a;
- }
- for (int i = sz2; i >= 0; i--) {
- if (!pred(up[a][i], b)) {
- a = up[a][i];
- }
- }
- return up[a][0];
- }
- ll ans_way_pr(ll a, ll v) {
- ll ans = 0;
- if (v != a) {
- for (int i = sz2; i >= 0; i--) {
- if (!pred(up[a][i], v)) {
- //ans += f[a][i];
- a = up[a][i];
- }
- }
- //ans += f[a][0];
- }
- else {
- ans = color[a];
- }
- return ans;
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- ll t = 1;
- //cin >> t;
- while (t--) {
- ll n, m, i, j, x, y, timer = 0, lc, timer2 = 0;
- cin >> n;
- d.build(n);
- g.resize(n);
- color.resize(n);
- u.resize(n);
- up.resize(n, vector <ll>(sz2 + 1));
- ti.resize(n);
- for (i = 0; i < n; i++) {
- cin >> color[i];
- }
- for (i = 0; i < n - 1; i++) {
- cin >> x >> y;
- g[x - 1].push_back(y - 1);
- g[y - 1].push_back(x - 1);
- }
- dfs_timer(0, timer, 0, 0);
- string s;
- cin >> m;
- while (m--) {
- cin >> s;
- if (s == "?") {
- cin >> x >> y;
- x--;
- y--;
- lc = lca(x, y);
- cout << d.get_sum(1, 0, n - 1, ti[x].first, ti[x].first) + d.get_sum(1, 0, n - 1, ti[y].first, ti[y].first) - 2 * d.get_sum(1, 0, n - 1, ti[lc].first, ti[lc].first) + color[lc] << endl;
- }
- else {
- cin >> x >> y;
- x--;
- d.update(1, 0, n - 1, ti[x].first, ti[x].second - 1, y - color[x]);
- color[x] = y;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement