QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#604950 | #8235. Top Cluster | DBsoleil# | WA | 2146ms | 302412kb | C++20 | 2.9kb | 2024-10-02 14:44:38 | 2024-10-02 14:44:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
static constexpr int Maxn = 5e5 + 5;
static constexpr int LogN = 20;
int n, qn, ans[Maxn], mans;
int a[Maxn], ia[Maxn];
vector<pair<int, int64_t>> g[Maxn];
vector<pair<int64_t, int>> q[Maxn];
vector<tuple<int64_t, int, int>> cq[Maxn];
namespace ndt {
int anc[Maxn], layer[Maxn];
int64_t cdep[LogN][Maxn];
#define foreach(u, v, w) \
for (const auto &[v, w]: g[u]) if (v != fa && !visited[v])
bool visited[Maxn]; int totN, mnN, root, siz[Maxn];
vector<int> all[LogN];
int calcsz(int u, int fa) { int r = 1; foreach(u, v, w) r += calcsz(v, u); return r; }
void rooting(int u, int fa) {
int mx = 0; siz[u] = 1;
foreach(u, v, w) rooting(v, u), siz[u] += siz[v], mx = max(mx, siz[v]);
if ((mx = max(mx, totN - siz[u])) < mnN) mnN = mx, root = u;
} // ndt::rooting
int get_root(int u) { mnN = totN = calcsz(u, 0), root = 0; return rooting(u, 0), root; }
void subtree(int u, int fa, int64_t depth, int layer) {
cdep[layer][u] = depth; all[layer].push_back(u);
foreach(u, v, w) subtree(v, u, depth + w, layer);
} // ndt::subtree
void divide(int u, int fa = 0, int lay = 1) {
visited[u] = true, anc[u] = fa, subtree(u, 0, 0, layer[u] = lay);
foreach(u, v, w) divide(get_root(v), u, lay + 1);
} // ndt::divide
void build(void) { divide(get_root(1)); }
} // namespace ndt
void upgrade_query(int u) {
using namespace ndt;
for (int x = u; x; x = anc[x])
for (const auto &[tw, ti]: q[u]) cq[x].emplace_back(tw, u, ti);
} // upgrade_query
void update(int u) {
using namespace ndt;
for (int x = u; x; x = anc[x]) {
int lay = layer[x]; int64_t wd = cdep[lay][u];
while (!cq[x].empty()) {
auto [w, v, i] = cq[x].back();
if (ans[i] != -1) { cq[x].pop_back(); continue; }
if (wd + cdep[lay][v] > w) { ans[i] = a[u]; cq[x].pop_back(); continue; }
else break;
}
}
} // update
int main(void) {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> qn; memset(ia, -1, sizeof(ia));
memset(ans, -1, sizeof(ans));
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) if (a[i] <= n) ia[a[i]] = i;
for (mans = 0; ia[mans] != -1; ++mans);
auto add_edge = [&](int u, int v, int64_t w)->void {
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}; // main::add_edge
for (int i = 1, u, v, w; i < n; ++i)
cin >> u >> v >> w, add_edge(u, v, w);
for (int i = 1, u; i <= qn; ++i) {
int64_t w; cin >> u >> w, q[u].emplace_back(w, i);
} ndt::build();
for (int i = 1; i <= n; ++i) upgrade_query(i);
for (int i = 1; i <= n; ++i) sort(cq[i].rbegin(), cq[i].rend());
for (int c = 0; c < mans; ++c) update(ia[c]);
for (int i = 1; i <= qn; ++i)
if (ans[i] == -1) ans[i] = mans;
for (int i = 1; i <= qn; ++i) printf("%d\n", ans[i]);
return 0;
} // main
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 20932kb
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: 2146ms
memory: 302412kb
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: '156'