This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "roads.h"
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define mp make_pair
#define pinode pair<node*, node*>
#define pb push_back
using namespace std;
const int N = 2e5 + 10;
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
struct node {
node *l = nullptr, *r = nullptr;
ll sz = 1, y = 0, sum = 0, x = 0;
node(ll nx) {
x = nx;
sz = 1;
l = r = nullptr;
y = bruh();
sum = nx;
}
} *rt[N];
void pull(node *a) {
if(!a)return;
a->sum = a->x;
a->sz = 1;
if(a->l != nullptr)a->sum += a->l->sum, a->sz += a->l->sz;
if(a->r != nullptr)a->sum += a->r->sum, a->sz += a->r->sz;
}
node *mrg(node *a, node *b) {
if(a == nullptr || b == nullptr)
return (a != nullptr ? a : b);
if(a->y > b->y) {
a->r = mrg(a->r, b);
pull(a);
return a;
}
b->l = mrg(a, b->l);
pull(b);
return b;
}
pinode splitSz(node *a, ll F) {
if(a == nullptr)
return mp(nullptr, nullptr);
if((a->l ? a->l->sz : 0) >= F) {
pinode z = splitSz(a->l, F);
a->l = z.s;
pull(a);
return mp(z.f, a);
}
else {
ll dlt = F - (a->l ? a->l->sz + 1 : 1);
pinode z = splitSz(a->r, dlt);
a->r = z.f;
pull(a);
return mp(a, z.s);
}
}
pinode splitX(node *a, ll x) {
if(a == nullptr)
return mp(nullptr, nullptr);
if(a->x <= x) {
pinode z = splitX(a->r, x);
a->r = z.f;
pull(a);
return mp(a, z.s);
}
else {
pinode z = splitX(a->l, x);
a->l = z.s;
pull(a);
return mp(z.f, a);
}
}
ll get(ll v, ll del) {
if(del <= 0)return 0;
pinode z = splitSz(rt[v], del);
ll sum = (z.f ? z.f->sum : 0);
rt[v] = mrg(z.f, z.s);
return sum;
}
void insert(ll v, ll zn) {
pinode z = splitX(rt[v], zn);
node *u = new node(zn);
rt[v] = mrg(mrg(z.f, u), z.s);
}
void dele(ll v, ll zn) {
pinode z = splitX(rt[v], zn - 1);
pinode z2 = splitSz(z.s, 1);
rt[v] = mrg(z.f, z2.s);
}
ll dp[N][2];
bool was[N];
vector<int> act;
vector<pii> g[N];
vector<pii> g2[N];
vector<int> st[N];
void solve(ll v, ll p, ll x) {
ll del = ((ll)g2[v].size()) - x;
was[v] = 1;
for(auto u : g[v]) {
if(u.f != p) {
solve(u.f, v, x);
}
}
ll sum = 0;
for(auto u : g[v]) {
if(u.f != p) {
sum += dp[u.f][1];
if(dp[u.f][0] + u.s - dp[u.f][1] < 0)
sum += dp[u.f][0] + u.s - dp[u.f][1], del--;
else
insert(v, dp[u.f][0] + u.s - dp[u.f][1]);
}
}
dp[v][0] = sum + get(v, del - 1);
dp[v][1] = sum + get(v, del);
for(auto u : g[v]) {
if(u.f != p) {
if(dp[u.f][0] + u.s - dp[u.f][1] >= 0)
dele(v, dp[u.f][0] + u.s - dp[u.f][1]);
}
}
}
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) {
for(int i = 0; i <= n; i++)
rt[i] = nullptr;
vector<ll> answ;
for(int j = 0; j < n - 1; j++)
U[j]++, V[j]++;
for(int i = 1; i < n; i++) {
g2[U[i - 1]].pb(mp(V[i - 1], W[i - 1]));
g2[V[i - 1]].pb(mp(U[i - 1], W[i - 1]));
}
for(int i = 1; i <= n; i++) {
st[g2[i].size()].pb(i);
}
for(int j = n - 1; j >= 0; j--) {
for(auto v : st[j]) {
act.pb(v);
for(auto u : g2[v]) {
if((ll)g2[u.f].size() > j) {
g[u.f].pb(mp(v, u.s));
g[v].pb(u);
dele(u.f, u.s);
}
else if((ll)g2[u.f].size() == j) {
g[v].pb(u);
}
else {
insert(v, u.s);
}
}
}
ll ans = 0;
for(auto u : act) {
if(!was[u]) {
solve(u, 0, j);
ans += dp[u][1];
}
}
answ.pb(ans);
for(auto u : act)was[u] = 0, dp[u][0] = dp[u][1] = 0;
}
reverse(answ.begin(), answ.end());
return answ;
}
/*
7
1 0 2
2 1 3
3 2 7
4 3 7
5 4 7
6 5 7
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |