QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606882#8235. Top ClusterDBsoleilWA 531ms141544kbC++202.7kb2024-10-03 12:51:042024-10-03 12:51:05

Judging History

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

  • [2024-10-03 12:51:05]
  • 评测
  • 测评结果:WA
  • 用时:531ms
  • 内存:141544kb
  • [2024-10-03 12:51:04]
  • 提交

answer


#include <bits/stdc++.h>
using namespace std;
static constexpr int Maxn = 5e5 + 5, LogN = 19;
int n, qn, na, a[Maxn], ia[Maxn];
vector<tuple<int, int64_t>> g[Maxn];
int par[Maxn], dep[Maxn];
int edfn[Maxn], edn, iedfn[Maxn * 2];
int64_t wdep[Maxn];
void dfs(int u, int fa, int depth, int64_t wdepth) {
  par[u] = fa, dep[u] = depth, wdep[u] = wdepth;
  edfn[u] = ++edn, iedfn[edn] = u;
  for (const auto &[v, w]: g[u]) if (v != fa)
    dfs(v, u, depth + 1, wdepth + w), iedfn[++edn] = u;
} // dfs
int spt[LogN + 1][Maxn * 2], lowbit[Maxn * 2];
int min_dep(int x, int y) { return dep[iedfn[x]] < dep[iedfn[y]] ? x : y; }
int lca(int x, int y) {
  if (x == y) return x;
  x = edfn[x], y = edfn[y];
  if (x > y) swap(x, y);
  int k = lowbit[y - x + 1];
  return iedfn[min_dep(spt[k][x], spt[k][y - (1 << k) + 1])];
} // lca
int64_t distance(int x, int y) {
  int z = lca(x, y);
  return wdep[x] + wdep[y] - 2 * wdep[z];
} // distance
struct diam {
  int x, y;
  diam() = default;
  diam(int x, int y) : x(x), y(y) { }
  friend bool operator < (const diam &lhs, const diam &rhs)
  { return distance(lhs.x, lhs.y) < distance(rhs.x, rhs.y); }
  friend bool operator > (const diam &lhs, const diam &rhs)
  { return distance(lhs.x, lhs.y) > distance(rhs.x, rhs.y); }
  friend diam operator + (const diam &lhs, const diam &rhs)
  { return max({lhs, rhs, diam(lhs.x, rhs.x), diam(lhs.x, rhs.y), diam(lhs.y, rhs.x), diam(lhs.y, rhs.y)}); }
} d[Maxn];
int main(void) {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> n >> qn; memset(ia, -1, sizeof(ia));
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) if (a[i] <= n) ia[a[i]] = i;
  for (na = 0; ia[na] != -1; ++na);
  for (int i = 1; i < n; ++i) {
    int u, v; int64_t w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs(1, 0, 1, 0LL);
  for (int i = 2; i <= edn; ++i) lowbit[i] = lowbit[i >> 1] + 1;
  for (int i = 1; i <= edn; ++i) spt[0][i] = i;
  for (int j = 1; j < LogN; ++j)
    for (int i = 1; i + (1 << j) - 1 <= edn; ++i)
      spt[j][i] = min_dep(spt[j - 1][i], spt[j - 1][i + (1 << j - 1)]);
  for (int i = 0; i < na; ++i)
    d[i] = (i == 0 ? diam(ia[0], ia[0]) : d[i - 1] + diam(ia[i], ia[i]));
  for (int qi = 1; qi <= qn; ++qi) {
    int u; int64_t w;
    cin >> u >> w;
    auto check = [&](int x)->bool {
      int64_t w1 = distance(d[x].x, u);
      int64_t w2 = distance(d[x].y, u);
      return max(w1, w2) > w;
    }; // main::check
    int low = 0, high = na - 1, ans = na;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (check(mid)) ans = mid, high = mid - 1;
      else low = mid + 1;
    }
    printf("%d\n", ans);
  }
  return 0;
} // main

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 22224kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: -100
Wrong Answer
time: 531ms
memory: 141544kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

wrong answer 36936th numbers differ - expected: '14', found: '4'