QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#578078 | #7118. Closing Time | devxxed# | 9 | 2ms | 4176kb | C++20 | 7.6kb | 2024-09-20 16:22:44 | 2024-09-20 16:22:44 |
answer
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define fi first
#define se second
#define All(x) x.begin(), x.end()
constexpr ll INF = 2'000'000'000'000'000'000;
template<typename Z> bool chmin(Z &a, Z b) { return ((a > b) ? (a = b, true) : false); }
template<typename Z> bool chmax(Z &a, Z b) { return ((a < b) ? (a = b, true) : false); }
#define debug(x) cout << #x << " = " << (x) << "\n"
template<typename Y, typename Z>
ostream& operator<<(ostream& os, const pair<Y, Z> &p) {
return os << "(" << p.fi << ", " << p.se << ")";
}
template<typename Z>
ostream& operator<<(ostream& os, const vector<Z> &v) {
os << "["; bool _first = true;
for (auto i : v) { if (!_first) { os << ", "; } os << i; _first = false; }
return os << "]";
}
struct Tree {
int n;
vector<vector<int>> adj;
vector<int> u, v;
vector<ll> w;
vector<int> par_edge;
int timer;
vector<int> tin, tout;
vector<bool> special_path;
Tree(int n, vector<int> _u, vector<int> _v, vector<int> _w) :
n(n), adj(n), u(_u), v(_v), w(n-1),
par_edge(n), timer(0), tin(n), tout(n),
special_path(n-1)
{
for (int i = 0; i < n-1; i++) {
adj[u[i]].push_back(i);
adj[v[i]].push_back(i);
w[i] = _w[i];
}
dfsInit(0);
}
void dfsInit(int cur, int prv = -1) {
par_edge[cur] = prv;
tin[cur] = ++timer;
for (auto e : adj[cur]) if (e != prv) {
int nxt = getDest(cur, e);
dfsInit(nxt, e);
}
tout[cur] = timer;
}
int getDest(int x, int e) { return ((x == u[e]) ? v[e] : u[e]); }
bool isAncestor(int anc, int des) {
return tin[anc] <= tin[des] && tout[des] <= tout[anc];
}
pair<vector<int>, vector<ll>> getPath(int x, int y) {
int lca = x;
while (!isAncestor(lca, y)) lca = getDest(lca, par_edge[lca]);
vector<int> nodes_x, nodes_y;
vector<ll> dist_x, dist_y;
while (x != lca) {
special_path[par_edge[x]] = true;
nodes_x.push_back(x);
dist_x.push_back(w[par_edge[x]]);
x = getDest(x, par_edge[x]);
}
while (y != lca) {
special_path[par_edge[y]] = true;
nodes_y.push_back(y);
dist_y.push_back(w[par_edge[y]]);
y = getDest(y, par_edge[y]);
}
nodes_x.push_back(lca);
while (!nodes_y.empty()) { nodes_x.push_back(nodes_y.back()); nodes_y.pop_back(); }
while (!dist_y.empty()) { dist_x.push_back(dist_y.back()); dist_y.pop_back(); }
return {nodes_x, dist_x};
}
void dfsDistance(int x, int prv, ll root_dist, vector<ll> &dist) {
if (root_dist > 0) dist.push_back(root_dist);
for (auto e : adj[x]) {
if (e == prv || special_path[e]) continue;
dfsDistance(getDest(x, e), e, root_dist + w[e], dist);
}
}
vector<ll> getDistances(int x) {
vector<ll> ret;
dfsDistance(x, -1, 0, ret);
return ret;
}
};
vector<vector<ll>> insideTimes(vector<ll> dist, vector<ll> &dx, vector<ll> &dy) {
int n = dist.size() + 1;
dx = dy = vector<ll>(n);
dx[0] = dy[n-1] = 0;
for (int i = 1; i < n; i++) dx[i] = dx[i-1] + dist[i-1];
for (int i = n-2; i >= 0; i--) dy[i] = dy[i+1] + dist[i];
// debug(dx); debug(dy);
vector ret(n+1, vector(n+1, INF));
for (int l = 0; l <= n; l++) {
vector<ll> node_costs(n, 0);
ll sum = 0;
for (int i = 0; i < l; i++) { node_costs[i] = dx[i]; sum += dx[i]; }
for (int r = 0; r <= n; r++) {
if (r > 0) {
int i = n-r;
sum -= node_costs[i];
chmax(node_costs[i], dy[i]);
sum += node_costs[i];
}
ret[l][r] = sum;
}
}
return ret;
}
vector<ll> minPlusConvolution(vector<ll> a, vector<ll> b) {
int n = a.size(), m = b.size();
vector<ll> ret(n + m - 1, INF);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
chmin(ret[i + j], a[i] + b[j]);
}
}
return ret;
}
vector<ll> dncConvolution(int l, int r, vector<vector<ll>> &v) {
if (l == r) return v[l];
int m = (l + r) / 2;
return minPlusConvolution(dncConvolution(l, m, v), dncConvolution(m+1, r, v));
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
Tree T(N, U, V, W);
// generate minimum time for all possible scores of inside case
auto [inside_nodes, inside_dist] = T.getPath(X, Y);
vector<ll> dist_x, dist_y;
auto inside_times = insideTimes(inside_dist, dist_x, dist_y);
// debug(inside_nodes); debug(inside_dist);
// debug(dist_x); debug(dist_y);
int p_len = inside_nodes.size();
int ans = 2;
vector<vector<ll>> distances;
for (int i = 0; i < p_len; i++) distances.push_back(T.getDistances(inside_nodes[i]));
for (int l = 1; l <= p_len; l++) {
for (int r = 1; r <= p_len; r++) {
if (inside_times[l][r] > K) continue;
vector<pll> pos;
for (int i = 0; i < p_len; i++) {
bool in_l = (i < l);
bool in_r = (i >= p_len-r);
if (in_l && in_r) {
ll c[2] = {dist_x[i], dist_y[i]};
if (c[0] > c[1]) swap(c[0], c[1]);
for (auto d : distances[i]) pos.push_back({d + c[0], c[1] - c[0]});
} else if (in_l) {
for (auto d : distances[i]) pos.push_back({d + dist_x[i], INF});
} else if (in_r) {
for (auto d : distances[i]) pos.push_back({d + dist_y[i], INF});
}
}
multiset<ll> ms;
sort(All(pos));
ll rem = K - inside_times[l][r];
const auto greedyTake = [&](ll bound) {
int ret = 0;
for (auto x : ms) {
if (x <= bound) { ret++; bound -= x; }
else break;
}
return ret;
};
// cout << l << " " << r << "\n"; debug(one); debug(two); debug(inside_times[l][r]);
chmax(ans, l + r);
int taken = 0;
for (auto [o, t] : pos) {
rem -= o;
taken++;
if (rem < 0) break;
ms.insert(t);
chmax(ans, l + r + taken + greedyTake(rem));
}
// debug(ans); cout << "\n";
}
}
return ans;
}
#ifdef Zanite
int main()
{
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> N(Q), X(Q), Y(Q);
std::vector<long long> K(Q);
std::vector<std::vector<int>> U(Q), V(Q), W(Q);
for (int q = 0; q < Q; q++)
{
assert(4 == scanf("%d %d %d %lld", &N[q], &X[q], &Y[q], &K[q]));
U[q].resize(N[q] - 1);
V[q].resize(N[q] - 1);
W[q].resize(N[q] - 1);
for (int i = 0; i < N[q] - 1; ++i)
{
assert(3 == scanf("%d %d %d", &U[q][i], &V[q][i], &W[q][i]));
}
}
fclose(stdin);
std::vector<int> result(Q);
for (int q = 0; q < Q; q++)
{
result[q] = max_score(N[q], X[q], Y[q], K[q], U[q], V[q], W[q]);
}
for (int q = 0; q < Q; q++)
{
printf("%d\n", result[q]);
}
fclose(stdout);
return 0;
}
#endif
詳細信息
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 200000 31011 61157 8517583098 31011 129396 964383 1655 129396 331139 1655 191487 566483 110385 191487 865248 43212 110385 542661 43212 81682 13766 81682 91774 546589 91774 124706 780638 124706 175650 118706 10421 175650 615314 10421 151953 436270 140430 151...
output:
result:
Subtask #2:
score: 9
Accepted
Test #4:
score: 9
Accepted
time: 1ms
memory: 4092kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 50 23 25 382806473 0 1 375710 1 2 898637 2 3 10402 3 4 536577 4 5 385023 5 6 71075 6 7 543368 7 8 301497 8 9 174394 9 10 711312 10 11 923006 11 12 675532 12 13 838667 13 14 565729 14 15 979816 15 16 862618 16 17 576015 17 18 177751 18 19 306989 19 20 881492...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 96
result:
ok
Test #5:
score: 9
Accepted
time: 0ms
memory: 3800kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 47 20 22 25669694 0 1 291237 1 2 851987 2 3 421247 3 4 962919 4 5 643085 5 6 224951 6 7 756890 7 8 147295 8 9 625823 9 10 736374 10 11 290526 11 12 335466 12 13 539086 13 14 957449 14 15 423408 15 16 932444 16 17 356032 17 18 307118 18 19 94279 19 20 989546...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 26
result:
ok
Test #6:
score: 9
Accepted
time: 1ms
memory: 3876kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 46 6 34 355277954 0 1 574359 1 2 58362 2 3 778155 3 4 538832 4 5 128903 5 6 79774 6 7 715282 7 8 259924 8 9 640303 9 10 361135 10 11 506866 11 12 527045 12 13 946672 13 14 620381 14 15 701743 15 16 766266 16 17 524732 17 18 340089 18 19 630172 19 20 357712 ...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 74
result:
ok
Test #7:
score: 9
Accepted
time: 0ms
memory: 3788kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 45 14 44 70669 0 1 266130 1 2 372315 2 3 965349 3 4 119493 4 5 190522 5 6 506822 6 7 45330 7 8 423189 8 9 748532 9 10 653554 10 11 102552 11 12 90431 12 13 843259 13 14 422626 14 15 35334 15 16 1 16 17 1 17 18 1 18 19 1 19 20 1 20 21 1 21 22 1 22 23 1 23 24...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 4
result:
ok
Test #8:
score: 9
Accepted
time: 1ms
memory: 4096kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 50 0 49 332340134 0 1 987193 1 2 950856 2 3 893737 3 4 870689 4 5 810192 5 6 790242 6 7 744052 7 8 714922 8 9 677074 9 10 630082 10 11 599644 11 12 528415 12 13 491648 13 14 479486 14 15 439182 15 16 371085 16 17 357776 17 18 308161 18 19 254481 19 20 20430...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 44
result:
ok
Test #9:
score: 9
Accepted
time: 1ms
memory: 4068kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 50 0 49 329827838 0 1 972981 1 2 953594 2 3 908629 3 4 849675 4 5 808354 5 6 782643 6 7 759387 7 8 698618 8 9 650896 9 10 639153 10 11 591828 11 12 532588 12 13 503214 13 14 447660 14 15 403224 15 16 380834 16 17 327127 17 18 300115 18 19 244025 19 20 23362...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 44
result:
ok
Test #10:
score: 9
Accepted
time: 0ms
memory: 4080kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 3 0 1 1 0 1 1 1 2 1
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 3
result:
ok
Test #11:
score: 9
Accepted
time: 0ms
memory: 3816kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 20 8 9 31897732 0 1 428932 1 2 103619 2 3 805647 3 4 691922 4 5 320369 5 6 755877 6 7 701672 7 8 426711 8 9 507102 9 10 492732 10 11 68466 11 12 317311 12 13 37666 13 14 499673 14 15 502226 15 16 753200 16 17 523387 17 18 904778 18 19 695859
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 30
result:
ok
Test #12:
score: 9
Accepted
time: 0ms
memory: 3880kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 20 15 16 25160185 0 1 193268 1 2 139314 2 3 193167 3 4 446469 4 5 793626 5 6 502774 6 7 598587 7 8 69975 8 9 499282 9 10 943594 10 11 804459 11 12 536095 12 13 578060 13 14 142523 14 15 364845 15 16 181397 16 17 64327 17 18 224827 18 19 439557
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 26
result:
ok
Test #13:
score: 9
Accepted
time: 0ms
memory: 3740kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 20 0 19 77846543 0 1 603320 1 2 433257 2 3 572719 3 4 340716 4 5 549198 5 6 383420 6 7 50703 7 8 720210 8 9 304217 9 10 162280 10 11 68594 11 12 401595 12 13 446738 13 14 920882 14 15 494739 15 16 616729 16 17 874820 17 18 269830 18 19 53532
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 34
result:
ok
Test #14:
score: 9
Accepted
time: 0ms
memory: 3808kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 19 2 17 6692 0 1 748749 1 2 369010 2 3 3351 3 4 1 4 5 1 5 6 1 6 7 1 7 8 1 8 9 1 9 10 1 10 11 1 11 12 1 12 13 1 13 14 1 14 15 1 15 16 1 16 17 3346 17 18 11355
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 3
result:
ok
Subtask #3:
score: 0
Time Limit Exceeded
Dependency #2:
100%
Accepted
Test #15:
score: 12
Accepted
time: 1ms
memory: 3760kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 25 20 9 16 34385 0 1 196 1 2 733 2 3 758 3 4 92 4 5 636 5 6 462 6 7 491 7 8 239 8 9 873 9 10 983 10 11 72 11 12 988 12 13 690 13 14 162 14 15 287 15 16 247 16 17 121 17 18 140 18 19 64 20 9 13 37109 0 1 530 1 2 588 2 3 841 3 4 274 4 5 994 5 6 879 6 7 879 7 8 ...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 25 25 32 30 30 29 27 29 26 34 29 26 28 27 28 25 29 31 25 35 31 29 32 32 32
result:
ok
Test #16:
score: 12
Accepted
time: 2ms
memory: 4176kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 500 370 372 19501231139 0 1 617029 1 2 797198 2 3 612141 3 4 591522 4 5 350816 5 6 688732 6 7 453281 7 8 832047 8 9 259836 9 10 18218 10 11 771848 11 12 559096 12 13 175229 13 14 52243 14 15 644076 15 16 971271 16 17 808287 17 18 369695 18 19 213453 19 20 4...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 771
result:
ok
Test #17:
score: 12
Accepted
time: 1ms
memory: 3908kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 457 25 26 23461573 0 1 966726 1 2 347411 2 3 485905 3 4 803836 4 5 621204 5 6 767483 6 7 853669 7 8 510772 8 9 966639 9 10 221347 10 11 334454 11 12 553189 12 13 435896 13 14 294128 14 15 68783 15 16 492900 16 17 272820 17 18 566713 18 19 958845 19 20 28815...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 23
result:
ok
Test #18:
score: 0
Time Limit Exceeded
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 479 90 404 53062927890 0 1 698391 1 2 854901 2 3 299918 3 4 209753 4 5 125978 5 6 520489 6 7 823919 7 8 421785 8 9 57291 9 10 69394 10 11 44699 11 12 238062 12 13 272509 13 14 761088 14 15 488160 15 16 342026 16 17 452424 17 18 632018 18 19 387438 19 20 836...
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 884
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #36:
score: 9
Accepted
time: 0ms
memory: 3788kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 4 0 1 9 0 2 2 1 2 3 2 3 3
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 6
result:
ok
Test #37:
score: 9
Accepted
time: 0ms
memory: 3816kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 4 0 1 8 0 2 2 1 2 3 2 3 100
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 5
result:
ok
Test #38:
score: 0
Wrong Answer
time: 0ms
memory: 3880kb
input:
cc61ad56a4797fb3f5c9529f73ce6fcedd85669b 1 8 0 4 84 0 1 1 1 2 29 2 3 29 3 4 1 4 5 20 2 6 20 3 7 1
output:
081ce3c351cbf526b37954b9ad30f2b531a7585c OK 8
result:
wrong answer 1st lines differ - on the 1st token, expected: '9', found: '8'
Subtask #6:
score: 0
Skipped
Dependency #2:
100%
Accepted
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #4:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
0%