QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#518909 | #9165. Petrol stations | skittles1412 | 18 | 396ms | 33572kb | C++17 | 7.1kb | 2024-08-14 14:03:10 | 2024-08-14 14:03:11 |
Judging History
answer
// cf bits/extc++.h nonsense
#ifdef ONLINE_JUDGE
#define _EXT_CODECVT_SPECIALIZATIONS_H 1
#define _EXT_ENC_FILEBUF_H 1
#endif
#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using u64 = uint64_t;
using ll = long long;
using ld = long double;
template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
inline void init_io() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
}
template <typename T>
vector<T> iota(int n, const T& init) {
vector<T> arr(n);
iota(begin(arr), end(arr), init);
return arr;
}
template <typename T>
vector<vector<T>> transposed(const vector<vector<T>>& arr) {
int n = sz(arr), m = sz(arr[0]);
vector ans(m, vector<T>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[j][i] = arr[i][j];
}
}
return ans;
}
template <typename T>
bool on(const T& mask, int bit) {
return (mask >> bit) & 1;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
out << "[";
for (int i = 0; i < sz(arr); i++) {
if (i) {
out << ", ";
}
out << arr[i];
}
return out << "]";
}
template <typename T, size_t N>
ostream& operator<<(ostream& out, const array<T, N>& arr) {
out << "[";
for (int i = 0; i < sz(arr); i++) {
if (i) {
out << ", ";
}
out << arr[i];
}
return out << "]";
}
template <typename A, typename B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <typename A, typename T>
int lbs(const A& arr, const T& x) {
return int(lower_bound(begin(arr), end(arr), x) - begin(arr));
}
inline vector<bool>::reference& operator&=(vector<bool>::reference&& a, bool b) {
return a = a & b;
}
template <typename T>
T reversed(T arr) {
reverse(begin(arr), end(arr));
return arr;
}
constexpr int MAXN = 7e4 + 5, LOGN = 17;
int n, siz[MAXN];
long kv, ans[MAXN];
bool vis[MAXN];
vector<pair<int, long>> graph[MAXN];
void pdfs(int u, int p) {
siz[u] = 1;
for (auto& [v, _w] : graph[u]) {
if (v == p) {
continue;
}
pdfs(v, u);
siz[u] += siz[v];
}
}
set<pair<long, int>> st2;
vector<long> out2;
long buf2[MAXN], mul2;
void dfs2(int u, int p, long d) {
buf2[u] = 0;
st2.emplace(d, u);
for (auto& [v, w] : graph[u]) {
if (vis[v] || v == p) {
continue;
}
dfs2(v, u, d + w);
}
ans[u] += mul2 * buf2[u];
buf2[u]++;
dbg(u, buf2[u]);
auto it = st2.lower_bound({d - kv, -1});
if (it == begin(st2)) {
fill_n(back_inserter(out2), buf2[u], d);
} else {
if (u == 3) {
dbg(it->second, u, d, d - kv);
}
buf2[it->second] += buf2[u];
}
st2.erase({d, u});
}
int lift3[MAXN][LOGN + 1];
long lift3v[MAXN][LOGN + 1], depth3[MAXN];
template <typename Cb>
void dfs3(int u, int p, long d, const Cb& cb) {
depth3[u] = d;
lift3[u][0] = p;
for (int i = 1; i <= LOGN; i++) {
int cp = lift3[u][i - 1];
if (cp == -1) {
lift3[u][i] = -1;
lift3v[u][i] = 0;
} else {
lift3[u][i] = lift3[cp][i - 1];
lift3v[u][i] = lift3v[u][i - 1] + lift3v[cp][i - 1];
}
}
ans[p] += siz[u] * lift3v[u][0];
auto query_dep_ge = [&](long nd) -> long {
long ans = cb(nd);
int cu = u;
for (int i = LOGN; i >= 0; i--) {
int nu = lift3[cu][i];
if (nu != -1 && depth3[nu] >= nd) {
ans += lift3v[cu][i];
cu = nu;
}
}
return ans;
};
auto query_dep = [&](long ql, long qr) -> long {
return query_dep_ge(ql) - query_dep_ge(qr);
};
for (auto& [v, w] : graph[u]) {
if (vis[v] || v == p) {
continue;
}
lift3v[v][0] = query_dep(d - kv, d - kv + w);
dfs3(v, u, d + w, cb);
}
}
void dfs1(int u) {
while (true) {
pair opt {-1, -1};
for (auto& [v, _w] : graph[u]) {
if (vis[v]) {
continue;
}
opt = max(opt, {siz[v], v});
}
auto& [vsz, v] = opt;
if (vsz * 2 > siz[u]) {
siz[u] -= siz[v];
siz[v] += siz[u];
u = v;
} else {
break;
}
}
dbg("CENTROID", u);
vis[u] = true;
vector<vector<long>> couts;
for (auto& [v, w] : graph[u]) {
if (vis[v]) {
continue;
}
out2.clear();
st2.clear();
st2.emplace(0, u);
mul2 = siz[u] - siz[v];
dfs2(v, u, w);
for (auto& a : out2) {
a = -a;
}
sort(begin(out2), end(out2));
couts.push_back(out2);
}
dbg("MID CENTROID", u, vector(ans, ans + n));
vector<long> a_outs;
a_outs.push_back(0);
for (auto& a : couts) {
a_outs.insert(end(a_outs), begin(a), end(a));
}
sort(begin(a_outs), end(a_outs));
auto query_vec_ge = [&](const vector<long>& arr, long x) -> int {
return int(end(arr) - lower_bound(begin(arr), end(arr), x));
};
depth3[u] = 0;
int ccid = 0;
fill(begin(lift3[u]), end(lift3[u]), -1);
fill(begin(lift3v[u]), end(lift3v[u]), 0);
if (u == 0) {
dbg(couts);
}
for (auto& [v, w] : graph[u]) {
if (vis[v]) {
continue;
}
auto& ccout = couts[ccid++];
auto query_dep_ge = [&](long nd) -> long {
return query_vec_ge(a_outs, nd) - query_vec_ge(ccout, nd);
};
auto query_dep = [&](long ql, long qr) -> long {
return query_dep_ge(ql) - query_dep_ge(qr);
};
lift3v[v][0] = query_dep(-kv, -kv + w);
dfs3(v, u, w, query_dep_ge);
}
dbg("POST CENTROID", u, vector(ans, ans + n));
for (auto& [v, _w] : graph[u]) {
if (vis[v]) {
continue;
}
dfs1(v);
}
}
void solve() {
cin >> n >> kv;
for (int i = 0; i < n - 1; i++) {
int u, v;
long w;
cin >> u >> v >> w;
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
pdfs(0, -1);
dfs1(0);
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
}
int main() {
init_io();
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 18
Accepted
time: 0ms
memory: 10172kb
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:
ok 750 lines
Test #2:
score: 18
Accepted
time: 3ms
memory: 10076kb
input:
967 334 285 176 1 648 431 1 493 893 2 261 165 1 158 512 2 675 881 1 232 200 2 452 380 2 961 35 1 831 131 1 424 458 1 807 835 2 154 855 1 524 513 2 448 27 1 82 331 2 454 190 2 469 210 2 15 445 2 860 1 2 901 47 0 496 572 2 227 122 1 141 223 2 214 924 1 4 381 2 394 703 2 859 611 2 63 688 1 197 674 1 59...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 967 lines
Test #3:
score: 18
Accepted
time: 2ms
memory: 10076kb
input:
1000 963 408 385 876 23 519 951 649 232 605 821 385 792 194 221 882 18 984 910 115 40 155 558 973 126 876 633 625 795 781 727 923 248 397 120 632 320 392 531 185 527 973 508 986 457 918 74 339 432 259 385 243 93 973 961 640 385 531 614 325 839 823 936 691 159 240 184 40 625 891 692 385 331 196 140 1...
output:
0 0 482612 0 912 0 0 766 0 0 0 24 989 0 493138 0 0 0 0 0 1996 979 890 1996 0 0 0 0 1996 0 0 0 0 0 0 0 254507 0 997 2 450646 0 244035 727 0 2043 0 0 0 0 0 0 0 0 0 0 0 0 19929 1996 0 0 0 0 997 0 0 0 0 1994 0 2 0 0 0 0 0 0 0 0 1962 0 0 0 0 3990 493900 0 0 0 0 0 0 3988 0 940 0 0 0 0 0 1996 0 2366 0 1989...
result:
ok 1000 lines
Test #4:
score: 0
Wrong Answer
time: 0ms
memory: 10856kb
input:
1000 396 412 257 190 290 965 25 399 938 174 980 459 117 874 575 124 386 912 40 666 164 124 97 680 49 589 442 197 451 649 109 893 648 134 975 733 198 121 966 63 346 775 94 381 246 169 507 319 159 333 287 101 127 682 118 48 784 34 708 170 0 142 723 148 836 766 185 277 711 188 512 974 68 658 404 136 95...
output:
215160 138947 196491 47632 103775 128657 26093 195362 2694 0 234548 3952 120001 175260 489004 1998 9576 0 97602 2334 86553 18629 5792 57931 251700 122745 0 33115 227964 190106 203868 102592 1290 440 0 9054 79 1620 2166 496039 28992 169701 102833 9701 218081 1463 999 433548 7728 4935 882 817 87200 36...
result:
wrong answer 84th lines differ - expected: '555', found: '4497'
Subtask #2:
score: 8
Accepted
Test #13:
score: 8
Accepted
time: 1ms
memory: 10044kb
input:
2 1 0 1 1
output:
0 0
result:
ok 2 lines
Test #14:
score: 8
Accepted
time: 308ms
memory: 33532kb
input:
70000 1 50913 18377 1 33894 11911 1 61940 7863 1 61602 33470 1 26341 35575 1 30813 50301 1 2994 67214 1 38527 61714 1 37381 3396 1 43274 14867 1 59352 12066 1 68877 40750 1 44756 43671 1 57921 17977 1 10879 47719 1 26878 13556 1 27329 23697 1 45109 11513 1 21307 12312 1 27202 27807 1 7250 14810 1 27...
output:
2128187656 1918647796 1539693556 1198079316 2227641388 537651676 1566795636 1713609688 462341800 403913520 1372196836 2449371376 2448661176 226085260 2289814488 2374543080 1462250988 2446901740 2288343736 788226400 1802397916 2219170356 2367201616 130103388 1927347880 2324936140 630135880 1489088716...
result:
ok 70000 lines
Subtask #3:
score: 10
Accepted
Dependency #2:
100%
Accepted
Test #15:
score: 10
Accepted
time: 356ms
memory: 33536kb
input:
70000 517272873 57335 18148 62837135 28239 56484 253183094 23004 59130 129215861 558 17489 52424960 55884 27928 150784869 35390 18538 216587635 31618 4121 168195328 49409 13677 142007449 61001 39616 40892641 67336 21612 185484704 34486 9556 246576628 4933 23924 201603991 57035 5529 29829254 38668 21...
output:
450859 215263283 544492560 222149 1276050 1993840 179130 3145479 1061520 186918 436257 128366615 192824 0 190234048 88612 216212 1174022 0 1203363128 455706327 343283 436439022 417240 1223229612 182003220 739660714 325508658 0 1222504809 1192350690 0 507280212 444391111 0 866072172 564047 0 90962902...
result:
ok 70000 lines
Test #16:
score: 10
Accepted
time: 396ms
memory: 33572kb
input:
70000 611016790 21272 16063 50360 30758 33429 30642 23317 5625 9045 66335 5731 24130 36096 15843 18089 60665 41152 19640 6148 39003 53047 42150 9859 46839 5191 59681 3924 53733 53709 36514 50103 15977 57464 40309 10907 5473 38449 24104 14928 69191 4445 31512 57129 33963 47456 14993 17284 40793 34588...
output:
69999 102210 23584 30220 0 0 111532 0 0 90100 0 69120 140466 49221 1132 98672 0 16653 60795 0 33401 15030 0 139998 6985 0 80680 0 0 96782 0 3500 19900 0 80444 52863 31767 84426 0 34538 11825 25587 187296 0 78744 36540 0 0 102247 0 71640 100900 58416 0 10056 165995 44813 21184 154679 0 52136 20348 0 ...
result:
ok 70000 lines
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 235ms
memory: 27480kb
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:
769608 34960 1162375 626228 2 419792 493364 419754 3190301 15 838740 515085 769584 0 0 659707 915948 209895 208656 69972 1251384 277968 1231290 517455 825285 560784 417892 349256 489692 979020 139930 139938 209902 1518040 1146693 287344 403088 1 279861 209896 699603 732904 723193 769575 140252 10391...
result:
wrong answer 9th lines differ - expected: '1817301', found: '3190301'
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%