Submission #998508

#TimeUsernameProblemLanguageResultExecution timeMemory
998508Neco_arcHarbingers (CEOI09_harbingers)C++17
100 / 100
70 ms26452 KiB
#include <bits/stdc++.h> //#include <bits/debug.hpp> #define ll long long #define all(x) x.begin(), x.end() #define Neco "Harbingers" #define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin()) #define getbit(x,i) ((x >> i)&1) #define _left id * 2, l, mid #define _right id * 2 + 1, mid + 1, r #define cntbit(x) __builtin_popcountll(x) #define fi(i, a, b) for(int i = a; i <= b; i++) #define fid(i, a, b) for(int i = a; i >= b; i--) #define maxn (int) 1e5 + 7 using namespace std; const ll mod = 1e9 + 7; //972663749 const ll base = 911382323; int n; vector<pair<int, ll>> edges[maxn]; pair<ll, ll> a[maxn]; ll h[maxn], dp[maxn]; struct roll { pair<ll, ll> val; long double x; int siz; }; struct CHT { int N = 0; pair<ll, ll> P[maxn]; long double X[maxn]; long double inter(pair<ll, ll> x, pair<ll, ll> y) { return 1. * (y.second - x.second) / (x.first - y.first); } void Change(roll x) { P[N] = x.val, X[N] = x.x; N = x.siz; } void watch() { fi(i, 1, N) cout << P[i].first << " " << P[i].second << '\n'; cout << '\n'; } roll Add(pair<ll, ll> p) { p.first *= -1, p.second *= -1; int oldN = N, vt = 1; if(N > 0) { int l = 1, r = N; while(l <= r) { int mid = (l + r) >> 1; if(inter(p, P[mid]) < X[mid]) r = mid - 1; else l = mid + 1; } vt = l; } roll x = {P[vt], X[vt], oldN}; N = vt; P[N] = p; X[N] = N == 1 ? -1e18 : inter(p, P[N - 1]); return x; } ll get(ll x) { if(!N) return 1e18; int it = upper_bound(X + 1, X + 1 + N, x) - X - 1; return -(P[it].first * x + P[it].second); } } Cht; void dfs(int u, int par) { if(u != 1) dp[u] = h[u] * a[u].second + a[u].first + Cht.get(a[u].second); roll x = Cht.Add({ -h[u], dp[u] }); for(auto [v, w] : edges[u]) if(v != par) { h[v] = h[u] + w; dfs(v, u); } Cht.Change(x); } void solve() { cin >> n; fi(i, 1, n - 1) { int x, y, w; cin >> x >> y >> w; edges[x].push_back({y, w}); edges[y].push_back({x, w}); } fi(i, 2, n) cin >> a[i].first >> a[i].second; dfs(1, 0); fi(i, 2, n) cout << dp[i] << " "; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(Neco".inp", "r")) { freopen(Neco".inp", "r", stdin); freopen(Neco".out", "w", stdout); } int nTest = 1; // cin >> nTest; while(nTest--) { solve(); } return 0; }

Compilation message (stderr)

harbingers.cpp: In function 'int main()':
harbingers.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...