QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#395353#8235. Top Clusterqawszx#TL 11ms74724kbC++143.3kb2024-04-21 13:38:512024-04-21 13:38:52

Judging History

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

  • [2024-04-21 13:38:52]
  • 评测
  • 测评结果:TL
  • 用时:11ms
  • 内存:74724kb
  • [2024-04-21 13:38:51]
  • 提交

answer

#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
//using namespace __gnu_pbds;
using std::max; using std::min; using std::pair;
long long scan() {
  long long x = 0; char ch = getchar();
  for (; !isdigit(ch); ch = getchar());
  for (; isdigit(ch); ch = getchar()) x = 10 * x + ch - '0';
  return x;
}
int top, stk[11];
void print(int x) {
  do stk[++top] = x % 10, x /= 10; while (x);
  while (top) putchar(stk[top--] + '0');
}
const int N = 5e5 + 5;
int n, Q, id[N], ansmx,
    tot, head[N], nxt[2 * N], ver[2 * N], wei[2 * N],
    Fa[N], fullSz, sz[N], msz[N];
bool idv[N], vis[N];
std::vector<int> vec, arr[N];
std::vector<pair<int, int>> res[N];
std::map<int, int> dir[N];
std::map<int, long long> dis[N];
void addEdge(int u, int v, int w) {
  nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, wei[tot] = w;
}
void prepare(int u, int fa) {
  ++fullSz, sz[u] = 1, msz[u] = 0, vec.emplace_back(u);
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i]; if (vis[v] || v == fa) continue;
    prepare(v, u), sz[u] += sz[v], msz[u] = max(msz[u], sz[v]);
  }
}
void dfs(int u, int fa, long long d, int rot) {
  dis[rot][u] = d;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i]; if (vis[v] || v == fa) continue;
    dfs(v, u, d + wei[i], rot);
  }
}
void construct(int divFa) {
  int rot = 0;
  for (int v: vec) {
    msz[v] = max(msz[v], fullSz - sz[v]);
    if (!rot || msz[v] < msz[rot]) rot = v;
  }
  dfs(rot, 0, 0, rot);
  std::sort(vec.begin(), vec.end(),
    [&](int x, int y) { return dis[rot][x] < dis[rot][y]; });
  vis[rot] = true, Fa[rot] = divFa, arr[rot] = vec;
  for (int i = head[rot]; i; i = nxt[i]) {
    int u = ver[i]; if (vis[u]) continue;
    fullSz = 0, vec.clear(), prepare(u, 0);
    for (int v: vec) dir[rot][v] = u;
    construct(rot);
  }
  int sz = arr[rot].size(); res[rot].resize(sz);
  int mn = 0, cmn = 0;
  for (int i = sz - 1; i >= 0; --i) {
    int v = arr[rot][i];
    if (!mn) mn = v;
    else {
      if (dir[rot][mn] == dir[rot][v]) {
        if (id[v] < id[mn]) mn = v;
      }
      else if (dir[rot][cmn] == dir[rot][v]) {
        if (id[v] < id[cmn]) cmn = v;
      }
      else {
        if (id[v] < id[mn]) cmn = mn, mn = v;
        else if (id[v] < id[cmn]) cmn = v;
      }
      if (id[mn] > id[cmn]) std::swap(mn, cmn);
    }
    res[rot][i] = {mn, cmn};
  }
}
int main() {
  n = scan(), Q = scan();
  id[0] = INT_MAX;
  for (int i = 1; i <= n; ++i) {
    id[i] = scan(); if (id[i] < N) idv[id[i]] = true;
  }
  for (int i = 0; i < N; ++i) if (!idv[i]) { ansmx = i; break; }
  for (int i = 1; i < n; ++i) {
    int u = scan(), v = scan(), w = scan();
    addEdge(u, v, w), addEdge(v, u, w);
  }
  prepare(1, 0), construct(0);
  while (Q--) {
    int ask = scan(), ans = ansmx; long long d = scan();
    for (int u = ask; u; u = Fa[u]) {
	    int sz = arr[u].size(), l = 0, r = sz;
	    long long k = d - dis[u][ask];
	    while (l < r) {
	      int mid = (l + r) >> 1;
	      if (dis[u][arr[u][mid]] <= k) l = mid + 1;
	      else r = mid;
      }
      if (l < sz) {
        pair<int, int> it = res[u][l];
        if (dir[u][it.first] == dir[u][ask]) ans = min(ans, id[it.second]);
        else ans = min(ans, id[it.first]);
      }
    }
    print(ans), putchar('\n');
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 74724kb

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
Time Limit Exceeded

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:


result: