Submission #1025534

#TimeUsernameProblemLanguageResultExecution timeMemory
1025534SebFireworks (APIO16_fireworks)C++17
100 / 100
198 ms84896 KiB
//#pragma GCC optimize("Ofast") //#pragma GCC target("avx,avx2,fma") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,sse4a,avx,avx2,popcnt,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> //order_of_key = menores, find_by_order = consultar indexado en 0, regresa iterador using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vll = vector<ll>; using vii = vector<int>; mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count()); #define mid ((l+r)>>1) #define f first #define s second #define pb push_back #define MP make_pair #define all(x) (x).begin(),(x).end() #define inicio(x) (*(x).begin()) #define rinicio(x) (*(x).rbegin()) #define SZ(x) (int)(x.size()) const ll MAXN = (ll)3e5 + 5; const ll MOD = (ll)1e9 + 7; vector<pll> g[MAXN]; priority_queue<ll> pq[MAXN]; ll sz[MAXN]; void dfs1(int nodo = 1, int p = 0) { sz[nodo] = 1; for (auto &it : g[nodo]) { if (it.f == p) continue; dfs1(it.f, nodo); sz[nodo] += sz[it.f]; } return; } void dfs2(int nodo = 1, int p = 0, ll d = 0) { pll bign = MP(-1, -1); for (auto &it : g[nodo]) { if (it.f == p) continue; if (bign.f == -1 || sz[it.f] > sz[bign.f]) bign = it; } if (bign.f != -1) { dfs2(bign.f, nodo, bign.s); swap(pq[nodo], pq[bign.f]); } else { pq[nodo].push(0); pq[nodo].push(0); } for (auto &it : g[nodo]) { if (it.f == p || it.f == bign.f) continue; dfs2(it.f, nodo, it.s); while (!pq[it.f].empty()) { pq[nodo].push(pq[it.f].top()); pq[it.f].pop(); } } for (int i = 0; i < SZ(g[nodo]) - 2; i++) pq[nodo].pop(); ll a, b; a = pq[nodo].top(); pq[nodo].pop(); b = pq[nodo].top(); pq[nodo].pop(); a += d; b += d; pq[nodo].push(a); pq[nodo].push(b); return; } void tc() { ll N, M, ans = 0, pre = 0; cin >> N >> M; for (int i = 2; i <= N + M; i++) { ll a, p; cin >> a >> p; g[i].pb(MP(a, p)); g[a].pb(MP(i, p)); ans += p; } g[0].pb(MP(1, 0)); g[1].pb(MP(0, 0)); dfs1(); dfs2(); vll v; pq[1].pop(); while (!pq[1].empty()) { v.pb(pq[1].top()); pq[1].pop(); } reverse(all(v)); for (int i = 0; i < SZ(v); i++) { ans -= (SZ(v) - i)*(v[i] - pre); pre = v[i]; } cout << ans << "\n"; return; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int t = 1; //cin >> t; for (int i = 1; i <= t; i++) { tc(); } return 0; } //checa tus constantes, n = 1? overflow?
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...