QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#395243 | #8235. Top Cluster | qawszx# | TL | 8ms | 78716kb | C++14 | 3.8kb | 2024-04-21 11:33:19 | 2024-04-21 11:33:19 |
Judging History
answer
#include <bits/stdc++.h>
long long scan() {
long long x = 0; char ch = getchar();
for (; !isdigit(ch); ch = getchar());
for (; isdigit(ch); ch = getchar()) x = 10 * x + ch - '0';
return x;
}
int top, stk[11];
void print(int x) {
do stk[++top] = x % 10, x /= 10; while (x);
while (top) putchar(stk[top--] + '0');
}
const int N = 5e5 + 5;
int n, Q, id[N], ansmx, Fa[N], ans[N];
bool idv[N];
std::vector<std::pair<int, int>> ver[N];
int eulerc, euler[2 * N], apr[N], dep[N]; long long dis[N];
void getEuler(int u, int fa) {
euler[++eulerc] = u, apr[u] = eulerc;
for (auto it: ver[u]) {
int v = it.first, w = it.second;
if (v == fa) continue;
dep[v] = dep[u] + 1, dis[v] = dis[u] + w, getEuler(v, u), euler[++eulerc] = u;
}
}
int lg2[2 * N]; std::pair<int, int> st[2 * N][19];
void initST() {
for (int i = 2; i <= eulerc; ++i) lg2[i] = lg2[i >> 1] + 1;
for (int i = 1; i <= eulerc; ++i)
st[i][0] = std::make_pair(dep[euler[i]], euler[i]);
for (int j = 1; j < 19; ++j)
for (int i = 1; i + (1 << j) - 1 <= eulerc; ++i)
st[i][j] = std::min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
std::pair<int, int> getMin(int l, int r) {
if (l > r) std::swap(l, r);
int x = lg2[r - l + 1];
return std::min(st[l][x], st[r - (1 << x) + 1][x]);
}
long long getDis(int u, int v) {
return dis[u] + dis[v] - 2 * dis[getMin(apr[u], apr[v]).second];
}
int fullSz, sz[N], msz[N];
bool vis[N]; std::vector<int> vec;
void prepare(int u, int fa) {
++fullSz, sz[u] = 1, msz[u] = 0; vec.push_back(u);
for (auto it: ver[u]) {
int v = it.first;
if (vis[v] || v == fa) continue;
prepare(v, u); sz[u] += sz[v], msz[u] = std::max(msz[u], sz[v]);
}
}
int divFa[N];
std::map<int, int> whr[N];
std::vector<int> arr[N];
std::vector<std::pair<int, int>> res[N];
void construct(int fa) {
int u = 0;
for (auto v: vec) {
msz[v] = std::max(msz[v], fullSz - sz[v]);
if (!u || msz[v] < msz[u]) u = v;
}
vis[u] = true, divFa[u] = fa;
arr[u] = vec;
for (auto it: ver[u]) {
int v = it.first;
if (vis[v]) continue;
fullSz = 0; vec.clear();
prepare(v, 0);
for (auto x: vec) whr[u][x] = v;
construct(u);
}
std::sort(arr[u].begin(), arr[u].end(),
[&](int x, int y) { return getDis(x, u) < getDis(y, u); });
res[u].resize(arr[u].size());
int sz = arr[u].size();
int mn = 0, cmn = 0;
for (int i = sz - 1; i >= 0; --i) {
int v = arr[u][i];
if (mn == 0) mn = v;
else {
if (whr[u][mn] == whr[u][v]) {
if (id[v] < id[mn]) mn = v;
}
else if (whr[u][cmn] == whr[u][v]) {
if (id[v] < id[cmn]) cmn = v;
}
else {
if (id[v] < id[mn]) cmn = mn, mn = v;
else if (id[v] < id[cmn]) cmn = v;
}
if (id[mn] > id[cmn]) std::swap(mn, cmn);
}
res[u][i] = std::make_pair(mn, cmn);
}
}
int main() {
n = scan(), Q = scan();
id[0] = 1e9 + 9;
for (int i = 1; i <= n; ++i) {
id[i] = scan(); if (id[i] < N) idv[id[i]] = true;
}
for (int i = 0; i < N; ++i) if (!idv[i]) { ansmx = i; break; }
for (int i = 1; i < n; ++i) {
int u = scan(), v = scan(), w = scan();
ver[u].emplace_back(v, w), ver[v].emplace_back(u, w);
}
getEuler(1, 0); initST(); prepare(1, 0); construct(0);
while (Q--) {
int u = scan(), ask = u; long long d = scan(); int ans = ansmx;
while (u) {
int sz = arr[u].size();
int l = 0, r = sz;
long long k = d - getDis(ask, u);
while (l < r) {
int mid = (l + r) >> 1;
if (getDis(arr[u][mid], u) <= k) l = mid + 1;
else r = mid;
}
if (l < sz) {
std::pair<int, int> it = res[u][l];
if (whr[u][it.first] == whr[u][ask]) ans = std::min(ans, id[it.second]);
else ans = std::min(ans, id[it.first]);
}
u = divFa[u];
}
print(ans), putchar('\n');
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 8ms
memory: 78716kb
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
Time Limit Exceeded
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...