QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#395414 | #8235. Top Cluster | qawszx | WA | 88ms | 313688kb | C++14 | 4.3kb | 2024-04-21 14:18:22 | 2024-04-21 14:18:23 |
Judging History
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), putchar('\n');
}
flush();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 88ms
memory: 313688kb
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:
1034
result:
wrong answer 1st numbers differ - expected: '1', found: '1034'