QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#498119 | #9165. Petrol stations | Qwerty1232# | 0 | 89ms | 3700kb | C++23 | 4.2kb | 2024-07-29 23:57:48 | 2024-07-29 23:57:49 |
Judging History
answer
#include <bits/stdc++.h>
int K;
struct Shit {
int64_t tm;
int cnt;
bool operator<(const Shit& other) const {
return tm < other.tm;
}
};
struct Cum {
int64_t dlt;
int sh;
std::vector<Shit> vec;
};
using Fuck = std::vector<Cum>;
// using Fuck2 = std::pair<int64_t, Fuck>;
int cum_size(const Cum& c) {
return c.vec.size() - c.sh;
}
void cum_push(Cum& a) {
for (int i = a.sh; i < a.vec.size(); i++) {
a.vec[i].tm += a.dlt;
}
}
Cum cum_merge(Cum& a, Cum& b) {
Cum res(0, 0, {});
cum_push(a);
cum_push(b);
res.vec.resize(cum_size(a) + cum_size(b));
std::merge(a.vec.begin() + a.sh, a.vec.end(),
b.vec.begin() + b.sh, b.vec.end(),
res.vec.begin());
a.dlt = 0, a.sh = 0, a.vec.clear();
b.dlt = 0, b.sh = 0, b.vec.clear();
return res;
}
// merges to a
void fuck_merge(Fuck& a, Fuck& b) {
a.resize(std::max(a.size(), b.size()) + 1);
Cum carry{0, 0, {}};
for (int i = 0; i < a.size(); i++) {
if (cum_size(carry)) {
if (cum_size(a[i])) {
carry = cum_merge(carry, a[i]);
} else {
a[i] = std::move(carry);
}
}
if (i < b.size() && cum_size(b[i])) {
if (cum_size(a[i])) {
carry = cum_merge(a[i], b[i]);
} else {
a[i] = std::move(b[i]);
}
}
}
assert(cum_size(carry) == 0);
while (a.size() && cum_size(a.back()) == 0) {
a.pop_back();
}
b.clear();
}
std::pair<int, int> cum_fisting(Cum& c, int w) {
c.dlt -= w;
int dlt = 0;
int sum = 0;
while (cum_size(c) && c.vec[c.sh].tm + c.dlt < 0) {
sum += c.vec[c.sh].cnt, c.sh++, dlt++;
}
if (dlt) {
c.vec.push_back({(K - w) - c.dlt, sum});
}
return {dlt, sum};
}
int fuck_fisting(Fuck& f, int w) {
int sum = 0;
for (auto& cum : f) {
auto [dt, sm] = cum_fisting(cum, w);
sum += sm;
}
return sum;
}
void cum_reverse_fisting(Cum& c, int w, int ftg) {
c.dlt += w;
if (ftg) {
c.sh -= ftg;
c.vec.pop_back();
}
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n >> K;
std::vector<std::vector<std::pair<int, int>>> gr(n);
std::vector<std::vector<int64_t>> dp(n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
std::cin >> u >> v >> w;
gr[u].push_back({v, w}), gr[v].push_back({u, w});
}
for (int i = 0; i < n; i++) {
dp[i].resize(gr[i].size());
}
std::vector<int> sz(n), depth(n);
{
auto dfs = [&](auto dfs, int v, int f) -> void {
sz[v] = 1;
for (auto [t, w] : gr[v]) {
if (t != f) {
depth[t] = depth[v] + 1;
dfs(dfs, t, v);
sz[v] += sz[t];
}
}
};
dfs(dfs, 0, -1);
}
auto get_sb_sz = [&](int u, int v) {
if (depth[u] < depth[v]) {
return sz[v];
}
return n - sz[v] + 1;
};
for (int i = 0; i < n; i++) {
auto dfs = [&](auto dfs, int v, int fr) -> Fuck {
Fuck f;
f.push_back(Cum{0, 0, {Shit{K, 1}}});
for (int ti = 0; ti < gr[v].size(); ti++) {
auto [t, w] = gr[v][ti];
if (t == fr) {
continue;
}
auto f2 = dfs(dfs, t, v);
int sum = fuck_fisting(f2, w);
dp[v][ti] = sum;
fuck_merge(f, f2);
}
return f;
};
dfs(dfs, i, -1);
}
std::vector<int64_t> ans(n, 0);
for (int v = 0; v < n; v++) {
for (int ti = 0; ti < gr[v].size(); ti++) {
auto [t, w] = gr[v][ti];
ans[t] += int64_t(dp[v][ti]) * (get_sb_sz(t, v));
}
}
for (int i = 0; i < n; i++) {
std::cout << ans[i] << " \n"[i == n - 1];
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 89ms
memory: 3700kb
input:
750 918 159 63 18 573 310 105 135 400 57 618 27 113 41 265 24 99 576 61 242 85 109 490 88 0 626 721 0 407 446 0 78 644 124 346 198 17 541 504 147 543 423 24 302 450 25 397 344 80 129 607 76 474 59 140 30 10 29 375 260 1 404 81 0 658 695 153 450 100 92 532 249 10 597 151 133 739 714 0 212 345 85 558 ...
output:
0 96 45 94 0 0 0 0 0 146 0 0 0 20 32 0 0 88 18 0 2 0 0 0 0 0 43 48 36 10 13 18 0 42 158 0 35 79 17 0 0 0 0 36 0 84 0 8 0 0 20 38 6 0 0 0 0 52 12 4 0 0 12 0 0 0 198 0 30 16 13 45 0 46 0 0 18 44 0 12 0 105 0 0 0 28 75 0 0 12 48 0 0 66 0 0 0 35 0 18 65 42 52 0 0 140 81 114 0 0 27 60 30 76 0 43 0 0 75 2...
result:
wrong answer 1st lines differ - expected: '0', found: '0 96 45 94 0 0 0 0 0 146 0 0 0...0 0 0 0 242 0 35 24 6 0 3 0 0 0'
Subtask #2:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 0ms
memory: 3536kb
input:
2 1 0 1 1
output:
0 0
result:
wrong answer 1st lines differ - expected: '0', found: '0 0'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #17:
score: 0
Time Limit Exceeded
input:
69973 4 44281 27162 1 15299 61302 1 19250 66379 1 45970 65938 1 23683 4445 2 62232 40589 1 37028 58991 2 58769 32024 0 41860 69672 2 14687 10058 2 7874 6780 2 60579 37047 2 739 4096 2 53137 22114 2 35060 21464 0 42597 11591 2 68051 23473 2 61458 35690 2 38719 6601 2 57406 26025 1 38192 41521 0 47941...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%