Submission #1221754

#TimeUsernameProblemLanguageResultExecution timeMemory
1221754Theo830트리 (IOI24_tree)C++20
0 / 100
42 ms17568 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define id pair<ll,vector<ll> >
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "tree.h"
int n;
ll l,r;
std::vector<int> p, w;
vector<vector<int> >adj;
multiset<ii>pq[60005];
ll par[60005];
ll siz[60005];
ll find_par(ll a){
    if(par[a] == a){
        return a;
    }
    return par[a] = find_par(par[a]);
}
void enose(ll a,ll b){
    ll p1 = find_par(a);
    ll p2 = find_par(b);
    if(p1 == p2){
        return;
    }
    if(pq[p2].size() < pq[p1].size()){
        swap(p1,p2);
    }
    for(auto x:pq[p1]){
        pq[p2].insert(x);
    }
    pq[p1].clear();
    par[p1] = p2;
    siz[p2] += siz[p1];
    siz[p1] = 0;
}
ii solve(ll idx){
    if(adj[idx].empty()){
        return {l,l * w[idx]};
    }
    ii ans = {0,0};
    ll sum = 0;
    for(auto x:adj[idx]){
        ii res = solve(x);
        enose(idx,x);
        ans.S += res.S;
        sum += res.F;
    }
    ll p = find_par(idx);
    while((sum > r && !pq[p].empty()) && -(*pq[p].begin()).F < w[idx]){
        auto it = pq[p].begin();
        ll thelo = min((*it).S,sum - r);
        ll we = -(*it).F;
        ans.S += thelo * we;
        sum -= thelo;
        siz[p] -= thelo;
        ll posa = (*it).S - thelo;
        pq[p].erase(pq[p].find(*it));
        if(posa){
            pq[p].insert(ii(-we,posa));
        }
    }
    if(sum > r){
        ans.S += w[idx] * (sum - r);
        sum = r;
    }
    pq[p].insert(ii(-w[idx],sum - l));
    siz[p] += sum - l;
    while(siz[p] > sum-l){
        auto it = pq[p].rbegin();
        ll thelo = min((*it).S,siz[p] - (sum - l));
        ll we = -(*it).F;
        siz[p] -= thelo;
        ll posa = (*it).S - thelo;
        pq[p].erase(pq[p].find(*it));
        if(posa){
            pq[p].insert(ii(-we,posa));
        }
    }
    ans.F = sum;
    return ans;
}
void init(std::vector<int> P, std::vector<int> W) {
    p = P;
    w = W;
    n = (int)p.size();
    adj.assign(n+5,vector<int>());
    f(i,1,n){
        adj[p[i]].pb(i);
    }
}
long long query(int L, int R){
    l = L;
    r = R;
    f(i,0,n){
        pq[i].clear();
        par[i] = i;
        siz[i] = 0;
    }
    return solve(0).S;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...