제출 #1358076

#제출 시각아이디문제언어결과실행 시간메모리
1358076AvianshFireworks (APIO16_fireworks)C++20
0 / 100
6 ms14404 KiB
#include <bits/stdc++.h>

using namespace std;
//st>=n are explosives
int n,m;
#define int long long

struct slopeTrick{
    priority_queue<int>pri;
    int mn;
    int lazy;
    void shift(int x){
        lazy+=x;
    }
    void push(int x){
        pri.push(x-lazy);
    }
    int top(){
        return pri.top()+lazy;
    }
    void pop(){
        pri.pop();
    }
    int size(){
        return pri.size();
    }
    void printer(){
        vector<int>temp;
        while(size()){
            temp.push_back(top());
            pop();
        }
        reverse(temp.begin(),temp.end());
        for(int i : temp){
            cout << i << " ";
            push(i);
        }
        cout << "\n";
    }
};

const int mxn = 3e5+5;

slopeTrick slopes[mxn];

void dfs(int st, vector<array<int,2>>g[]){
    for(array<int,2>a:g[st]){
        dfs(a[0],g);
    }
    slopes[st].mn=0;
    slopes[st].lazy=0;
    if(st>=n){
        slopes[st].push(0);
        slopes[st].push(0);
    }
    for(array<int,2>a:g[st]){
        if(slopes[a[0]].size()==0){
            continue;
        }
        ///merge slopes[a[0]],slopes[st] into slopes[st];
        ///first shift slopes[a[0]]
        slopes[a[0]].shift(a[1]);
        int A = slopes[a[0]].top();
        slopes[a[0]].pop();
        int B = slopes[a[0]].top();
        slopes[a[0]].pop();
        slopes[a[0]].shift(-a[1]);
        slopes[a[0]].push(A);
        slopes[a[0]].push(B);
        ///slopes shifted change of this edge accounted for now must merge.
        if(slopes[a[0]].size()>slopes[st].size()){
            swap(slopes[a[0]],slopes[st]);
        }
        if(slopes[a[0]].size()==0)
            continue;
        slopes[st].mn+=slopes[a[0]].mn;
        ///first one first
        if(slopes[a[0]].top()>=slopes[st].top()){
            slopes[a[0]].pop();
            //nothing happens
        }
        else{
            ///shenangians
            slopes[st].push(slopes[a[0]].top());
            slopes[st].pop();
            slopes[st].mn+=slopes[st].top()-slopes[a[0]].top();
            slopes[a[0]].pop();
        }
        //first one done now others
        while(slopes[a[0]].size()){
            if(slopes[a[0]].top()>=slopes[st].top()){
                slopes[st].mn+=slopes[a[0]].top()-slopes[st].top();
                slopes[st].push(slopes[a[0]].top());
                slopes[a[0]].pop();
            }
            else{
                slopes[st].push(slopes[a[0]].top());
                slopes[a[0]].pop();
            }
        }
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    vector<array<int,2>>g[n+m];
    for(int i = 1;i<n;i++){
        int p,c;
        cin >> p >> c;
        p--;
        g[p].push_back({i,c});
    }
    for(int i = 0;i<m;i++){
        int p,c;
        cin >> p >> c;
        p--;
        g[p].push_back({i+n,c});
    }
    dfs(0,g);
    cout << slopes[0].mn;
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…