QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#383399 | #8235. Top Cluster | 24bot | WA | 590ms | 122976kb | C++14 | 2.7kb | 2024-04-09 12:39:17 | 2024-04-09 12:39:17 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
namespace lyre {
void main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
auto Lg = __lg(n);
vector<int> w(n), dfn(n);
vector<int> parent(n);
vector<long> dep(n);
vector<vector<pair<int, int>>> st(Lg + 1, vector<pair<int, int>>(n));
vector<vector<pair<int, int>>> G(n);
for (int i = 0; i < n; ++i)
cin >> w[i];
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
auto index = 0;
auto dfs = [&] (auto self, int x, int o) -> void {
parent[x] = o;
dfn[x] = index++;
for (auto [y, w] : G[x]) {
if (y == o) continue;
dep[y] = dep[x] + w;
self(self, y, x);
}
};
dfs(dfs, 0, -1);
for (int i = 0; i < n; ++i)
st[0][dfn[i]] = make_pair(dfn[i], dfn[parent[i]]);
for (int i = 0; i < Lg; ++i) {
for (int j = 0; j + (1 << (i + 1)) - 1 < n; ++j)
st[i + 1][j] = min(st[i][j], st[i][j + (1 << i)]);
}
auto lca = [&] (int x, int y) -> int {
if (x == y) return x;
x = dfn[x], y = dfn[y];
if (x > y) swap(x, y);
auto k = __lg(y - x++);
return min(st[k][x], st[k][y - (1 << k) + 1]).second;
};
auto dis = [&] (int x, int y) -> long {
return dep[x] + dep[y] - 2 * dep[lca(x, y)];
};
vector<int> ord(n);
vector<pair<int, int>> g(n);
iota(ord.begin(), ord.end(), 0);
auto comp = [&] (int x, int y) -> bool {
return w[x] < w[y];
};
sort(ord.begin(), ord.end(), comp);
auto hi = 0;
for (; hi < n && w[ord[hi]] == hi; ++hi);
auto p = ord[0], q = ord[0];
g[0] = make_pair(p, q);
for (int i = 1; i < hi; ++i) {
auto x = ord[i];
if (dis(x, p) > dis(x, q)) {
if (dis(x, p) > dis(p, q)) q = x;
} else {
if (dis(x, q) > dis(p, q)) p = x;
}
g[i] = make_pair(p, q);
}
auto chk = [&] (int v, int x, long k) -> bool {
if (v >= hi) return false;
if (dis(x, g[v].first) > k) return false;
if (dis(x, g[v].second) > k) return false;
return true;
};
auto query = [&] (int x, long k) -> int {
if (!chk(0, x, k)) return 0;
auto l(0), r(hi - 1);
while (l < r) {
auto mid = (l + r + 1) / 2;
if (chk(mid, x, k)) l = mid;
else r = mid - 1;
}
return l + 1;
};
assert(hi);
for (int i = 0; i < m; ++i) {
int x; long k;
cin >> x >> k;
cout << query(--x, k) << '\n';
}
}
}
int main() { lyre::main(); }
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
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: 590ms
memory: 122976kb
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 14879th numbers differ - expected: '0', found: '249999'