QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#623547 | #8235. Top Cluster | fosov | RE | 4ms | 63780kb | C++14 | 2.8kb | 2024-10-09 13:00:04 | 2024-10-09 13:00:10 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define N 500010
#define lg 20
vector<pair<int, int>> G[N];
pair<int, int> ord[N], dia[N];
ll f[lg][N], dis[N], dep[N];
void add_edge(int u, int v, int l) {
G[u].emplace_back(v, l);
G[v].emplace_back(u, l);
}
void dfs(int u, int fa, ll pd, int d) {
f[0][u] = fa;
dis[u] = pd;
dep[u] = d;
for (auto [v, l] : G[u]) {
if (v == fa) continue;
dfs(v, u, pd+l, d+1);
}
}
void init(int n) {
for (int i = 1; i < lg; ++ i) {
for (int j = 1; j <= n; ++ j) {
f[i][j] = f[i-1][f[i-1][j]];
}
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for (int i = lg-1; i >= 0; -- i) if ((d>>i)&1) u = f[i][u];
for (int i = lg-1; i >= 0; -- i) {
if (f[i][u] != f[i][v]) {
u = f[i][u];
v = f[i][v];
}
}
return u == v ? u : f[0][u];
}
ll distance(int u, int v) {
int r = lca(u, v);
return dis[u] + dis[v] - 2ll * dis[r];
}
int main() {
#ifdef TEST
freopen("zz.in", "r+", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q; cin >> n >> q;
int dw = -1;
set<int> wset;
for (int i = 1; i <= n; ++ i) {
int w; cin >> w;
wset.insert(w);
ord[i] = make_pair(w, i);
}
for (int i = 0; i <= n; ++ i) {
if (wset.find(i) == wset.end()) {
dw = i;
break;
}
}
assert(dw != -1);
sort(ord+1, ord+1+n);
dia[1] = make_pair(ord[1].second, ord[1].second);
for (int i = 0; i < n-1; ++ i) {
int u, v, l; cin >> u >> v >> l;
add_edge(u, v, l);
}
dfs(1, 1, 0, 0);
init(n);
for (int i = 2; i <= n; ++ i) {
auto [pn0, pn1] = dia[i-1];
auto [_, cn] = ord[i];
ll d0 = distance(pn0, pn1);
ll d1 = distance(pn0, cn);
ll d2 = distance(pn1, cn);
dia[i] = dia[i-1];
if (d0 < d1) dia[i] = make_pair(pn0, cn), d0 = d1;
if (d0 < d2) dia[i] = make_pair(pn1, cn), d0 = d2;
}
auto in_range = [&](int r, int x, ll k) {
auto [n0, n1] = dia[r];
return max(distance(n0, x), distance(n1, x)) <= k;
};
while (q --) {
ll x, k; cin >> x >> k;
int l = 1, r = n+1;
while (l < r) {
int m = (l + r) / 2;
if (in_range(m, x, k)) {
l = m+1;
} else {
r = m;
}
}
assert(!in_range(l, x, k));
cout << min(dw, ord[l].first) << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 63780kb
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
Runtime Error
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...