Submission #475213

#TimeUsernameProblemLanguageResultExecution timeMemory
475213Vladth11Fireworks (APIO16_fireworks)C++14
7 / 100
6 ms7372 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define debug(x) cerr << #x << " " << x << "\n" #define debugs(x) cerr << #x << " " << x << " " using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef pair <ll, ll> pii; typedef pair <long double, pii> muchie; const ll NMAX = 100001; const ll VMAX = 1000001; const ll INF = (1LL << 59); const ll MOD = 998244353; const ll BLOCK = 318; const ll base = 31; const ll nr_of_bits = 21; vector <pii> v[NMAX]; int n, m; multiset <int> st[NMAX]; int slope[NMAX]; int schema[NMAX]; void shift(int node, int d) { int act = slope[node]; int last = 0; while(act > 0) { auto it = st[node].rbegin(); last = *it; st[node].erase(st[node].find(*it)); act--; } int celalalt = *st[node].rbegin(); vector <int> elem; elem.push_back(last + d); elem.push_back(celalalt + d); for(auto x : st[node]){ if(x + d <= celalalt) elem.push_back(x + d); } st[node].clear(); for(auto x : elem) st[node].insert(x); slope[node] = 1; } void merge(multiset <int> &a, multiset <int> &b) { if(a.size() < b.size()) { swap(a, b); } for(auto x : b) { a.insert(x); } b.clear(); } void DFS(int node, int p) { if(v[node].size() == 1 && node != 1) { st[node].insert(0); st[node].insert(0); slope[node] = 1; return; } for(auto y : v[node]) { int x = y.first; int d = y.second; if(x == p) continue; DFS(x, node); shift(x, d); slope[node] += slope[x]; schema[node] += schema[x]; merge(st[node], st[x]); } //debugs(node); //debug(slope[node]); //for(auto x : st[node]) // debug(x); //debug("next"); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll sum = 0; int i; cin >> n >> m; for(i = 2; i <= n + m; i++) { int p, l; cin >> p >> l; v[i].push_back({p, l}); v[p].push_back({i, l}); sum += 1LL * l; } DFS(1, 0); int last; while(slope[1] > 0) { auto it = st[1].rbegin(); last = *it; st[1].erase(st[1].find(*it)); slope[1]--; } /// Acum cel din spate e primul cu slope 0 int panta = 0; vector <pii> v; v.push_back({0, 0}); for(auto x : st[1]){ if(x == 0){ continue; } if(v.size() == 0 || v.back().first != x){ v.push_back({x, 1}); }else{ v.back().second++; } panta++; } //debug(panta); for(int i = 1; i < v.size(); i++){ sum -= 1LL * panta * (v[i].first - v[i - 1].first); // debug(panta); // debug(v[i].first); // debug(v[i - 1].first); panta -= v[i].second; } cout << sum; return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'int main()':
fireworks.cpp:123:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |     for(int i = 1; i < v.size(); i++){
      |                    ~~^~~~~~~~~~
fireworks.cpp:100:9: warning: variable 'last' set but not used [-Wunused-but-set-variable]
  100 |     int last;
      |         ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...