QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729609 | #8235. Top Cluster | veg# | TL | 3165ms | 85020kb | C++14 | 3.2kb | 2024-11-09 17:28:20 | 2024-11-09 17:28:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5 + 5;
int siz[N], mx[N], tot, ans[N], a[N], vis[N], n, q, rt;
ll t[N << 2];
vector<pair<int, int> > G[N];
vector<pair<ll, int> > Q[N];
vector<int> rec;
int idx[N << 2];
void ins(int x, ll y) {
int p = idx[x];
rec.push_back(p);
t[p] = y;
for (p >>= 1; p; p >>= 1)
t[p] = max(t[p << 1], t[p << 1 | 1]);
}
//void ins(int p, int l, int r, int x, ll y) {
// if (l == r) {
// t[p] = y;
// rec.push_back(p);
// return;
// }
// ll mid = l + r >> 1;
// if (x <= mid) ins(p << 1, l, mid, x, y);
// else ins(p << 1 | 1, mid + 1, r, x, y);
// t[p] = max(t[p << 1], t[p << 1 | 1]);
//}
int ask(int p, int l, int r, ll d) {
if (l == r) return l;
int mid = l + r >> 1;
if (t[p << 1] > d) return ask(p << 1, l, mid, d);
else return ask(p << 1 | 1, mid + 1, r, d);
}
void get_rt(int u, int fa) {
siz[u] = 1;
mx[u] = 0;
for (auto &e : G[u]) {
int v = e.first;
if (v == fa || vis[v]) continue;
get_rt(v, u);
siz[u] += siz[v];
if (siz[v] > mx[u]) mx[u] = siz[v];
}
mx[u] = max(mx[u], tot - siz[u]);
if (rt == 0 || mx[u] < mx[rt]) rt = u;
}
void get_ans(int u, int fa, ll d) {
for (auto &q : Q[u])
ans[q.second] = min(ans[q.second], ask(1, 0, n, q.first - d));
for (auto &e : G[u]) {
int v = e.first;
if (vis[v] || v == fa) continue;
get_ans(v, u, d + e.second);
}
}
void travel(int u, int fa, long long d) {
if (a[u] <= n)
ins(a[u], d);
for (auto &e : G[u]) {
int v = e.first;
if (v == fa || vis[v]) continue;
travel(v, u, d + e.second);
}
}
void dfs(int u) {
vis[u] = 1;
rec.clear();
if (a[u] <= n)
ins(a[u], 0);
for (auto e : G[u]) {
int v = e.first;
if (vis[v]) continue;
get_ans(v, u, e.second);
travel(v, u, e.second);
}
for (auto q : Q[u])
ans[q.second] = min(ans[q.second], ask(1, 0, n, q.first));
for (int x : rec) {
int y = x;
while (y) t[y] = 0, y >>= 1;
}
rec.clear();
for (int i = G[u].size() - 1; i >= 0; --i) {
auto &e = G[u][i];
int v = e.first;
if (vis[v]) continue;
get_ans(v, u, e.second);
travel(v, u, e.second);
}
for (int x : rec) {
int y = x;
while (y) t[y] = 0, y >>= 1;
}
for (auto &e : G[u]) {
int v = e.first;
if (vis[v]) continue;
rt = 0, tot = siz[v];
get_rt(v, u);
dfs(rt);
}
}
void build(int p, int l, int r) {
if (l == r) {
idx[l] = p;
return;
}
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
int main() {
// freopen("r.in", "r", stdin);
// freopen("r.out", "w", stdout);
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i < n; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
G[u].push_back(make_pair(v, w));
G[v].push_back(make_pair(u, w));
}
for (int i = 1; i <= q; ++i) {
int x; ll k;
scanf("%d%lld", &x, &k);
Q[x].push_back(make_pair(k, i));
ans[i] = 1e9;
}
build(1, 0, n);
rt = 0, tot = n;
get_rt(1, -1);
dfs(rt);
sort(a + 1, a + n + 1);
int mex = 0;
for (;; ++mex)
if (a[mex + 1] != mex)
break;
for (int i = 1; i <= q; ++i)
printf("%d\n", min(ans[i], mex));
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 38708kb
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: 0
Accepted
time: 3165ms
memory: 83132kb
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:
ok 500000 numbers
Test #3:
score: 0
Accepted
time: 2976ms
memory: 85020kb
input:
500000 500000 416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...
output:
0 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 250002 2500...
result:
ok 500000 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
500000 500000 161260 258036 999901390 280191 21975 193647 397811 362276 206096 421117 336278 416815 31870 999934957 999977591 15 999991550 235871 999916756 19 241647 466194 218880 352702 84559 479034 24903 124981 339271 153019 157132 333035 124326 464181 219609 94164 999933701 440055 118732 2285 175...