orazma

e.cpp

Jul 22nd, 2026
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define f first
  4. #define s second
  5. #define pb push_back
  6. #define mp make_pair
  7. #define ll long long
  8.  
  9. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  10.  
  11. using namespace std;
  12. const int MAXN = 1e6 + 10;
  13. const int L = 20;
  14. const int INF = 1e9 + 7;
  15. const int EPS = 1e-6;
  16. int timer = 0;
  17.  
  18. vector <int> g[MAXN];
  19. pair <string, pair <int, int> > b[MAXN];
  20. int tin[MAXN], tout[MAXN], a[MAXN];
  21. bool was[MAXN];
  22. int up[MAXN][21];
  23. bool upper(int u, int v){
  24.     return (tin[u] <= tin[v] && tout[v] <= tout[u]);
  25. }
  26.  
  27. void dfs(int v, int pr = 0){
  28.     was[v] = 1;
  29.     tin[v] = ++timer;
  30.     up[v][0] = pr;
  31.     for (int i = 1; i <= L; i++){
  32.         up[v][i] = up[up[v][i - 1]][i - 1];
  33.     }
  34.  
  35.     for (int to : g[v]){
  36.         if (!was[to]){
  37.             dfs(to, v);
  38.         }
  39.     }
  40.     tout[v] = ++timer;
  41. }
  42.  
  43. int lca(int a, int b){
  44.     if (upper(a, b)){
  45.         return a;
  46.     }
  47.     if (upper(b, a)){
  48.         return b;
  49.     }
  50.     for (int i = L; i >= 0; i--){
  51.         if (!upper(up[a][i], b)){
  52.             a = up[a][i];
  53.         }
  54.     }
  55.  
  56.     return up[a][0];
  57. }
  58. int t[4 * MAXN];
  59.  
  60. int get_sum(int v, int tl, int tr, int l, int r){
  61.     if (l > r){
  62.         return 0;
  63.     }
  64.     if (tl == l && tr == r){
  65.         return t[v];
  66.     }
  67.     int tm = (tl + tr) >> 1;
  68.     return get_sum(v + v, tl, tm, l, min(r, tm)) +
  69.             get_sum(v + v + 1, tm + 1, tr, max(l, tm + 1), r);
  70. }
  71.  
  72. void update(int v, int tl, int tr, int pos, int val){
  73.     if (tl > tr){
  74.         return;
  75.     }
  76.     if (tl == tr){
  77.         t[v] = val;
  78.         return;
  79.     }
  80.     int tm = (tl + tr) >> 1;
  81.     if (pos <= tm){
  82.         update(v + v, tl, tm, pos, val);
  83.     }
  84.     else{
  85.         update(v + v + 1, tm + 1, tr, pos, val);
  86.     }
  87.     t[v] = t[v + v] + t[v + v + 1];
  88. }
  89.  
  90. void solve(){
  91.     tin[0] = -INF;
  92.     tout[0] = INF;
  93.  
  94.     int n;
  95.     cin >> n;
  96.     for (int i = 1; i <= n; i++){
  97.         cin >> a[i];
  98.  
  99.     }
  100.     for (int i = 1; i < n; i++){
  101.         int u, v;
  102.         cin >> u >> v;
  103.         g[u].pb(v);
  104.         g[v].pb(u);
  105.     }
  106.  
  107.     dfs(1);
  108.     for (int i = 1; i <= n; i++){
  109.         update(1, 1, n + n, tin[i], a[i]);
  110.         update(1, 1, n + n, tout[i], -a[i]);
  111.     }
  112.  
  113.     int q;
  114.     cin >> q;
  115.     for (int i = 1; i <= q; i++){
  116.         int type;
  117.         cin >> type;
  118.         if (type == 0){
  119.             int u, v;
  120.             cin >> u >> v;
  121.             int v_lca = lca(u, v);
  122.             cout << get_sum(1, 1, n + n, tin[v_lca], tin[u]) +
  123.                     get_sum(1, 1, n + n, tin[v_lca], tin[v]) - a[v_lca] << "\n";
  124.  
  125.         }
  126.         else{
  127.             int u, cnt;
  128.             cin >> u >> cnt;
  129.             update(1, 1, n + n, tin[u], cnt);
  130.             update(1, 1, n + n, tout[u], -cnt);
  131.             a[u] = cnt;
  132.         }
  133.     }
  134. }
  135.  
  136. int main() {
  137.     IOS;
  138.     int t = 1;
  139.     //cin >> t;
  140.     while(t--){
  141.         solve();
  142.     }
  143.     return 0;
  144. }
  145.  
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment