답안 #1088218

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1088218 2024-09-14T06:57:15 Z mispertion Fireworks (APIO16_fireworks) C++14
7 / 100
7 ms 12124 KB
#include <bits/stdc++.h>

using namespace std;    
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"

const ld PI = 3.1415926535;
const int N = 5e5 + 10;
const int M = 1e5 + 10;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = 2e18;
const int P = 505319;
const ld eps = 1e-9;

int mult(int a, int b){
    return a * 1LL * b % mod;
}

int sum(int a, int b){
    a %= mod;
    b %= mod;
    if(a + b >= mod)
        return a + b - mod;
    if(a + b < 0)
        return a + b + mod;
    return a + b;
}

int binpow(int a, int n){
    if (n == 0)
        return 1;
    if (n % 2 == 1){
        return mult(binpow(a, n - 1), a);
    }
    else{
        auto b = binpow(a, n / 2);
        return mult(b, b);
    }
}

int n, m, h[N], l[N], r[N];
vector<pii> g[N];

void dfs(int v, int w){
    if(v > n){
        h[v] = 0;
        l[v] = w;
        r[v] = w;
        return;
    }
    multiset<int> lm, rm;
    for(auto ee : g[v]){
        int u = ee.F, w = ee.S;
        dfs(u, w);
        h[v] += h[u];
        if(sz(lm) == 0){
            lm.insert(l[u]);
            rm.insert(r[u]);
        }else{
            int cl = *lm.rbegin();
            int cr = *rm.begin();
            if(cr < l[u])
                h[v] += l[u] - cr;
            else if(r[u] < cl)
                h[v] += (cl - r[u]);
            if(l[u] <= cl)
                lm.insert(l[u]);
            else
                rm.insert(l[u]);
            if(r[u] <= cl)
                lm.insert(l[u]);
            else
                rm.insert(r[u]);
            if(sz(lm) > sz(rm)){
                int x = *lm.rbegin();
                lm.erase(lm.find(x));
                rm.insert(x);
            }else if(sz(lm) < sz(rm)){
                int x = *rm.begin();
                rm.erase(rm.begin());
                lm.insert(x);
            }
        }
    }
    assert(sz(lm) == sz(rm));
    l[v] = *lm.rbegin();
    r[v] = *rm.begin();
    l[v] += w;
    r[v] += w;
}

void solve(){
    cin >> n >> m;
    for(int i = 2; i <= n + m; i++){
        int p, w;
        cin >> p >> w;
        g[p].pb({i, w});
    }
    dfs(1, 0);
    cout << h[1] << '\n';
}   

signed main(){
    mispertion;
    int t = 1;
    //cin >> t;
    while (t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12120 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12120 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12072 KB Output is correct
9 Correct 4 ms 12124 KB Output is correct
10 Correct 5 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12120 KB Output is correct
2 Incorrect 5 ms 12120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12120 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12120 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12072 KB Output is correct
9 Correct 4 ms 12124 KB Output is correct
10 Correct 5 ms 12124 KB Output is correct
11 Correct 7 ms 12120 KB Output is correct
12 Incorrect 5 ms 12120 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 5 ms 12120 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12120 KB Output is correct
5 Correct 5 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 5 ms 12124 KB Output is correct
8 Correct 5 ms 12072 KB Output is correct
9 Correct 4 ms 12124 KB Output is correct
10 Correct 5 ms 12124 KB Output is correct
11 Correct 7 ms 12120 KB Output is correct
12 Incorrect 5 ms 12120 KB Output isn't correct
13 Halted 0 ms 0 KB -