Submission #966516

#TimeUsernameProblemLanguageResultExecution timeMemory
966516steveonalexFireworks (APIO16_fireworks)C++17
100 / 100
181 ms101456 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define ALL(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;} ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return mask & (-mask);} ll pop_cnt(ll mask){return __builtin_popcountll(mask);} ll ctz(ll mask){return __builtin_ctzll(mask);} ll clz(ll mask){return __builtin_clzll(mask);} ll logOf(ll mask){return 63 - clz(mask);} template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b){a = b; return true;} return false; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b){a = b; return true;} return false; } template <class T> void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){ for(auto i: a) out << i << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct SlopeTrick{ ll coeff, init_slope; priority_queue<ll> slope_point; SlopeTrick(){ init_slope = 0; coeff = 0; } }; const int N = 3e5 + 69; const ll INF = 1e18 + 69; int n, m; vector<pair<int,int>> graph[N]; SlopeTrick dp[N]; void dfs(int u){ if (u > n){ return; } for(pair<int, int> v: graph[u]){ dfs(v.first); SlopeTrick child_slope; if (v.first > n){ child_slope.coeff = v.second; child_slope.init_slope = -1; child_slope.slope_point.push(v.second); child_slope.slope_point.push(v.second); } else{ swap(child_slope, dp[v.first]); child_slope.coeff += v.second; ll x = child_slope.slope_point.top(); child_slope.slope_point.pop(); ll y = child_slope.slope_point.top(); child_slope.slope_point.pop(); child_slope.slope_point.push(x + v.second); child_slope.slope_point.push(y + v.second); } dp[u].coeff += child_slope.coeff; dp[u].init_slope += child_slope.init_slope; if (child_slope.slope_point.size() > dp[u].slope_point.size()) swap(child_slope.slope_point, dp[u].slope_point); while(child_slope.slope_point.size()){ dp[u].slope_point.push(child_slope.slope_point.top()); child_slope.slope_point.pop(); } } while(dp[u].slope_point.size() > (1 - dp[u].init_slope)) dp[u].slope_point.pop(); } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 2; i<=n+m; ++i){ int p, c; cin >> p >> c; graph[p].push_back({i, c}); } dfs(1); SlopeTrick ans = dp[1]; ll x = ans.coeff; ll slope = ans.init_slope; vector<ll> slope_point; while(ans.slope_point.size()) { slope_point.push_back(ans.slope_point.top()); ans.slope_point.pop(); } slope_point.push_back(0); reverse(ALL(slope_point)); for(int i = 1; i<slope_point.size(); ++i){ ll dx = slope_point[i] - slope_point[i-1]; x += slope * dx; slope++; if (slope == 0) break; } cout << x << "\n"; // for(int i = 0; i<N; ++i) minimize(ans, dp[1][i]); // const int N = 41; // int f[N], g[N], c[N]; // for(int i = 0; i<N; ++i) f[i] = abs(i-13) + abs(i-7); // for(int i = 0; i<N; ++i) g[i] = abs(i - 3); // memset(c, 63, sizeof c); // for(int i = 0; i<N; ++i) for(int j = 0; i + j < N; ++j) // minimize(c[i + j], g[i] + f[j]); // for(int i= 0; i<N; ++i) cout << c[i] << " "; cout << "\n"; return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:93:36: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   93 |     while(dp[u].slope_point.size() > (1 - dp[u].init_slope)) dp[u].slope_point.pop();
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:118:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for(int i = 1; i<slope_point.size(); ++i){
      |                    ~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...