Submission #1088218

#TimeUsernameProblemLanguageResultExecution timeMemory
1088218mispertionFireworks (APIO16_fireworks)C++14
7 / 100
7 ms12124 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...