(UPD: 2024-12-04 14:48 UTC) Judge is not working due to Cloudflare incident. (URL) We can do nothing about it, sorry. After the incident is resolved, we will grade all submissions.

Submission #1112815

#TimeUsernameProblemLanguageResultExecution timeMemory
1112815InvMODHarbingers (CEOI09_harbingers)C++14
50 / 100
175 ms62264 KiB
#include <bits/stdc++.h> #define FOR(i, a, b) for(int i = (a); i <= (b); i++) using namespace std; using ll = long long; const int N = 1e5+1; struct LichaoOptMem{ struct Line{ int a; ll b; Line(int a = 0, ll b = LLONG_MAX): a(a), b(b) {} ll f(ll x){return 1ll*a*x + b;} ll slope(){return a;} bool operator != (const Line& x) const{ return (a != x.a || b != x.b); } }; vector<bool> rekt; vector<int> compress; int global_version = 0; vector<vector<pair<Line, int>>> st; int trsz; LichaoOptMem(int n = 0, vector<int> comp = vector<int>()): trsz(n){ compress = comp; rekt.resize(n+1); st.assign(n+1, vector<pair<Line,int>>(1, make_pair(Line(), 0))); return; } void roll_back(vector<pair<Line,int>>& tr){ while(rekt[tr.back().second]) tr.pop_back(); return; } void update(int l, int r, Line curLine){ if(l > r) return; if(l == r){ if(st[l].back().first.f(compress[l]) > curLine.f(compress[l])) return st[l].push_back(make_pair(curLine, global_version)), void(); } else{ int mid = l+r>>1; roll_back(st[mid]); Line version = st[mid].back().first; if(version.f(compress[mid]) > curLine.f(compress[mid])) swap(curLine, version); if(version.slope() < curLine.slope()) update(l, mid-1, curLine); if(version.slope() > curLine.slope()) update(mid+1, r, curLine); if(version != st[mid].back().first) st[mid].push_back(make_pair(version, global_version)); } return; } ll query(int l, int r, ll x){ if(l > r) return LLONG_MAX; int mid = l+r>>1; roll_back(st[mid]); ll cur = st[mid].back().first.f(x); if(x == compress[mid]) return cur; if(x < compress[mid]) return min(cur, query(l, mid-1, x)); if(x > compress[mid]) return min(cur, query(mid+1, r, x)); return -1; } ll query(ll x){ return query(0, trsz, x); } int addLine(int a, ll b){return ++global_version, update(0, trsz, Line(a, b)), global_version;} void rmvLine(int version){return rekt[version] = true, void();} }; ll dp[N]; vector<pair<int,int>> adj[N]; void dfs(int x, int p, ll cur_dist, LichaoOptMem& lichao, vector<int>& speed){ if(x) dp[x] += lichao.query(speed[x]) + (1ll * cur_dist * speed[x]); int rem_version = lichao.addLine(-cur_dist, dp[x]); for(pair<int,int> e : adj[x]){ int v = e.first, w = e.second; if(v != p){ dfs(v, x, cur_dist + 1ll*w, lichao, speed); } } lichao.rmvLine(rem_version); return; } void solve() { int n; cin >> n; vector<int> speed(n); FOR(i, 1, n-1){ int u,v,w; cin >> u >> v >> w; u--, v--; adj[u].emplace_back(v, w); adj[v].emplace_back(u, w); } vector<int> compress; FOR(i, 1, n-1){ cin >> dp[i] >> speed[i]; compress.push_back(speed[i]); } sort(compress.begin(), compress.end()); LichaoOptMem lichao((int)compress.size()-1, compress); dfs(0, -1, 0, lichao, speed); FOR(i, 1, n-1) cout << dp[i] <<" \n"[i == n-1]; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }

Compilation message (stderr)

harbingers.cpp: In member function 'void LichaoOptMem::update(int, int, LichaoOptMem::Line)':
harbingers.cpp:46:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |             int mid = l+r>>1;
      |                       ~^~
harbingers.cpp: In member function 'll LichaoOptMem::query(int, int, ll)':
harbingers.cpp:59:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |         int mid = l+r>>1;
      |                   ~^~
#Verdict Execution timeMemoryGrader output
Fetching results...