제출 #767933

#제출 시각아이디문제언어결과실행 시간메모리
767933hafoHarbingers (CEOI09_harbingers)C++14
0 / 100
82 ms26436 KiB
#include <bits/stdc++.h> #define ll long long #define ull unsigned ll #define pb push_back #define pa pair<int, int> #define pall pair<ll, int> #define fi first #define se second #define TASK "test" #define Size(x) (int) x.size() #define all(x) x.begin(), x.end() using namespace std; template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;} template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;} const int MOD = 1e9 + 7; const int LOG = 20; const int maxn = 1e5 + 7; const ll oo = 1e18 + 69; int n, s[maxn], p[maxn], u, v, w; vector<pa> g[maxn]; ll dp[maxn], f[maxn]; struct line { ll m, b; }; double inter(line &c, line &d) { return (double) (d.b - c.b) / (double) (c.m - d.m); } struct cht { struct opr { line ow; int sz, pos; }; stack<opr> undo_list; line lines[maxn]; int sz = 1; bool bad(line &a, line &b, line &c) { return inter(a, c) > inter(a, b); } ll calc(line a, ll x) { return a.m * x + a.b; } ll query(ll x) { ll res = 0; int l = 0, r = sz - 1, mid; while(l < r) { mid = l + r >> 1; ll s1 = calc(lines[mid], x); ll s2 = calc(lines[mid + 1], x); mini(res, min({s1, s2})); if(s1 < s2) { r = mid; } else { l = mid + 1; } } return res; } void add(line new_line) { int l = 1, r = sz - 1, mid, res = sz; while(l <= r) { mid = l + r >> 1; if(bad(lines[mid - 1], lines[mid], new_line)) r = mid - 1; else { res = mid; l = mid + 1; } } undo_list.push({lines[res], sz, res}); lines[res] = new_line; sz = res + 1; } void undo() { auto top = undo_list.top(); undo_list.pop(); lines[top.pos] = top.ow; sz = top.sz; } } cht; void dfs(int u, int par) { for(auto e:g[u]) { int v = e.fi, w = e.se; if(v == par) continue; f[v] = f[u] + w; dp[v] = f[v] * s[v] + p[v] + cht.query(s[v]); cht.add({-f[v], dp[v]}); dfs(v, u); cht.undo(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen(TASK".inp", "r", stdin); //freopen(TASK".out", "w", stdout); cin>>n; for(int i = 1; i < n; i++) { cin>>u>>v>>w; g[u].pb({v, w}); g[v].pb({u, w}); } for(int i = 2; i <= n; i++) cin>>p[i]>>s[i]; cht.lines[0] = {0, 0}; dfs(1, 0); for(int i = 2; i <= n; i++) cout<<dp[i]<<" "; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

harbingers.cpp: In member function 'long long int cht::query(long long int)':
harbingers.cpp:55:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |             mid = l + r >> 1;
      |                   ~~^~~
harbingers.cpp: In member function 'void cht::add(line)':
harbingers.cpp:71:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   71 |             mid = l + r >> 1;
      |                   ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...