QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77964 | #5421. Factories Once More | APJifengc | WA | 121ms | 17512kb | C++14 | 3.1kb | 2023-02-16 10:10:30 | 2023-02-16 10:10:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, k;
vector<pair<int, int>> e[MAXN];
mt19937 Rand(chrono::system_clock::now().time_since_epoch().count());
struct Treap {
int lc[MAXN], rc[MAXN], rnd[MAXN];
long long val[MAXN], k[MAXN], b[MAXN];
int siz[MAXN];
int tot;
stack<int> s;
int newNode(long long v) {
int p = s.empty() ? ++tot : s.top();
if (!s.empty()) s.pop();
k[p] = b[p] = lc[p] = rc[p] = 0;
val[p] = v, siz[p] = 1, rnd[p] = Rand();
return p;
}
void pushUp(int p) {
siz[p] = siz[lc[p]] + siz[rc[p]] + 1;
}
void tag(int p, long long K, long long B) {
val[p] += K * (siz[lc[p]] + 1) + B;
k[p] += K, b[p] += B;
}
void pushDown(int p) {
if (k[p] || b[p]) {
if (lc[p]) tag(lc[p], k[p], b[p]);
if (rc[p]) tag(rc[p], k[p], k[p] * (siz[lc[p]] + 1) + b[p]);
k[p] = b[p] = 0;
}
}
void split(int v, int p, int &x, int &y) {
if (!p) x = y = 0;
else {
pushDown(p);
if (v > val[p]) {
y = p;
split(v, lc[p], x, lc[p]);
} else {
x = p;
split(v, rc[p], rc[p], y);
}
pushUp(p);
}
}
int merge(int x, int y) {
if (!x || !y) return x + y;
pushDown(x), pushDown(y);
if (rnd[x] < rnd[y]) {
rc[x] = merge(rc[x], y);
pushUp(x);
return x;
} else {
lc[y] = merge(x, lc[y]);
pushUp(y);
return y;
}
}
void flatten(int p, vector<long long> &v) {
s.push(p);
pushDown(p);
if (lc[p]) flatten(lc[p], v);
v.push_back(val[p]);
if (rc[p]) flatten(rc[p], v);
}
void insert(int &p, int v) {
int x, y; split(v, p, x, y);
p = merge(merge(x, newNode(v)), y);
}
} t;
int root[MAXN];
int siz[MAXN];
void dfs(int u, int pre) {
siz[u] = 1;
int s = 0;
for (auto p : e[u]) if (p.first != pre) {
int v = p.first, w = p.second;
dfs(v, u);
t.tag(root[v], -2ll * w, 1ll * w * (k + 1));
siz[u] += siz[v];
if (siz[v] > siz[s]) s = v;
}
if (s) root[u] = root[s];
t.insert(root[u], 0);
for (auto p : e[u]) if (p.first != pre && p.first != s) {
int v = p.first;
vector<long long> val;
t.flatten(root[v], val);
for (long long w : val) {
t.insert(root[u], w);
}
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i < n; i++) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
e[u].push_back({v, w});
e[v].push_back({u, w});
}
dfs(1, 0);
long long ans = 0;
vector<long long> val;
t.flatten(root[1], val);
for (int i = 1; i <= k; i++) {
ans += val[i - 1];
}
printf("%lld\n", ans);
return 0;
}
/*
6 3
1 2 3
2 3 2
2 4 1
1 5 2
5 6 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9032kb
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: 4ms
memory: 9840kb
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: 1ms
memory: 9096kb
input:
2 2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 121ms
memory: 17512kb
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:
36499779246
result:
wrong answer 1st numbers differ - expected: '4915539756', found: '36499779246'