QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#633579 | #8235. Top Cluster | LateRegistration# | WA | 629ms | 232692kb | C++17 | 2.9kb | 2024-10-12 15:46:10 | 2024-10-12 15:46:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class comp = less<>> struct rmq {
vector<vector<ll>> st;
rmq(const vector<ll> &arr) : st(__lg(arr.size()) + 1) {
st[0] = arr;
for (int k = 1; k < st.size(); k++) {
st[k] = st[k - 1];
for (int i = 0; i + (1 << k) <= st[k].size(); i++) {
ll x = st[k - 1][i + (1 << (k - 1))];
st[k][i] = comp()(st[k][i], x) ? st[k][i] : x;
}
}
}
ll query(int l, int r) {
int k = __lg(r - l + 1);
ll x = st[k][l], y = st[k][r - (1 << k) + 1];
return comp()(x, y) ? x : y;
}
};
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
map<int, int> mp;
for (int i = 0; i < n; i++) {
int w;
cin >> w;
mp[w] = i;
}
vector<int> in(n), out(n);
vector<ll> seq;
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
vector<ll> dep(n);
int tim = 0;
auto dfs = [&](auto self, int u, int fa) -> void {
seq.push_back(dep[u]);
in[u] = tim++;
for (auto [v, w] : adj[u]) {
if (v != fa) {
dep[v] = dep[u] + w;
self(self, v, u);
}
}
seq.push_back(dep[u]);
out[u] = tim++;
};
dfs(dfs, 0, -1);
rmq<> rmq_seq(seq);
auto dist = [&](int u, int v) {
if (in[u] > in[v]) {
swap(u, v);
}
return dep[u] + dep[v] - 2 * rmq_seq.query(in[u], in[v]);
};
vector<pair<int, int>> diam;
int u, v;
ll tmp;
for (int i = 0; i < n; i++) {
if (mp.count(i)) {
int w = mp[i];
if (i == 0) {
u = v = w;
tmp = 0;
} else {
ll foo = dist(u, w), bar = dist(v, w);
if (foo >= tmp && foo >= bar) {
tmp = foo;
v = w;
} else if (bar >= tmp && bar >= foo) {
tmp = bar;
u = w;
}
}
diam.emplace_back(u, v);
} else {
break;
}
}
while (q--) {
int x;
ll k;
cin >> x >> k;
x--;
int l = 0, r = (int)diam.size() - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
auto [u, v] = diam[mid];
if (max(dist(x, u), dist(x, v)) <= k) {
l = (ans = mid) + 1;
} else {
r = mid - 1;
}
}
cout << ans + 1 << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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: 629ms
memory: 232692kb
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 20939th numbers differ - expected: '4', found: '8'