제출 #1124087

#제출 시각아이디문제언어결과실행 시간메모리
1124087luvnaHarbingers (CEOI09_harbingers)C++20
100 / 100
89 ms27464 KiB
#include<bits/stdc++.h> #define MASK(i) (1 << (i)) #define pub push_back #define all(v) v.begin(), v.end() #define compact(v) v.erase(unique(all(v)), end(v)) #define pii pair<int,int> #define fi first #define se second #define endl "\n" #define sz(v) (int)(v).size() #define dbg(x) "[" #x " = " << (x) << "]" using namespace std; template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;} template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;} typedef long long ll; typedef long double ld; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);} const int N = 2e5 + 15; const ll INF = 1e18; int n; vector<pii> g[N]; int wait[N], speed[N]; struct line{ ll slope, c; line() {} line(ll slope, ll c) : slope(slope), c(c) {} ll f(ll x) {return slope*x + c;} }; struct CHT{ int top = 0; vector<line> lines; stack<pair<pii, line>> st; void luvna(int n){ lines.resize(n + 15, line(0,0)); } bool dump(line d1, line d2, line d3){ return 1.0l*(d1.c - d2.c) / (1.0l * (d2.slope - d1.slope)) <= 1.0l*(d2.c - d3.c) / (1.0l * (d3.slope - d2.slope)); } ll query(ll x){ int l = 0, r = top - 1; while(l < r){ int mid = (l+r) >> 1; if(lines[mid].f(x) >= lines[mid+1].f(x)) l = mid+1; else r = mid; } return lines[l].f(x); } void add(line d){ int l = 1, r = top - 1, pos = top; while(l <= r){ int mid = (l+r) >> 1; if(dump(d, lines[mid], lines[mid-1])){ pos = mid; r = mid-1; } else l = mid+1; } st.push({pii(pos,top), lines[pos]}); top = pos + 1; lines[pos] = d; } void rollback(){ assert(!st.empty()); int pos = st.top().fi.fi; top = st.top().fi.se; line d = st.top().se; lines[pos] = d; st.pop(); } void print(){ for(int i = 0; i < top; i++){ cout << lines[i].slope << " " << lines[i].c << endl; } cout << endl; } }; int h[N]; ll dp[N]; CHT solver; void dfs(int u, int p){ //cout << solver.top << endl; if(u > 1){ //cout << solver.top << endl; //cout << dbg(u) << endl; //solver.print(); dp[u] = 1LL*speed[u]*h[u] + wait[u] + solver.query(speed[u]); } solver.add(line(-h[u], dp[u])); //solver.print(); for(auto [v, w] : g[u]) if(v != p){ h[v] = h[u] + w; dfs(v,u); } solver.rollback(); } void solve(){ cin >> n; for(int i = 1; i < n; i++){ int u, v, w; cin >> u >> v >> w; g[u].push_back({v,w}); g[v].push_back({u,w}); } for(int i = 2; i <= n; i++) cin >> wait[i] >> speed[i]; solver.luvna(n); dfs(1,-1); for(int i = 2; i <= n; i++) cout << dp[i] << " "; } signed main(){ ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); #define task "task" if(fopen(task".INP", "r")){ freopen(task".INP", "r", stdin); freopen(task".OUT", "w", stdout); } int t; t = 1; //cin >> t; while(t--) solve(); } /*103 251 142 722*/ /* 6 1 2 50 2 3 28 3 4 63 3 5 52 5 6 90 3 2 8 5 1 1 3 9 8 9 */

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

harbingers.cpp: In function 'int main()':
harbingers.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...