ivnikkk

Untitled

May 22nd, 2022 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(a) a.begin(), a.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef pair<int, int> pii;
  9. typedef long double ld;
  10. vector<vector<int>> gr;
  11. vector<int> head, siz, tin, tout, w, parent, a;
  12. const int inf = INT_MAX;
  13. struct segtree {
  14.     vector<int> t;
  15.     int n;
  16.     segtree() {}
  17.     void init(int _n) {
  18.         n = _n;
  19.         t.resize(4 * n + 100, -inf);
  20.     }
  21.     void build(int v, int tl, int tr, vector<int>& a) {
  22.         if (tl == tr) {
  23.             t[v] = a[tl];
  24.             return;
  25.         }
  26.         int tm = (tl + tr) / 2;
  27.         build(v * 2, tl, tm, a);
  28.         build(v * 2 + 1, tm + 1, tr, a);
  29.         t[v] = max(t[v * 2], t[v * 2 + 1]);
  30.     }
  31.     void upd(int v, int tl, int tr, int pos, int val) {
  32.         if (tl == tr) {
  33.             t[v] = val;
  34.             return;
  35.         }
  36.         int tm = (tl + tr) / 2;
  37.         if (pos <= tm) {
  38.             upd(v * 2, tl, tm, pos, val);
  39.         }
  40.         else {
  41.             upd(v * 2 + 1, tm + 1, tr, pos, val);
  42.         }
  43.         t[v] = max(t[v * 2], t[v * 2 + 1]);
  44.     }
  45.     int get_mx(int v, int tl, int tr, int l, int r) {
  46.         if (tl > r || tr < l) {
  47.             return -INT_MAX;
  48.         }
  49.         if (tl >= l && tr <= r) {
  50.             return t[v];
  51.         }
  52.         int tm = (tl + tr) / 2;
  53.         return max(get_mx(v * 2, tl, tm, l, r), get_mx(v * 2 + 1, tm + 1, tr, l, r));
  54.     }
  55. };
  56. int t = 0;
  57. void dfs1(int v, int p) {
  58.     siz[v] = 1;
  59.     for (int& u : gr[v]) {
  60.         if (u == p) {
  61.             swap(u, gr[v].back());
  62.             gr[v].pop_back();
  63.             break;
  64.         }
  65.     }
  66.     parent[v] = (p == -1 ? v : p);
  67.     for (int& u : gr[v]) {
  68.         dfs1(u, v);
  69.         siz[v] += siz[u];
  70.         if (siz[u] > siz[gr[v][0]]) {
  71.             swap(gr[v][0], u);
  72.         }
  73.     }
  74. }
  75. void dfs2(int v) {
  76.     tin[v] = t++;
  77.     a.push_back(w[v]);
  78.     for (int& u : gr[v]) {
  79.         if (u == gr[v][0]) {
  80.             head[u] = head[v];
  81.         }
  82.         else {
  83.             head[u] = u;
  84.         }
  85.         dfs2(u);
  86.     }
  87.     tout[v] = t;
  88. }
  89. segtree Euler;
  90. void upd(int v, int val) {
  91.     Euler.upd(1, 0, Euler.n - 1, tin[v], val);
  92. }
  93. bool is_in_sector(int u, int v) {
  94.     return tin[u] <= tin[v] && tin[v] < tout[u];
  95. }
  96. void up(int& u, int& v, int& ans) {
  97.     while (!is_in_sector(head[u], v)) {
  98.         ans = max(ans, Euler.get_mx(1, 0, Euler.n - 1, tin[head[u]], tin[u]));
  99.         u = parent[head[u]];
  100.     }
  101. }
  102. int get_max(int u, int v) {
  103.     int ans = -inf;
  104.     up(u, v, ans);
  105.     up(v, u, ans);
  106.     if (!is_in_sector(u, v)) {
  107.         swap(u, v);
  108.     }
  109.     ans = max(ans, Euler.get_mx(1, 0, Euler.n - 1, tin[u], tin[v]));
  110.     return ans;
  111. }
  112. signed main() {
  113. #ifdef _DEBUG
  114.     freopen("input.txt", "r", stdin);
  115.     freopen("output.txt", "w", stdout);
  116. #endif
  117.     ios_base::sync_with_stdio(false);
  118.     cin.tie(nullptr);
  119.     cout.tie(nullptr);
  120.     int n;
  121.     cin >> n;
  122.     gr.resize(n), tin.resize(n), tout.resize(n), siz.resize(n), w.resize(n), head.resize(n), parent.resize(n);
  123.     for (int i = 0; i < n; i++) {
  124.         cin >> w[i];
  125.     }
  126.     for (int i = 0; i < n - 1; i++) {
  127.         int u, v;
  128.         cin >> u >> v;
  129.         u--, v--;
  130.         gr[u].push_back(v);
  131.         gr[v].push_back(u);
  132.     }
  133.     dfs1(0, -1);
  134.     dfs2(0);
  135.     Euler.init((int)a.size());
  136.     Euler.build(1, 0, Euler.n - 1, a);
  137.     int q;
  138.     cin >> q;
  139.     while (q--) {
  140.         char indef;
  141.         cin >> indef;
  142.         if (indef == '?') {
  143.             int u, v;
  144.             cin >> u >> v;
  145.             u--, v--;
  146.             cout << get_max(u, v) << '\n';
  147.         }
  148.         if (indef == '!') {
  149.             int v, x;
  150.             cin >> v >> x;
  151.             v--;
  152.             upd(v, x);
  153.         }
  154.     }
  155. }
Add Comment
Please, Sign In to add comment