Submission #237143

#TimeUsernameProblemLanguageResultExecution timeMemory
237143xiaowuc1Harbingers (CEOI09_harbingers)C++14
100 / 100
133 ms19192 KiB
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <vector> using namespace std; // BEGIN NO SAD #define rep(i, a, b) for(int i = a; i < (b); ++i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef vector<int> vi; // END NO SAD typedef long long ll; typedef pair<int, int> pii; typedef vector<vector<ll>> matrix; typedef pair<ll, ll> pll; int n; vector<pii> edges[100005]; ll ret[100005]; ll dist[100005]; ll gain[100005]; ll timeper[100005]; pll bestlines[100005]; // bestlines.first * x + bestlines.second int numlines; ll eval(pll a, ll x) { return a.first*x + a.second; } ll qry(ll x) { assert(numlines > 0); int lhs = 0; int rhs = numlines-1; while(lhs != rhs) { int mid = (lhs+rhs)/2; if(eval(bestlines[mid], x) <= eval(bestlines[mid+1], x)) rhs = mid; else lhs = mid + 1; } ll ret = eval(bestlines[lhs], x); return ret; } bool cross(pll a, pll b, pll c){ return (1.0 * (b.second - a.second) / (a.first - b.first) >= 1.0 * (c.second - b.second) / (b.first - c.first)); } int ins(pll a) { if(numlines <= 1) return numlines; int lhs = 0; int rhs = numlines-1; while(lhs != rhs) { int mid = (lhs+rhs)/2; if(cross(bestlines[mid], bestlines[mid+1], a)) rhs = mid; else lhs = mid+1; } return lhs+1; } void dfs(int curr, int par) { if(curr > 1) { ret[curr] = gain[curr] + timeper[curr]*dist[curr] + qry(timeper[curr]); } int oldnumlines = numlines; int idx = ins(pll(-dist[curr], ret[curr])); pll save = bestlines[idx]; for(pii out: edges[curr]) { if(out.first == par) continue; numlines = idx+1; bestlines[idx] = pll(-dist[curr], ret[curr]); dist[out.first] = dist[curr] + out.second; dfs(out.first, curr); } bestlines[idx] = save; numlines = oldnumlines; } void solve() { cin >> n; for(int i = 1; i < n; i++) { int a, b, w; cin >> a >> b >> w; edges[a].emplace_back(b, w); edges[b].emplace_back(a, w); } for(int i = 2; i <= n; i++) cin >> gain[i] >> timeper[i]; dfs(1, -1); for(int i = 2; i <= n; i++) cout << ret[i] << " \n"[i == n]; } // are there edge cases (N=1?) // are array sizes proper (scaled by proper constant, for example 2* for koosaga tree) // integer overflow? // DS reset properly between test cases int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...