QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#623584 | #8235. Top Cluster | fosov | WA | 1158ms | 113760kb | C++14 | 2.9kb | 2024-10-09 13:17:34 | 2024-10-09 13:17:34 |
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];
int st[lg][N], dep[N], seq[N<<1], fr[N], z;
ll dis[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) {
fr[u] = z;
st[0][z ++] = u;
dis[u] = pd;
dep[u] = d;
for (auto [v, l] : G[u]) {
if (v == fa) continue;
dfs(v, u, pd+l, d+1);
st[0][z ++] = u;
}
}
void init() {
for (int i = 1; i < lg; ++ i) {
for (int j = 0; j + (1 << i) - 1 < z; ++ j) {
int l = j, r = j + (1 << (i-1));
st[i][j] = dep[st[i-1][l]] < dep[st[i-1][r]] ? st[i-1][l] : st[i-1][r];
}
}
}
// use euler sequence to achieve O(1)
int lca(int u, int v) {
if (fr[u] > fr[v]) swap(u, v);
int lgn = ceil(log2(fr[v] - fr[u] + 1));
int l = fr[u], r = fr[v] - (1 << lgn) + 1;
return dep[st[lgn][l]] < dep[st[lgn][r]] ? st[lgn][l] : st[lgn][r];
}
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();
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));
int res = l == n+1 ? dw : min(dw, ord[l].first);
cout << res << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 31424kb
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: 1158ms
memory: 113760kb
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: '0'