QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#601912 | #9165. Petrol stations | liuziao# | 18 | 533ms | 22120kb | C++23 | 4.1kb | 2024-09-30 15:47:39 | 2024-09-30 15:47:43 |
Judging History
answer
#include <bits/stdc++.h>
// #define int int64_t
const int kMaxN = 7e4 + 5;
int n, k, m, rt, nowr;
int sz[kMaxN], mx[kMaxN], f[kMaxN];
int64_t det, ans[kMaxN], sum[kMaxN], unq[kMaxN * 2];
bool del[kMaxN];
std::vector<std::pair<int, int>> G[kMaxN];
std::vector<int> id, vec[kMaxN];
struct BIT {
int c[kMaxN * 2];
void upd(int x, int v) {
for (; x <= m; x += x & -x) c[x] += v;
}
int qry(int x) {
int ret = 0;
for (; x; x -= x & -x) ret += c[x];
return ret;
}
void clear() { std::fill_n(c + 1, m, 0); }
} bit;
void getsz(int u, int fa) {
sz[u] = 1, mx[u] = 0;
for (auto [v, w] : G[u]) {
if (v == fa || del[v]) continue;
getsz(v, u);
sz[u] += sz[v], mx[u] = std::max(mx[u], sz[v]);
}
}
void getrt(int u, int fa, int tot) {
mx[u] = std::max(mx[u], tot - sz[u]);
if (!rt || mx[u] < mx[rt]) rt = u;
for (auto [v, w] : G[u]) {
if (v == fa || del[v]) continue;
getrt(v, u, tot);
}
}
void discrete() {
std::sort(unq + 1, unq + 1 + m);
m = std::unique(unq + 1, unq + 1 + m) - (unq + 1);
}
int getid(int64_t x) { return std::lower_bound(unq + 1, unq + 1 + m, x) - unq; }
void dfs2(int u, int fa, int rt, int coef) {
vec[rt].emplace_back(u), id.emplace_back(u);
f[u] = 1, unq[++m] = sum[u] + k;
for (auto [v, w] : G[u]) {
if (v == fa || del[v]) continue;
sum[v] = sum[u] + w;
dfs2(v, u, rt, coef);
}
if (sum[u] > k) {
int L = 0, R = (int)id.size(), res = 0;
while (L + 1 < R) {
int mid = (L + R) >> 1;
if (sum[u] - sum[id[mid]] <= k) R = res = mid;
else L = mid;
}
f[id[res]] += f[u], ans[id[res]] += 1ll * coef * f[u];
f[u] = 0;
} else {
unq[++m] = k - sum[u];
}
id.pop_back();
}
void dfs3(int u, int fa) {
for (auto [v, w] : G[u]) {
if (v == fa || del[v]) continue;
int r = getid(sum[u] + w) - 1, lst = nowr, cnt = 0;
if (r > nowr) {
cnt = bit.qry(r) - bit.qry(nowr);
nowr = r;
bit.upd(getid(sum[u] + k), cnt);
ans[u] += 1ll * cnt * sz[v];
}
dfs3(v, u);
if (r > lst) {
bit.upd(getid(sum[u] + k), -cnt);
nowr = lst;
}
}
}
void dfs1(int u) {
m = sum[u] = 0, id = {u};
unq[++m] = 0, unq[++m] = k;
f[u] = 1;
getsz(u, 0);
for (auto [v, w] : G[u]) {
if (del[v]) continue;
vec[v].clear(), sum[v] = w;
dfs2(v, u, v, sz[u] - sz[v]);
}
discrete();
bit.upd(getid(sum[u]), f[u]);
for (auto [v, w] : G[u]) {
if (del[v]) continue;
for (auto x : vec[v]) {
if (f[x]) {
assert(sum[x] <= k);
bit.upd(getid(k - sum[x]), f[x]);
}
}
}
for (auto [v, w] : G[u]) {
if (del[v]) continue;
for (auto x : vec[v]) {
if (f[x]) {
assert(sum[x] <= k);
bit.upd(getid(k - sum[x]), -f[x]);
}
}
nowr = 0;
int r = getid(w) - 1, cnt = 0;
if (r) {
cnt = bit.qry(r) - bit.qry(nowr);
nowr = r;
bit.upd(getid(k), cnt), ans[u] += 1ll * (cnt - 1) * sz[v];
}
dfs3(v, u);
if (r) {
nowr = 0, bit.upd(getid(k), -cnt);
}
for (auto x : vec[v]) {
if (f[x]) {
assert(sum[x] <= k);
bit.upd(getid(k - sum[x]), f[x]);
}
}
}
// std::cerr << u << '\n';
// return;
bit.clear();
del[u] = 1;
for (auto [v, w] : G[u]) {
if (del[v]) continue;
rt = 0, getsz(v, 0), getrt(v, 0, sz[v]), dfs1(rt);
}
}
void dickdreamer() {
std::cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v, w;
std::cin >> u >> v >> w;
++u, ++v;
G[u].emplace_back(v, w), G[v].emplace_back(u, w);
}
rt = 0, getsz(1, 0), getrt(1, 0, n), dfs1(rt);
for (int i = 1; i <= n; ++i) std::cout << ans[i] << '\n';
}
int32_t main() {
#ifdef ORZXKR
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
// std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
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: 2ms
memory: 3756kb
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:
12 96 45 94 0 0 29 0 16 150 0 28 0 20 32 0 0 88 18 0 2 9 15 0 0 0 43 48 36 10 13 18 0 42 160 0 35 79 17 15 0 0 14 48 0 84 0 8 0 0 20 38 6 0 0 0 0 52 12 4 0 0 12 0 25 0 198 0 30 16 13 45 0 46 0 0 18 44 18 12 0 109 0 0 0 28 75 0 20 12 48 0 0 66 0 0 0 35 20 18 65 42 52 0 0 140 81 114 20 18 27 60 30 86 ...
result:
wrong answer 1st lines differ - expected: '0', found: '12'
Subtask #2:
score: 8
Accepted
Test #13:
score: 8
Accepted
time: 0ms
memory: 3696kb
input:
2 1 0 1 1
output:
0 0
result:
ok 2 lines
Test #14:
score: 8
Accepted
time: 441ms
memory: 21968kb
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: 463ms
memory: 21320kb
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: 533ms
memory: 22120kb
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: 326ms
memory: 17176kb
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 1817550 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: '1817550'
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%