QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626881#5148. Tree DistancedanielzTL 41ms63512kbC++205.6kb2024-10-10 13:42:062024-10-10 13:42:08

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 13:42:08]
  • 评测
  • 测评结果:TL
  • 用时:41ms
  • 内存:63512kb
  • [2024-10-10 13:42:06]
  • 提交

answer

#include "bits/stdc++.h"

using ll = long long;
const ll inf = 1e18;
using namespace std;
template <typename T>
void add(vector<T> &v, T x) { v.push_back(x); }
template <typename T, typename C>
void add(set<T, C> &v, T x) { v.insert(x); }
template <typename T>
void erase(vector<T> &v, T x) { v.erase(find(v.begin(), v.end(), x)); }
template <typename T, typename C>
void erase(set<T, C> &v, T x) { v.erase(x); }
using namespace std;
struct W {
  int v; ll w;
  friend istream& operator>>(istream& is, W &e) { return is >> e.v >> e.w; }
  friend ostream& operator<<(ostream& os, W &e) { return os << e.v << ":" << e.w; }
  bool operator==(const W o) const { return make_pair(v, w) == make_pair(o.v, o.w); }
  operator int() const { return v; }
  bool operator<(const W &o) const { return make_pair(v, w) < make_pair(o.v, o.w); }
  bool operator<(int x) const { return v < x; }
};
template <int N, typename T = int, class V = vector<T>>
struct Graph {
  int n, m;
  Graph() {}
  Graph(int n, int m) : n(n), m(m) {}
  V g[N];
  void add(int u, T e) { ::add(g[u], e); }
  void u(int u, int v) {
    if constexpr (is_same_v<T, int>) {
      add(u, v);
      add(v, u);
    }
  }
  Graph& input() {
    for (int i = 0; i < m; i++) {
      int u; T v; cin >> u >> v;
      ::add(g[u], v);
      if constexpr (is_same_v<T, W>) ::add(g[v], W{u, v.w});
      else ::add(g[v], u);
    }
    return *this;
  }
};
using namespace std;
template <int N, typename T = int>
struct ST {
  static constexpr int K = 33 - __builtin_clz(N);
  function<T(T, T)> f;
 T st[N + 1][K]{};
 void build(T a[N]) {
  for (int i = 0; i < N; i++) st[i][0] = a[i];
  for (int k = 1; k < K; k++) for (int i = 0; i < N; i++) {
   st[i][k] = st[i][k - 1];
   if (int j = i + (1 << (k - 1)); j < N) st[i][k] = f(st[i][k], st[j][k - 1]);
  }
 }
 T F(int l, int r) {
    assert(l < r);
    int k = log2(r - l);
    return f(st[l][k], st[r - (1 << k)][k]);
 }
};
template <int N, typename T = int, typename V = vector<T>>
struct Tree : Graph<N, T, V> {
  int r, s[N], p[N];
  ll d[N]{};
  Tree() {}
  Tree(int n) : Graph<N, T, V>(n, n - 1) {
    fill(s, s + n + 1, 1);
  }
  Tree& root(int x) { return r = p[x] = x, *this; }
  int I[N], o[2 * N], t = 0;
  ST<2 * N> st{[&](int x, int y) { return d[x] < d[y] ? x : y; }};
  int dfs(int x) {
    for (T e : this->g[x]) {
      o[I[x] = t++] = x;
      if (e == p[x]) continue;
      int w = 1;
      if constexpr (is_same_v<T, W>) w = e.w;
      d[e] = d[p[e] = x] + w;
      s[x] += dfs(e);
    }
    if (x == r) st.build(o);
    return s[x];
  }
  int lca(int u, int v) {
    if (I[u] > I[v]) swap(u, v);
    return st.F(I[u], I[v] + 1);
  }
  int D(int u, int v) {
    return d[u] + d[v] - 2 * d[lca(u, v)];
  }
  int cr, cp[N];
  vector<int> cg[N];
  int decompose(int x, int n) {
    for (int y : this->g[x]) {
      if (s[y] > n / 2) return decompose(y, n);
    }
    for(int y : this->g[x]) {
      W e = *this->g[y].find(x);
      this->g[y].erase(e);
      add(cg[cp[y] = x], decompose(y, s[y]));
      add(this->g[y], e);
    }
    return x;
  }
  Tree& decompose() { return cr = decompose(r, s[r]), *this; }
  Tree& dfs() { return dfs(r), *this; }
  Tree& input() { return Graph<N, T, V>::input(), *this; }
};
using namespace std;
template <int N, typename T>
struct Centroid {
  Tree<N, T, set<T>> &t;
  int r;
  vector<int> g[N];
  int decompose(int x, int n) {
    for (auto y : t.g[x]) {
      if (t.sz[y] > n / 2) return decompose(y, x, n);
    }
    for(int y : t.g[x]) {
      t.g[y].erase(x);
      g[x].push_back(decompose(y, x, t.sz[y]));
    }
    t.g[x].clear();
    return x;
  }
  Centroid& decompose() {
    return r = decompose(t.r, t.sz[t.r]), *this;
  }
};
using namespace std;
template <int N, typename T = int>
struct SGT {
  T a[2 * N], t0;
  function<T(T, T)> f;
  SGT& fn(function<T(T, T)> f, T x) { return this->f = f, t0 = x, *this; }
  SGT& fill(T x) { return ::fill(a, a + 2 * N, x), *this; }
  T query(int l, int r) {
    T tl = t0, tr = t0;
    for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
      if (l & 1) tl = f(tl, a[l++]);
      if (r & 1) tr = f(a[--r], tr);
    }
    return f(tl, tr);
  }
  void upd(int i, T x) {
    for (a[i += N] = x; i >>= 1; ) a[i] = f(a[i << 1], a[i << 1|1]);
  }
};
using namespace std;
const int N = 2e5 + 1;
struct P { int i; ll d;
  bool operator<(const P &o) const { return i < o.i; }
};
struct E { int u, v; ll w;
  bool operator<(const E &o) const {
    return v == o.v ? w > o.w : v < o.v;
  }
};
vector<P> v[N];
SGT<2 * N, ll> sgt;
ll r[N];
int main() {
  int n; cin >> n;
  Tree<N, W, set<W, less<>>> t(n);
  t.input().root(1).dfs().decompose();
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (j) {
      v[j].push_back({i, t.D(i, j)});
      j = t.cp[j];
    }
  }
  vector<E> e;
  for (int i = 1; i <= n; i++) {
    vector<P> st;
    for (auto [j, d] : v[i]) {
      while (st.size() && st.back().d >= d) {
        e.push_back({st.back().i, j, t.D(st.back().i, j)});
        st.pop_back();
      }
      if (st.size()) e.push_back({st.back().i, j, t.D(st.back().i, j)});
      st.push_back({j, d});
    }
  }
  sgt.fn([](ll x, ll y) { return min(x, y); }, inf).fill(inf);
  int q; cin >> q;
  for (int i = 0; i < q; i++) {
    int u, v; cin >> u >> v;
    e.push_back({u, v, -i});
  }
  sort(e.begin(), e.end());
  for (auto [u, v, w] : e) {
    if (w < 1) r[-w] = sgt.query(u, n + 1);
    else sgt.upd(u, w);
  }
  for (int i = 0; i < q; i++) {
    cout << (r[i] < inf ? r[i] : -1) << endl;
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 41ms
memory: 63512kb

input:

5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5

output:

-1
3
7
7
2

result:

ok 5 number(s): "-1 3 7 7 2"

Test #2:

score: -100
Time Limit Exceeded

input:

199999
31581 23211 322548833
176307 196803 690953895
34430 82902 340232856
36716 77480 466375266
7512 88480 197594480
95680 61864 679567992
19572 14126 599247796
188006 110716 817477802
160165 184035 722372640
23173 188594 490365246
54801 56250 304741654
10103 45884 643490340
127469 154479 214399361...

output:


result: