Advertisement
tien_noob

Path Queries - Euler Tour Technique - CSES

Sep 7th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. //Make CSP great again
  2. #include <bits/stdc++.h>
  3. #define TASK "TESTCODE"
  4. using namespace std;
  5. const int N = 2e5;
  6. int n, a[N + 1], q;
  7. vector<int> Adj[N + 1];
  8. struct FenwickTree
  9. {
  10.     long long b1[N + 1], b2[N + 1];
  11.     void add(long long b[], int pos, long long val)
  12.     {
  13.         for (; pos <= n; pos += pos & (-pos))
  14.         {
  15.             b[pos] += val;
  16.         }
  17.     }
  18.     void add_range(int l, int r, long long x)
  19.     {
  20.         add(b1, l, x);
  21.         add(b1, r + 1, -x);
  22.         add(b2, l, x * (l - 1));
  23.         add(b2, r + 1, -x * r);
  24.     }
  25.     long long sum(long long b[], int pos)
  26.     {
  27.         long long ret = 0;
  28.         for (; pos > 0; pos -= pos & (- pos))
  29.         {
  30.             ret += b[pos];
  31.         }
  32.         return ret;
  33.     }
  34.     long long prefix(int pos)
  35.     {
  36.         return sum(b1, pos) * pos - sum(b2, pos);
  37.     }
  38.     long long sum_range(int l, int r)
  39.     {
  40.         return prefix(r) - prefix(l - 1);
  41.     }
  42. };
  43. FenwickTree Tree;
  44. void read()
  45. {
  46.     cin >> n >> q;
  47.     for (int i = 1; i <= n; ++ i)
  48.     {
  49.         cin >> a[i];
  50.     }
  51.     for (int i = 1; i < n; ++ i)
  52.     {
  53.         int u, v;
  54.         cin >> u >> v;
  55.         Adj[u].push_back(v);
  56.         Adj[v].push_back(u);
  57.     }
  58. }
  59. int st[N + 1], ed[N + 1], timer = 0;
  60. void DFS(int u, int p)
  61. {
  62.     ++timer;
  63.     st[u] = timer;
  64.     for (int v : Adj[u])
  65.     {
  66.         if (v != p)
  67.         {
  68.             DFS(v, u);
  69.         }
  70.     }
  71.     ed[u] = timer;
  72. }
  73. void solve()
  74. {
  75.     DFS(1, 0);
  76.     for (int i = 1; i <= n; ++ i)
  77.     {
  78.         Tree.add_range(st[i], ed[i], 1LL * a[i]);
  79.     }
  80.     while(q--)
  81.     {
  82.         int t;
  83.         cin >> t;
  84.         if (t == 1)
  85.         {
  86.             int u, x;
  87.             cin >> u >> x;
  88.             Tree.add_range(st[u], ed[u], x - a[u]);
  89.             a[u] = x;
  90.         }
  91.         else
  92.         {
  93.             int u;
  94.             cin >> u;
  95.             cout << Tree.sum_range(st[u], st[u]) << '\n';
  96.         }
  97.     }
  98. }
  99. int main()
  100. {
  101.     ios_base::sync_with_stdio(false);
  102.     cin.tie(nullptr);
  103.     if (fopen(TASK".INP", "r"))
  104.     {
  105.         freopen(TASK".INP", "r", stdin);
  106.         //freopen(TASK".OUT", "w", stdout);
  107.     }
  108.     int t = 1;
  109.     bool typetest = false;
  110.     if (typetest)
  111.     {
  112.         cin >> t;
  113.     }
  114.     for (int __ = 1; __ <= t; ++ __)
  115.     {
  116.         read();
  117.         solve();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement