QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#269695 | #5421. Factories Once More | jrjyy | WA | 111ms | 16160kb | C++20 | 2.7kb | 2023-11-29 21:34:20 | 2023-11-29 21:34:20 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
std::mt19937 rnd(std::random_device{}());
struct Node {
Node *l{}, *r{};
int s = 0;
i64 v = 0;
std::pair<i64, i64> t{0, 0};
decltype(rnd()) w{rnd()};
};
void pull(Node *u) {
u->s = 1 + (u->l ? u->l->s : 0) + (u->r ? u->r->s : 0);
}
void apply(Node *u, std::pair<i64, i64> x) {
if (!u) {
return;
}
u->v += (u->l ? u->l->s : 0) * x.first + x.second;
u->t = {u->t.first + x.first, u->t.second + x.second};
}
void push(Node *u) {
apply(u->l, u->t);
auto t = u->t;
t.second += t.first * (1 + (u->l ? u->l->s : 0));
apply(u->r, t);
u->t = {0, 0};
}
std::pair<Node *, Node *> split(Node *u, Node *v) {
if (!u) {
return {u, u};
}
push(u);
if (u->v > v->v) {
auto [l, r] = split(u->r, v);
u->r = l;
pull(u);
return {u, r};
} else {
auto [l, r] = split(u->l, v);
u->l = r;
pull(u);
return {l, u};
}
}
void insert(Node *&u, Node *v) {
if (!u) {
u = v;
return;
}
if (v->w < u->w) {
std::tie(v->l, v->r) = split(u, v);
pull(v);
u = v;
return;
}
push(u);
if (u->v > v->v) {
insert(u->r, v);
} else {
insert(u->l, v);
}
pull(u);
}
void dfs(Node *&u, Node *v) {
if (!v) {
return;
}
push(v);
dfs(u, v->l);
dfs(u, v->r);
v->l = v->r = nullptr;
pull(v);
insert(u, v);
}
void merge(Node *&u, Node *v) {
if (!u || (v && u->s < v->s)) {
std::swap(u, v);
}
dfs(u, v);
}
i64 get(Node *u, int &k) {
if (!u || !k) {
return 0;
}
push(u);
i64 res = get(u->l, k);
if (k > 0) {
res += u->v;
--k;
}
res += get(u->r, k);
return res;
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, k;
std::cin >> n >> k;
std::vector<std::vector<std::pair<int, int>>> adj(n);
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
std::cin >> u >> v >> w;
--u, --v;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
auto dfs = [&](auto self, int u, int fa) -> Node * {
Node *f{};
for (auto [v, w] : adj[u]) {
if (v == fa) {
continue;
}
auto g = self(self, v, u);
apply(g, {-w * 2, 1ll * w * (k - 1)});
merge(f, g);
}
insert(f, new Node{});
return f;
};
auto f = dfs(dfs, 0, -1);
std::cout << get(f, k) << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3540kb
input:
6 3 1 2 3 2 3 2 2 4 1 1 5 2 5 6 3
output:
22
result:
ok 1 number(s): "22"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
4 3 1 2 2 1 3 3 1 4 4
output:
18
result:
ok 1 number(s): "18"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
2 2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 105ms
memory: 16076kb
input:
100000 17 37253 35652 9892 56367 53643 1120 47896 49255 4547 93065 88999 1745 5251 6742 5031 49828 50972 8974 31548 46729 1032 56341 56287 4812 21896 22838 1682 82124 90557 7307 76289 76949 7028 33834 45380 6856 15499 15064 2265 10127 5251 9920 87208 93945 9487 68990 72637 6891 91640 85004 2259 4748...
output:
4915539756
result:
ok 1 number(s): "4915539756"
Test #5:
score: 0
Accepted
time: 96ms
memory: 15888kb
input:
100000 54 52645 54121 2692 53845 52739 658 88841 87795 9298 79147 80362 6720 80683 80909 7138 30882 28439 3197 85375 85227 6903 80229 77345 445 79601 78148 7956 15262 16666 8402 87894 95824 844 17024 12005 5687 63972 65707 8592 40510 45074 9135 50774 49596 7692 37825 38581 9735 18425 8926 1747 84473...
output:
42231195087
result:
ok 1 number(s): "42231195087"
Test #6:
score: 0
Accepted
time: 88ms
memory: 15780kb
input:
100000 102 16851 15608 9173 34201 33232 4258 54234 53975 3926 85209 83376 6251 7847 6527 2379 92238 90747 5818 74729 75538 1672 98398 98838 6273 53445 54006 4317 32889 33232 9974 12450 15916 1801 40600 40892 1397 81713 79934 4580 28979 30696 8260 90747 94477 5725 54584 53210 3583 53396 53535 1868 88...
output:
175914470238
result:
ok 1 number(s): "175914470238"
Test #7:
score: 0
Accepted
time: 97ms
memory: 15980kb
input:
100000 497 25231 28005 505 36245 34348 7087 59602 57941 1691 77459 74493 7828 11601 4890 9847 11168 11760 3807 97468 93452 8094 14843 13669 488 97738 96869 5615 27369 25231 3081 79596 78931 4604 47155 45416 1728 49273 49193 7851 38358 38735 4842 18025 23117 6165 32677 32970 9931 34174 35023 9709 688...
output:
4545765711714
result:
ok 1 number(s): "4545765711714"
Test #8:
score: 0
Accepted
time: 111ms
memory: 15428kb
input:
100000 1008 9180 10823 9475 54255 56689 9732 42103 43318 4094 11647 12305 5651 63247 66770 513 88023 85592 3518 65473 66850 7176 3995 6958 9978 2408 780 982 71617 71736 9828 22243 19070 9488 41041 40935 2604 2697 6337 8631 90433 86018 7306 73087 76067 9705 92594 99998 6304 70188 68983 8730 13139 132...
output:
11601533653407
result:
ok 1 number(s): "11601533653407"
Test #9:
score: -100
Wrong Answer
time: 104ms
memory: 16160kb
input:
100000 4998 4535 1424 2772 79531 75798 6655 29016 24101 7995 82506 82511 2829 62622 55576 6770 13299 12683 7014 10087 7482 4906 54613 61198 7397 38305 37235 7840 78676 78348 906 68742 70584 5410 96124 96972 9461 84908 86633 3419 99187 98820 1644 34611 31584 1458 18895 19637 6568 38305 39973 5203 324...
output:
464391352443854
result:
wrong answer 1st numbers differ - expected: '464391127886092', found: '464391352443854'