제출 #1248528

#제출 시각아이디문제언어결과실행 시간메모리
1248528Zbyszek99트리 (IOI24_tree)C++20
100 / 100
138 ms83824 KiB
#include "tree.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}

vi sons[200001];
int P[200001];
ll W[200001];
vl K[200001];
ll Z[200001];
bool is_leaf[200001];
bool used[200001];

int rep_[200001];
int leafs[200001];
int n;
ll leafs_cnt;

ll Ld[1000001];
ll Rd[1000001];
int cur_k[200001];
vector<pll> events[1000001];

int fint(int v)
{
    if(rep_[v] == v) return v;
    rep_[v] = fint(rep_[v]);
    return rep_[v];
}

void onion(int a, int b)
{
    a = fint(a);
    b = fint(b);
    rep_[b] = a;
    leafs[a] += leafs[b]-1;
}

void init(vi P2, vi W2) 
{
    n = siz(P2);
    rep(i,n)
    {
        P[i] = P2[i];
        W[i] = W2[i];
        sons[P[i]].pb(i);
        rep_[i] = i;
        is_leaf[i] = 1;
    }
    rep(i,n) is_leaf[P[i]] = 0;
    rep(i,n) leafs[P[i]]++;
    rep(i,n) if(is_leaf[i]) leafs_cnt += W[i];
    vector<pll> v_sort;
    rep(i,n) if(!is_leaf[i]) v_sort.pb({W[i],i});
    sort(all(v_sort));
    reverse(all(v_sort));
    forall(it,v_sort)
    {
        int v = it.ss;
        used[v] = 1;
        forall(it,sons[v])
        {
            if(used[it]) K[v].pb(leafs[fint(it)]);
            else K[v].pb(1);
        }
        Z[v] = (P[v] != -1 ? (used[P[v]] ? leafs[fint(P[v])]-1 : 0):0);
        forall(it,sons[v])
        {
            if(used[it])
            {
                onion(v,it);
            }
        }
        if(P[v] != -1 && used[P[v]])
        {
            onion(P[v],v);
        }
    }
    ll cur_L = 0;
    ll cur_R = 0;
    rep(i,n)
    {
        if(is_leaf[i]) continue;
        cur_R += W[i] * siz(K[i]);
        cur_L -= W[i];
        ll sk = 0;
        forall(it,K[i])
        {
            sk += it;
            events[it].pb({it*W[i],-W[i]});
        }
        events[Z[i]+1].pb({(1+Z[i])*W[i],-W[i]});
        events[max(K[i].back(),Z[i]+sk)].pb({-(Z[i]+sk) * W[i],W[i]});
    }
    rep2(d,1,1000000)
    {
        forall(it,events[d])
        {
            cur_L += it.ff;
            cur_R += it.ss;
        }
        Ld[d] = cur_L;
        Rd[d] = cur_R;
    }
}

ll query(int L, int R) 
{
    int d = R/L;
    return leafs_cnt*L + Ld[d] * L + Rd[d] * R;
}
#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...