Submission #157242

#TimeUsernameProblemLanguageResultExecution timeMemory
157242PeppaPigHarbingers (CEOI09_harbingers)C++14
0 / 100
203 ms65540 KiB
#include <bits/stdc++.h> #define long long long #define pii pair<long, long> #define x first #define y second using namespace std; const int N = 1e5+5; struct PersistentLiChaoTree { struct item { pii line; item *l, *r; item(pii val, item *l, item *r) : line(val), l(l), r(r) {} }; item *ver[N]; // long f(long x, pii l) { return l.x * x + l.y; } // item* newleaf(pii val) { return new item(val, NULL, NULL); } // item* newpar(pii val, item* l, item *r) { return new item(val, l, r); } // item* update(pii k, item *p, int l = 1, int r = 1e9) { // if(!p) return newleaf(k); // if(f(l, k) < f(l, p->val) && f(r, k) < f(r, p->val)) return newpar(k, p->l, p->r); // if(f(l, k) > f(l, p->val) && f(r, k) > f(r, p->val)) return p; // int m = (l + r) >> 1; // bool lef = f(l, k) < f(l, p->val); // bool mid = f(m, k) < f(m, p->val); // if(l == r) return newleaf(mid ? k : p->val); // if(lef != mid) return newpar(mid ? k : p->val, update(mid ? p->val : k, p->l, l, m), p->r); // else return newpar(mid ? k : p->val, p->l, update(mid ? p->val : k, p->r, m+1, r)); // } // long get(int x, item *p, int l = 1, int r = 1e9) { // int m = (l + r) >> 1; // if(!p) return 1e18; // if(l == r) return f(x, p->val); // if(x < m) return min(f(x, p->val), get(x, p->l, l, m)); // else return min(f(x, p->val), get(x, p->r, m+1, r)); // } long get(pii a, long x) { return a.x * x + a.y; } item* newleaf(pii v) { return new item(v, nullptr, nullptr); } item* newpar(pii v, item* l, item *r) { return new item(v, l, r); } item* update(pii v, item *p, int l = 1, int r = 1e9) { if(p == NULL) return newleaf(v); if(get(v, l) > get(p->line, l) && get(v, r) > get(p->line, r)) return p; if(get(v, l) < get(p->line, l) && get(v, r) < get(p->line, r)) return newpar(v, p->l, p->r); pii ret = p->line; int m = l + r >> 1; if(get(ret, l) > get(v, l)) swap(ret, v); if(get(ret, m) > get(v, m)) return newpar(v, update(ret, p->l, l, m), p->r); return newpar(ret, p->l, update(v, p->r, m+1, r)); } long query(long v, item *p, int l = 1, int r = 1e9) { if(p == NULL) return 1e18; long mn = get(p->line, v); int m = l + r >> 1; if(v <= m) return min(mn, query(v, p->l, l, m)); return min(mn, query(v, p->r, m+1, r)); } } t; int n; vector<pii> g[N]; long S[N], V[N], d[N], ans[N]; void dfs(int u, int p) { ans[u] = t.query(V[u], t.ver[p]) + V[u] * d[u] + S[u]; t.ver[u] = t.update(pii(-d[u], ans[u]), t.ver[p]); for(pii v : g[u]) if(v.x != p) { d[v.x] = d[u] + v.y; dfs(v.x, u); } } int main() { scanf("%d", &n); for(int i = 1, a, b, c; i < n; i++) { scanf("%d %d %d", &a, &b, &c); g[a].emplace_back(b, c); g[b].emplace_back(a, c); } for(int i = 2; i <= n; i++) scanf("%lld %lld", S+i, V+i); t.ver[1] = t.newleaf(pii(0, 0)); dfs(1, 0); for(int i = 2; i <= n; i++) printf("%lld ", ans[i]); printf("\n"); return 0; }

Compilation message (stderr)

harbingers.cpp: In member function 'PersistentLiChaoTree::item* PersistentLiChaoTree::update(std::pair<long long int, long long int>, PersistentLiChaoTree::item*, int, int)':
harbingers.cpp:58:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int m = l + r >> 1;
                 ~~^~~
harbingers.cpp: In member function 'long long int PersistentLiChaoTree::query(long long int, PersistentLiChaoTree::item*, int, int)':
harbingers.cpp:67:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int m = l + r >> 1;
                 ~~^~~
harbingers.cpp: In function 'int main()':
harbingers.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
harbingers.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:93:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 2; i <= n; i++) scanf("%lld %lld", S+i, V+i);
                                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...