Submission #1269678

#TimeUsernameProblemLanguageResultExecution timeMemory
1269678steveonalexHarbingers (CEOI09_harbingers)C++20
90 / 100
130 ms34308 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1ULL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll lcm(ll a, ll b){return a / gcd(a, b) * b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ull mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} double rngesus_d(double l, double r){ double wow = (double) ((ull) rng()) / ((ull)(0-1)); return wow * (r - l) + l; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct Line{ ll a, b; Line(ll a = 0, ll b = 0): a(a), b(b){} }; ll f(Line cur, ll x){ return cur.a * x + cur.b; } struct LichaoTree{ int n; vector<ll> b; vector<Line> a; vector<pair<int, Line>> history; LichaoTree(vector<ll> _b){ b = _b; n = b.size() - 1; a.resize(n * 2 + 2, Line(1e9, 1e18)); } int get_version(){ return history.size(); } void update(Line x){ update(x, 1, n, 1); } void update(Line x, int l, int r, int id){ if (f(a[id], b[l]) <= f(x, b[l]) && f(a[id], b[r]) <= f(x, b[r])) return; if (f(a[id], b[l]) >= f(x, b[l]) && f(a[id], b[r]) >= f(x, b[r])) { history.push_back({id, a[id]}); a[id] = x; return; } int mid = (l + r) >> 1; update(x, l, mid, id + 1); update(x, mid+1, r, id + (mid - l + 1) * 2); } ll get(ll x){ return get(x, 1, n, 1); } ll get(ll x, int l, int r, int id){ ll ans = f(a[id], x); if (l == r) return ans; int mid = (l + r) >> 1; if (b[mid] >= x) minimize(ans, get(x, l, mid, id + 1)); else minimize(ans, get(x, mid+1, r, id + (mid - l + 1) * 2)); return ans; } void roll_back(int version){ while(get_version() > version){ auto i = history.back(); history.pop_back(); a[i.first] = i.second; } } }; const int N = 1e5 + 69; int n; vector<pair<ll, ll>> graph[N]; ll h[N]; ll dp[N]; vector<ll> s, v; void dfs(int u, int p, LichaoTree &lct){ int cur_version = lct.get_version(); if (u != 1){ dp[u] = s[u] + lct.get(v[u]) + v[u] * h[u]; } lct.update(Line(-h[u], dp[u])); for(pair<ll, ll> v: graph[u]) if (v.first != p){ h[v.first] = h[u] + v.second; dfs(v.first, u, lct); } lct.roll_back(cur_version); } void solve(){ cin >> n; for(int i = 1; i < n; ++i){ int u, v, d; cin >> u >> v >> d; graph[u].push_back({v, d}); graph[v].push_back({u, d}); } s.resize(n+1); v.resize(n+1); for(int i = 2; i <= n; ++i) cin >> s[i] >> v[i]; vector<ll> b = v; remove_dup(b); LichaoTree lct(b); dfs(1, 0, lct); for(int i = 2; i <= n; ++i) cout << dp[i] << " "; cout << "\n"; } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t cock = clock(); solve(); cerr << "Time elapsed: " << clock() - cock << "ms!\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...