QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#395416#8235. Top ClusterqawszxWA 2965ms1015388kbC++144.3kb2024-04-21 14:18:572024-04-21 14:18:58

Judging History

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

  • [2024-04-21 14:18:58]
  • 评测
  • 测评结果:WA
  • 用时:2965ms
  • 内存:1015388kb
  • [2024-04-21 14:18:57]
  • 提交

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;
namespace fastInput {
  const int BUF_SIZE = 1 << 23;
  char buf[BUF_SIZE], *p, *end;
  char getc() {
    if (p == end) {
      p = buf, end = buf + fread(buf, 1, BUF_SIZE, stdin);
      if (p == end) return EOF;
    }
    return *p++;
  }
  int scan() {
    int x = 0; char ch = getc();
    for (; !isdigit(ch); ch = getc());
    for (; isdigit(ch); ch = getc()) x = 10 * x + ch - '0';
    return x;
  }
} using fastInput::scan;
namespace fw {
  const int S = 1 << 23;
  int cnt; char B[S + 3];
  inline void print(int x) {
    if (x > 9) print(x / 10);
    B[cnt++] = (x % 10) | 48;
    if (cnt == S) fwrite(B, 1, S, stdout), cnt = 0;
  }
  inline void print(unsigned long long x) {
    if (x > 9) print(x / 10);
    B[cnt++] = (x % 10) | 48;
    if (cnt == S) fwrite(B, 1, S, stdout), cnt = 0;
  }
  inline void print(char ch) {
    B[cnt++] = ch;
    if (cnt == S) fwrite(B, 1, S, stdout), cnt = 0;
  }
  inline void flush() {
    fwrite(B, 1, cnt, stdout), cnt = 0;
  }
} using fw::print; using fw::flush;
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<long long> arrDis[N];
std::vector<pair<int, int>> res[N];
__gnu_pbds::gp_hash_table<int, int> dir[N];
__gnu_pbds::gp_hash_table<int, long long> dis[N];
long long tmpDis[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] = tmpDis[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);
  int sz = vec.size();
  std::sort(vec.begin(), vec.end(),
    [&](int x, int y) { return tmpDis[x] < tmpDis[y]; });
  arrDis[rot].resize(sz);
  for (int i = 0; i < sz; ++i) arrDis[rot][i] = tmpDis[vec[i]];
  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);
  }
  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];
	    if (arrDis[u][sz - 1] <= k) l = sz;
	    if (arrDis[u][0] > k) r = 0;
	    while (l < r) {
	      int mid = (l + r) >> 1;
	      if (arrDis[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), print('\n');
  }
  flush();
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 90ms
memory: 313520kb

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: 0
Accepted
time: 2348ms
memory: 1014020kb

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:

ok 500000 numbers

Test #3:

score: -100
Wrong Answer
time: 2965ms
memory: 1015388kb

input:

500000 500000
416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 2nd numbers differ - expected: '250002', found: '0'