Submission #1063778

#TimeUsernameProblemLanguageResultExecution timeMemory
1063778trMatherzHarbingers (CEOI09_harbingers)C++17
10 / 100
1065 ms22612 KiB
#include <iostream> // #include <fstream> // std::ifstream cin ("pushabox.in"); // std::ofstream cout ("pushabox.out"); // includes #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <vector> #include <array> #include <algorithm> #include <numeric> #include <iomanip> #include <unordered_set> #include <stack> #include <ext/pb_ds/assoc_container.hpp> #include <random> #include <chrono> #include <bitset> #include <complex> // usings using namespace std; using namespace __gnu_pbds; // misc #define ll long long #define ld long double #define pb push_back #define pq priority_queue #define ub upper_bound #define lb lower_bound template <typename T, typename U> bool emin(T &a, const U &b) { return b < a ? a = b, true : false; } template <typename T, typename U> bool emax(T &a, const U &b) { return b > a ? a = b, true : false; } typedef uint64_t hash_t; // vectors #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vpii vector<pair<int, int>> #define vvpii vector<vector<pair<int, int>>> #define vppipi vector<pair<int, pair<int, int>>> #define vl vector<ll> #define vvl vector<vl> #define vvvl vector<vvl> #define vpll vector<pair<ll, ll>> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define vvvb vector<vvb> #define vs vector<string> #define sz(x) (int)x.size() #define rz resize #define all(x) x.begin(), x.end() #define vc vector<char> #define vvc vector<vc> // pairs #define pii pair<int, int> #define pll pair<ll, ll> #define mp make_pair #define f first #define s second // sets #define si set<int> #define sl set<ll> #define ss set<string> #define in insert template <class T> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // maps #define mii map<int, int> #define mll map<ll, ll> // loops #define FR(x, z, y) for (int x = z; x < y; x++) #define FRE(x, z, y) FR(x, z, y + 1) #define F(x, y) FR(x, 0, y) #define FE(x, y) F(x, y + 1) #define A(x, y) for (auto &x : y) ll isect(pll a, pll b) { return (a.s - b.s) / (b.f - a.f); } int n; vl d, dp; vvpll a; vpll b, c; int sz = 1; int add(ll k, ll m){ if(sz == 1 || isect(c[sz - 2], c[sz - 1]) <= isect(c[sz - 1], {k, m})) return sz; int l = 1, r = sz - 1; while(l < r) { int m = (l + r) / 2; if(isect(c[m - 1], c[m]) > isect(c[m], {k, m})) r = m; else l = m + 1; } // return l; for(int i = sz; i > 1; i--) { if(isect(c[i - 1], c[i - 2]) < isect(c[i - 1], {k, m})) { assert(l <= i); return i; } } return 1; } ll get(ll x){ int l = 1, r = sz - 1; while(l < r){ int m = (l + r - 1) / 2; if(isect(c[m], c[m + 1]) <= x) l = m + 1; else r = m; } return c[l].f * x + c[l].s; } void dfs(int x, int p){ int pos, prev = sz; pll past; if(p != -1) { dp[x] = b[x].f + b[x].s * d[x] + min(get(b[x].s), 0ll); pos = add(-d[x], dp[x]); past = c[pos]; c[pos] = { -d[x], dp[x] }; sz = pos + 1; } A(u, a[x]) { if(u.f == p) continue; d[u.f] = d[x] + u.s; dfs(u.f, x); } if(p != -1) { sz = prev; c[pos] = past; } } int main() { cin >> n; a = vvpll(n); b = c = vpll(n, {(ll) 1e9, (ll) 1e9}); c[0] = {0, 0}; d = dp = vl(n, 0); F(i, n - 1){ ll x, y, z; cin >> x >> y >> z; x--; y--; a[x].pb({y, z}); a[y].pb({x, z}); } F(i, n - 1) cin >> b[i + 1].f >> b[i + 1].s; dfs(0, -1); F(i, n - 1) cout << dp[i + 1] << " "; }
#Verdict Execution timeMemoryGrader output
Fetching results...