Advertisement
ivnikkk

Untitled

Jul 6th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <stack>
  9. #include <iomanip>
  10. #include <fstream>
  11. #include <string>
  12. #include <set>
  13. #include <deque>
  14. #include <queue>
  15. #include <map>
  16. #include <bitset>
  17. #include <random>
  18. #include <list>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <cassert>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef long double ld;
  28. typedef string str;
  29. //typedef __int128 ultraint;
  30. #define endl "\n"
  31. #define sqrt sqrtl
  32. #define F first
  33. #define S second
  34. #define all(vc666) vc666.begin(), vc666.end()
  35. //#define pow fast_pow
  36. //#define Vec Point
  37.  
  38. const ll inf = (ll)1e18 + 7;
  39. ld EPS = 1e-9;
  40. ld Pi = 3.1415926535897932384;
  41.  
  42. struct segtree {
  43.     struct node {
  44.         ll val = 0, push = 0;
  45.     };
  46.     vector<node> t;
  47.     void build(ll n) {
  48.         t.resize(4 * n);
  49.     }
  50.     void push(ll v, ll tl, ll tr) {
  51.         if (!t[v].push) {
  52.             return;
  53.         }
  54.         t[v].val += (tr - tl + 1) * t[v].push;
  55.         if (v * 2 + 1 >= (ll)t.size()) {
  56.             t[v].push = 0;
  57.             return;
  58.         }
  59.         t[v * 2].push += t[v].push;
  60.         t[v * 2 + 1].push += t[v].push;
  61.         t[v].push = 0;
  62.     }
  63.     void update(ll v, ll tl, ll tr, ll l, ll r, ll x) {
  64.         push(v, tl, tr);
  65.         if (tl > r || tr < l) {
  66.             return;
  67.         }
  68.         if (tl >= l && tr <= r) {
  69.             t[v].push += x;
  70.             push(v, tl, tr);
  71.             return;
  72.         }
  73.         ll tm = (tl + tr) / 2;
  74.         update(v * 2, tl, tm, l, r, x);
  75.         update(v * 2 + 1, tm + 1, tr, l, r, x);
  76.         t[v].val = t[v * 2].val + t[v * 2 + 1].val;
  77.     }
  78.     ll get_sum(ll v, ll tl, ll tr, ll l, ll r) {
  79.         push(v, tl, tr);
  80.         if (tl > r || tr < l) {
  81.             return 0;
  82.         }
  83.         if (tl >= l && tr <= r) {
  84.             return t[v].val;
  85.         }
  86.         ll tm = (tl + tr) / 2;
  87.         return get_sum(v * 2, tl, tm, l, r) + get_sum(v * 2 + 1, tm + 1, tr, l, r);
  88.     }
  89. };
  90.  
  91. const ll sz2 = 18;
  92. vector <vector <ll> > g;
  93. vector <pair <ll, ll> > ti;// tin and tout
  94. vector <vector <ll> > up;
  95. vector <bool> u;
  96. vector <ll> color;
  97. segtree d;
  98.  
  99. void dfs_timer(ll v, ll& time, ll p, ll sum) {
  100.     u[v] = true;
  101.     sum += color[v];
  102.     ti[v].first = time;
  103.     d.update(1, 0, u.size() - 1, ti[v].first, ti[v].first, sum);
  104.     time++;
  105.     up[v][0] = max(p, (ll)0);
  106.     for (int i = 1; i <= sz2; i++) {
  107.         up[v][i] = up[up[v][i - 1]][i - 1];
  108.     }
  109.     for (int i = 0; i < g[v].size(); i++) {
  110.         if (!u[g[v][i]]) {
  111.             dfs_timer(g[v][i], time, v, sum);
  112.         }
  113.     }
  114.     ti[v].second = time;
  115.     return;
  116. }
  117.  
  118. bool pred(ll a, ll b) {
  119.     return ti[a].first <= ti[b].first && ti[b].first < ti[a].second;
  120. }
  121.  
  122. ll lca(ll a, ll b) {
  123.     if (pred(a, b)) {
  124.         return a;
  125.     }
  126.     for (int i = sz2; i >= 0; i--) {
  127.         if (!pred(up[a][i], b)) {
  128.             a = up[a][i];
  129.         }
  130.     }
  131.     return up[a][0];
  132. }
  133.  
  134. ll ans_way_pr(ll a, ll v) {
  135.     ll ans = 0;
  136.     if (v != a) {
  137.         for (int i = sz2; i >= 0; i--) {
  138.             if (!pred(up[a][i], v)) {
  139.                 //ans += f[a][i];
  140.                 a = up[a][i];
  141.             }
  142.         }
  143.         //ans += f[a][0];
  144.     }
  145.     else {
  146.         ans = color[a];
  147.     }
  148.     return ans;
  149. }
  150.  
  151. signed main() {
  152. #ifdef _DEBUG
  153.     freopen("input.txt", "r", stdin);
  154.     freopen("output.txt", "w", stdout);
  155. #endif
  156.     ios_base::sync_with_stdio(0);
  157.     cin.tie(NULL);
  158.     cout.tie(NULL);
  159.     ll t = 1;
  160.     //cin >> t;
  161.     while (t--) {
  162.         ll n, m, i, j, x, y, timer = 0, lc, timer2 = 0;
  163.         cin >> n;
  164.         d.build(n);
  165.         g.resize(n);
  166.         color.resize(n);
  167.         u.resize(n);
  168.         up.resize(n, vector <ll>(sz2 + 1));
  169.         ti.resize(n);
  170.         for (i = 0; i < n; i++) {
  171.             cin >> color[i];
  172.         }
  173.         for (i = 0; i < n - 1; i++) {
  174.             cin >> x >> y;
  175.             g[x - 1].push_back(y - 1);
  176.             g[y - 1].push_back(x - 1);
  177.         }
  178.         dfs_timer(0, timer, 0, 0);
  179.         string s;
  180.         cin >> m;
  181.         while (m--) {
  182.             cin >> s;
  183.             if (s == "?") {
  184.                 cin >> x >> y;
  185.                 x--;
  186.                 y--;
  187.                 lc = lca(x, y);
  188.                 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;
  189.             }
  190.             else {
  191.                 cin >> x >> y;
  192.                 x--;
  193.                 d.update(1, 0, n - 1, ti[x].first, ti[x].second - 1, y - color[x]);
  194.                 color[x] = y;
  195.             }
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement