Submission #886652

# Submission time Handle Problem Language Result Execution time Memory
886652 2023-12-12T14:57:21 Z erray Grapevine (NOI22_grapevine) C++17
100 / 100
906 ms 109932 KB
#include <bits/stdc++.h>

using namespace std;

template<typename A, typename B>
string to_string(pair<A, B>);
string to_string(string S) {
  return '"' + S + '"';
}
string to_string(const char* c) {
  return to_string(string(c));
}
string to_string(bool b) {
  return (b ? "true" : "false");
}
template<size_t T>
string to_string(bitset<T> bs) {
  return bs.to_string();
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < int(v.size()); ++i) {
    if (int(res.size()) > 1) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  return res + "}";
}
template<typename T>
string to_string(T v) {
  string res = "{";
  for (auto e : v) {
    if (int(res.size()) > 1) {
      res += ", ";
    }
    res += to_string(e);
  }
  return res + "}";
}
template<typename A, typename B>
string to_string(pair<A, B> p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
void debug_out() {
  cerr << endl;
}
template<typename H, typename... T>
void debug_out(H head, T... tail) {
  cerr << "  " << to_string(head);
  debug_out(tail...);
}

#ifdef DEBUG 
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 
#else 
  #define debug(...) void(37)
#endif


const int BLOCK = 16; // divide smaller seg trees onto blocks if it gets TLE
const long long inf = 1LL << 55;
#define def int mid = (l + r) >> 1, rv  = v + ((mid - l + 1) << 1)
#define pull(v) tree[v].mn = min((l < mid) || act[l] ? tree[v + 1].mn : inf, (mid + 1 < r) || act[r] ? tree[rv].mn : inf) //change to if statements if it gets TLE
#define push(v) \
if (tree[v].tag != 0) { \
  tree[v + 1].modify(tree[v].tag); \
  tree[rv].modify(tree[v].tag); \
  tree[v].tag = 0; \
}

struct node {
  long long mn = 0;
  long long tag = 0;
  void modify(long long x) {
    mn += x;
    tag += x;
  }
};
struct SegTree {
  vector<node> tree;
  vector<bool> act;
  int n;
  void modify(int v, int l, int r, int ll, int rr, int x) {
    if (l >= ll && rr >= r) {
      tree[v].modify(x);
      return;
    }
    def;
    push(v);
    if (ll <= mid) {
      modify(v + 1, l, mid, ll, rr, x);
    } 
    if (mid < rr) {
      modify(rv, mid + 1, r, ll, rr, x);
    }
    pull(v);
  }
  int path(int v, int l, int r, int p) {
    if (l == r) {
      return v;
    }
    def;
    push(v);
    int res = -1;
    if (p <= mid) {
      res = path(v + 1, l, mid, p);
    } else {
      res = path(rv, mid + 1, r, p);
    }
    pull(v);
    //debug(v, l, r, tree[v].mn);
    return res;
  }
  void build(int v, int l, int r, vector<int>& a) {
    if (l == r) {
      tree[v].mn = a[l];
      return;
    }
    def;
    build(v + 1, l, mid, a);
    build(rv, mid + 1, r, a);
  }
  SegTree(int _n, vector<int>& a) : n(_n) {
    tree.resize(2 * n);
    act.resize(n);
    build(0, 0, n - 1, a); //same as 137
  }
  SegTree(int _n) : n(_n) {
    tree.resize(2 * n);
    if (n > 1) tree[0].mn = inf;
    act.resize(n);
  }
  SegTree() { } 
  long long mn() {
    return (n > 1 || act[0] ? tree[0].mn : inf);
  }
  long long dist(int i) {
    return tree[path(0, 0, n - 1, i)].mn; 
  }
  void change(int i) {
    act[i] = !act[i];
    path(0, 0, n - 1, i);
  }
  void modify(int ll, int rr, int x) {
    modify(0, 0, n - 1, ll, rr, x); //change variables to globals if it get's tle 
  }
};
#undef def
#undef pull
#undef push

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, QQ;
  cin >> N >> QQ;
  vector<vector<int>> g(N);
  vector<int> V(N - 1), U(N - 1), W(N - 1);
  for (int i = 0; i < N - 1; ++i) {
    cin >> V[i] >> U[i] >> W[i];
    --V[i], --U[i];
    g[V[i]].push_back(U[i]);
    g[U[i]].push_back(V[i]);
  }
  vector<int> tin(N, -1), tout(N), d(N);
  int tt = 0;
  function<void(int)> Dfs = [&](int v) {
    tin[v] = tt++;
    for (auto u : g[v]) {
      if (tin[u] == -1) {
        d[u] = d[v] + 1;
        Dfs(u);
      }
    }
    tout[v] = tt - 1;
  };
  Dfs(0);
  debug(tin, tout, d);
  vector<int> size(N);
  for (int i = 0; i < N; ++i) {
    size[i] = tout[i] - tin[i] + 1;
  }
  vector<int> par(N, -1);
  vector<int> cent_d(N);
  function<void(int, int)> Centroid = [&](int v, int from) {
    int to = -1;
    for (auto u : g[v]) {
      if (size[u] * 2 > size[v]) {
        to = u;
        break;
      }
    }
    if (to == -1) {
      par[v] = from;
      size[v] = 0;
      cent_d[v] = (from == -1 ? 0 : cent_d[from] + 1);
      for (auto u : g[v]) {
        if (size[u] > 0) {
          Centroid(u, v);
        }
      }
    } else {
      size[v] -= size[to];
      size[to] += size[v];
      Centroid(to, from);
    }
  };
  Centroid(0, -1);
  debug(par, cent_d);
  vector<int> inv_in(N);
  for (int i = 0; i < N; ++i) {
    inv_in[tin[i]] = i;
  }
  vector<vector<int>> inv_out(N);
  for (int i = 0; i < N; ++i) {
    inv_out[tout[i]].push_back(i);
  } 
  vector<vector<int>> in(N), out(N); //change sizes to const if it gets tle 
  for (int v = 0; v < N; ++v) {
    in[v].resize(cent_d[v] + 1);
    out[v].resize(cent_d[v] + 1);
  }
  vector<vector<int>> st_inds(N);
  for (auto v : inv_in) {
    int x = v;
    while (v != -1) {
      in[x][cent_d[v]] = int(st_inds[v].size());
      st_inds[v].push_back(tin[x]);
      v = par[v];
    }
  }
  vector<int> pt(N);
  for (int foo = 0; foo < N; ++foo) for (auto v : inv_out[foo]) {
    int p = v;
    while (p != -1) {
      int s = int(st_inds[p].size());
      while (pt[p] + 1 < s && st_inds[p][pt[p] + 1] <= tout[v]) {
        ++pt[p];
      }
      out[v][cent_d[p]] = pt[p];
      p = par[p];
    }
  }
  debug(in, out);
  vector<SegTree> st(N);
  for (int i = 0; i < N; ++i) {
    debug(i, st_inds[i]);
    st[i] = SegTree(int(st_inds[i].size()));
  }
  
  debug(inv_in);
  auto Lca = [&](int a, int b) {
    if (cent_d[a] > cent_d[b]) {
      swap(a, b);
    }
    while (cent_d[b] > cent_d[a]) {
      b = par[b];
    }
    while (a != b) {
      a = par[a], b = par[b];
    }
    return a;
  };
  auto Par = [&](int a, int b) {
    return tin[a] <= tin[b] && tout[a] >= tin[b];
  };
  
  auto Upd = [&](int a, int b, int w) {
    debug(a, b, w);
    int v = Lca(a, b);
    if (d[a] > d[b]) {
      swap(a, b);
    }
    int sub = b;
    while (v != -1) {
      int L = in[sub][cent_d[v]];
      int R = out[sub][cent_d[v]];
      bool inc = !Par(sub, v);
      debug(v, st_inds[v], L, R, w, inc); 
      //reduce two updates to one with constants if it get's tle 
      if (inc) { 
        st[v].modify(L, R, w);
      } else {
        if (L > 0) {
          st[v].modify(0, L - 1, w);
        }
        if (R + 1 < st[v].n) {
          st[v].modify(R + 1, st[v].n - 1, w);
        }
      }
      v = par[v];
    }
  };
  vector<int> new_W(N);
  for (int i = 0; i < N - 1; ++i) {
    if (d[V[i]] > d[U[i]]) {
      swap(V[i], U[i]);
    }
    new_W[U[i]] = W[i];
    Upd(V[i], U[i], W[i]);
  }
  /*
  for (int v = 0; v < N; ++v) {
    int p = v;
    while (p != -1) {
      debug(p, v, st[p].dist(in[v][cent_d[p]]));
      p = par[p];
    }
  }
  */
  swap(new_W, W);
  debug(W);
  int c = 0;
  vector<bool> act(N);
  while (QQ--) {
    int T;
    cin >> T;
    if (T == 1) {
      int Q;
      cin >> Q;
      --Q;
      int v = Q;
      debug(Q);
      long long ans = inf;
      while (v != -1) {
        debug(v, st[v].dist(in[Q][cent_d[v]]), st[v].mn());
        ans = min(ans, st[v].dist(in[Q][cent_d[v]]) + st[v].mn());
        v = par[v];
      }
      cout << (c == 0 ? -1LL : ans) << '\n';
    } else if (T == 2) {
      int U_i;
      cin >> U_i;
      --U_i;
      int v = U_i;
      c += (act[v] ? -1 : +1);
      act[v] = !act[v];
      while (v != -1) {
        debug(v, in[U_i][cent_d[v]]);
        st[v].change(in[U_i][cent_d[v]]);
        v = par[v];
      }
    } else {
      int A, B, W_i;
      cin >> A >> B >> W_i;
      --A, --B;
      if (d[A] > d[B]) {
        swap(A, B);
      }
      debug(A, B, W_i - W[B]);
      Upd(A, B, W_i - W[B]);
      W[B] = W_i;
    }
  }
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1884 KB Output is correct
2 Correct 6 ms 1628 KB Output is correct
3 Correct 7 ms 1884 KB Output is correct
4 Correct 5 ms 1884 KB Output is correct
5 Correct 5 ms 1884 KB Output is correct
6 Correct 5 ms 1884 KB Output is correct
7 Correct 2 ms 1368 KB Output is correct
8 Correct 2 ms 1384 KB Output is correct
9 Correct 2 ms 1372 KB Output is correct
10 Correct 7 ms 1884 KB Output is correct
11 Correct 5 ms 1884 KB Output is correct
12 Correct 5 ms 1884 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1372 KB Output is correct
15 Correct 4 ms 1368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 680 ms 90420 KB Output is correct
2 Correct 747 ms 88376 KB Output is correct
3 Correct 709 ms 88300 KB Output is correct
4 Correct 601 ms 107256 KB Output is correct
5 Correct 591 ms 100108 KB Output is correct
6 Correct 602 ms 102708 KB Output is correct
7 Correct 111 ms 50892 KB Output is correct
8 Correct 123 ms 49440 KB Output is correct
9 Correct 145 ms 49612 KB Output is correct
10 Correct 619 ms 105648 KB Output is correct
11 Correct 587 ms 104132 KB Output is correct
12 Correct 624 ms 106312 KB Output is correct
13 Correct 117 ms 49840 KB Output is correct
14 Correct 141 ms 49732 KB Output is correct
15 Correct 118 ms 50376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 702 ms 101516 KB Output is correct
2 Correct 729 ms 102344 KB Output is correct
3 Correct 725 ms 102648 KB Output is correct
4 Correct 659 ms 99780 KB Output is correct
5 Correct 713 ms 100612 KB Output is correct
6 Correct 700 ms 99336 KB Output is correct
7 Correct 763 ms 99264 KB Output is correct
8 Correct 810 ms 101256 KB Output is correct
9 Correct 734 ms 99780 KB Output is correct
10 Correct 770 ms 102472 KB Output is correct
11 Correct 775 ms 99880 KB Output is correct
12 Correct 796 ms 98412 KB Output is correct
13 Correct 687 ms 101276 KB Output is correct
14 Correct 668 ms 98248 KB Output is correct
15 Correct 677 ms 99888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 754 ms 89148 KB Output is correct
2 Correct 777 ms 93008 KB Output is correct
3 Correct 747 ms 90436 KB Output is correct
4 Correct 884 ms 109560 KB Output is correct
5 Correct 709 ms 108144 KB Output is correct
6 Correct 747 ms 109932 KB Output is correct
7 Correct 174 ms 52940 KB Output is correct
8 Correct 130 ms 54128 KB Output is correct
9 Correct 120 ms 53044 KB Output is correct
10 Correct 686 ms 108408 KB Output is correct
11 Correct 668 ms 104916 KB Output is correct
12 Correct 681 ms 108668 KB Output is correct
13 Correct 133 ms 52940 KB Output is correct
14 Correct 129 ms 54540 KB Output is correct
15 Correct 119 ms 53344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 906 ms 91464 KB Output is correct
2 Correct 835 ms 90668 KB Output is correct
3 Correct 822 ms 88724 KB Output is correct
4 Correct 753 ms 100684 KB Output is correct
5 Correct 728 ms 103060 KB Output is correct
6 Correct 710 ms 103716 KB Output is correct
7 Correct 125 ms 49944 KB Output is correct
8 Correct 125 ms 50632 KB Output is correct
9 Correct 139 ms 51084 KB Output is correct
10 Correct 691 ms 99384 KB Output is correct
11 Correct 741 ms 103472 KB Output is correct
12 Correct 751 ms 103424 KB Output is correct
13 Correct 128 ms 50888 KB Output is correct
14 Correct 121 ms 49888 KB Output is correct
15 Correct 144 ms 50124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1884 KB Output is correct
2 Correct 6 ms 1628 KB Output is correct
3 Correct 7 ms 1884 KB Output is correct
4 Correct 5 ms 1884 KB Output is correct
5 Correct 5 ms 1884 KB Output is correct
6 Correct 5 ms 1884 KB Output is correct
7 Correct 2 ms 1368 KB Output is correct
8 Correct 2 ms 1384 KB Output is correct
9 Correct 2 ms 1372 KB Output is correct
10 Correct 7 ms 1884 KB Output is correct
11 Correct 5 ms 1884 KB Output is correct
12 Correct 5 ms 1884 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1372 KB Output is correct
15 Correct 4 ms 1368 KB Output is correct
16 Correct 680 ms 90420 KB Output is correct
17 Correct 747 ms 88376 KB Output is correct
18 Correct 709 ms 88300 KB Output is correct
19 Correct 601 ms 107256 KB Output is correct
20 Correct 591 ms 100108 KB Output is correct
21 Correct 602 ms 102708 KB Output is correct
22 Correct 111 ms 50892 KB Output is correct
23 Correct 123 ms 49440 KB Output is correct
24 Correct 145 ms 49612 KB Output is correct
25 Correct 619 ms 105648 KB Output is correct
26 Correct 587 ms 104132 KB Output is correct
27 Correct 624 ms 106312 KB Output is correct
28 Correct 117 ms 49840 KB Output is correct
29 Correct 141 ms 49732 KB Output is correct
30 Correct 118 ms 50376 KB Output is correct
31 Correct 702 ms 101516 KB Output is correct
32 Correct 729 ms 102344 KB Output is correct
33 Correct 725 ms 102648 KB Output is correct
34 Correct 659 ms 99780 KB Output is correct
35 Correct 713 ms 100612 KB Output is correct
36 Correct 700 ms 99336 KB Output is correct
37 Correct 763 ms 99264 KB Output is correct
38 Correct 810 ms 101256 KB Output is correct
39 Correct 734 ms 99780 KB Output is correct
40 Correct 770 ms 102472 KB Output is correct
41 Correct 775 ms 99880 KB Output is correct
42 Correct 796 ms 98412 KB Output is correct
43 Correct 687 ms 101276 KB Output is correct
44 Correct 668 ms 98248 KB Output is correct
45 Correct 677 ms 99888 KB Output is correct
46 Correct 754 ms 89148 KB Output is correct
47 Correct 777 ms 93008 KB Output is correct
48 Correct 747 ms 90436 KB Output is correct
49 Correct 884 ms 109560 KB Output is correct
50 Correct 709 ms 108144 KB Output is correct
51 Correct 747 ms 109932 KB Output is correct
52 Correct 174 ms 52940 KB Output is correct
53 Correct 130 ms 54128 KB Output is correct
54 Correct 120 ms 53044 KB Output is correct
55 Correct 686 ms 108408 KB Output is correct
56 Correct 668 ms 104916 KB Output is correct
57 Correct 681 ms 108668 KB Output is correct
58 Correct 133 ms 52940 KB Output is correct
59 Correct 129 ms 54540 KB Output is correct
60 Correct 119 ms 53344 KB Output is correct
61 Correct 906 ms 91464 KB Output is correct
62 Correct 835 ms 90668 KB Output is correct
63 Correct 822 ms 88724 KB Output is correct
64 Correct 753 ms 100684 KB Output is correct
65 Correct 728 ms 103060 KB Output is correct
66 Correct 710 ms 103716 KB Output is correct
67 Correct 125 ms 49944 KB Output is correct
68 Correct 125 ms 50632 KB Output is correct
69 Correct 139 ms 51084 KB Output is correct
70 Correct 691 ms 99384 KB Output is correct
71 Correct 741 ms 103472 KB Output is correct
72 Correct 751 ms 103424 KB Output is correct
73 Correct 128 ms 50888 KB Output is correct
74 Correct 121 ms 49888 KB Output is correct
75 Correct 144 ms 50124 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 804 ms 90228 KB Output is correct
80 Correct 855 ms 92832 KB Output is correct
81 Correct 825 ms 93616 KB Output is correct
82 Correct 683 ms 107080 KB Output is correct
83 Correct 732 ms 106096 KB Output is correct
84 Correct 728 ms 105804 KB Output is correct
85 Correct 150 ms 54312 KB Output is correct
86 Correct 122 ms 53752 KB Output is correct
87 Correct 132 ms 52596 KB Output is correct
88 Correct 675 ms 103944 KB Output is correct
89 Correct 689 ms 106000 KB Output is correct
90 Correct 679 ms 104892 KB Output is correct
91 Correct 132 ms 54476 KB Output is correct
92 Correct 125 ms 53196 KB Output is correct
93 Correct 128 ms 52680 KB Output is correct