QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#629541#5148. Tree DistancedanielzWA 2651ms368788kbC++205.5kb2024-10-11 13:03:012024-10-11 13:03:01

Judging History

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

  • [2024-10-11 13:03:01]
  • 评测
  • 测评结果:WA
  • 用时:2651ms
  • 内存:368788kb
  • [2024-10-11 13:03:01]
  • 提交

answer

#include "bits/stdc++.h"

using ll = long long;
const ll inf = 1e18;
using namespace std;
struct W {
  long long 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 long long() const { return v; }
  bool operator<(const W &o) const { return make_pair(v, w) < make_pair(o.v, o.w); }
  bool operator<(long long x) const { return v < x; }
};
template <long long N, typename T = long long>
struct Graph {
  long long n, m;
  Graph() {}
  Graph(long long n, long long m) : n(n), m(m) {}
  Graph& init(long long n, long long m) { return this->n = n, this->m = m, *this; }
  vector<T> g[N];
  void add(long long u, T e) { g[u].push_back(e); }
  void u(long long u, long long v) {
    if constexpr (is_same_v<T, long long>) {
      add(u, v);
      add(v, u);
    }
  }
  Graph& input() {
    for (long long i = 0; i < m; i++) {
      long long u; T v; cin >> u >> v;
      g[u].push_back(v);
      if constexpr (is_same_v<T, W>) g[v].push_back({u, v.w});
      else g[v].push_back(u);
    }
    return *this;
  }
};
using namespace std;
constexpr long long lg(long long x) {
  return 31 - __builtin_clz(x);
}
template <long long N, typename T = long long>
struct ST {
  static constexpr long long K = lg(N) + 1;
  function<T(T, T)> f;
 T st[N + 1][K]{};
 void build(T a[N]) {
  for (long long i = 0; i < N; i++) st[i][0] = a[i];
  for (long long k = 1; k < K; k++) for (long long i = 0; i < N; i++) {
   st[i][k] = st[i][k - 1];
   if (long long j = i + (1 << (k - 1)); j < N) st[i][k] = f(st[i][k], st[j][k - 1]);
  }
 }
 T F(long long l, long long r) {
    assert(l < r);
    long long k = lg(r - l);
    return f(st[l][k], st[r - (1 << k)][k]);
 }
};
template <long long N, typename T = long long>
struct Tree : Graph<N, T> {
  long long r, s[N], p[N];
  ll d[N]{};
  Tree() {}
  Tree& init(long long n) {
    Graph<N, T>::init(n, n - 1);
    fill(s, s + n + 1, 1);
    return *this;
  }
  Tree& root(long long x) { return r = p[x] = x, *this; }
  long long I[N], o[2 * N]{}, t = 0;
  ST<2 * N> st{[&](long long x, long long y) { return d[x] < d[y] ? x : y; }};
  long long dfs(long long x) {
    for (T e : this->g[x]) {
      o[I[x] = t++] = x;
      if (e == p[x]) continue;
      ll 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];
  }
  long long lca(long long u, long long v) {
    if (I[u] > I[v]) swap(u, v);
    return st.F(I[u], I[v] + 1);
  }
  ll D(long long u, long long v) {
    return d[u] + d[v] - 2 * d[lca(u, v)];
  }
  long long cr, cp[N]{}, rem[N]{};
  vector<long long> cg[N];
  long long sz(long long x, long long p = -1) {
    s[x] = 0;
    for (long long y : this->g[x]) if (!rem[y] && y != p) s[x] += sz(y, x);
    return ++s[x];
  }
  long long decompose(long long x, long long n, long long p = -1) {
    for (long long y : this->g[x]) if (!rem[y] && y != p) {
      if (s[y] > n / 2) return decompose(y, n, x);
    }
    rem[x] = true;
    for(long long y : this->g[x]) if (!rem[y]) {
      y = decompose(y, sz(y));
      cg[cp[y] = x].push_back(y);
    }
    return x;
  }
  Tree& decompose() { return cr = decompose(r, sz(r)), *this; }
  Tree& dfs() { return dfs(r), *this; }
  Tree& input() { return Graph<N, T>::input(), *this; }
};
using namespace std;
template <long long N, typename T = long long>
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(long long l, long long 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(long long i, T x) {
    for (a[i += N] = x; i >>= 1; ) a[i] = f(a[i << 1], a[i << 1|1]);
  }
};
using namespace std;
const long long N = 2e5 + 1;
struct P { long long i; ll d;
  bool operator<(const P &o) const { return i < o.i; }
};
struct E { long long 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[10 * N];
Tree<N, W> t;
signed main() {
  long long n; cin >> n;
  t.init(n).input().root(1).dfs().decompose();
  for (long long i = 1; i <= n; i++) {
    long long j = i;
    while (j) {
      v[j].push_back({i, t.D(i, j)});
      j = t.cp[j];
    }
  }
  vector<E> e;
  for (long long 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);
  long long q; cin >> q;
  for (long long i = 0; i < q; i++) {
    long long 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 (long long i = 0; i < q; i++) {
    cout << (r[i] < inf ? r[i] : -1) << endl;
  }
}

详细

Test #1:

score: 100
Accepted
time: 126ms
memory: 91236kb

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
Wrong Answer
time: 2651ms
memory: 368788kb

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:

29573323
1178569098929
4088
65959
9935
15508270844
760172089
4867978
328055210
55721881562
2062364707
339719
287433
287433
9935
138216269
8212187
9404444928
4453926
9935
710854
114886
65959
1818252547
92126
91087
9277701023
26776689
5718199
710854
92126
6129659427
365255209
92126
710854
287433
90165...

result:

wrong answer 5th numbers differ - expected: '4366', found: '9935'