(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 #1112872

#TimeUsernameProblemLanguageResultExecution timeMemory
1112872InvMODHarbingers (CEOI09_harbingers)C++14
80 / 100
135 ms50036 KiB
#include <bits/stdc++.h> #define dbg(x) "[" #x " = " << (x) << "]" #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define sz(v) (int)(v).size() using namespace std; using ll = long long; const int N = 1e5; 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 = -1; vector<vector<pair<Line, int>>> st; int trsz; LichaoOptMem(): trsz(0), global_version(0), compress(0), st(0), rekt(0) {} void init(int n, vector<int>& comp){ trsz = sz(comp)-1; st.resize(sz(comp)+1); for(int v : comp) compress.push_back(v); global_version = -1; rekt.assign(n+1, false); return; } void update(int l, int r, Line curLine){ if(l > r) return; assert(!rekt[global_version]); if(l == r){ while(sz(st[l]) && rekt[st[l].back().second]) st[l].pop_back(); if(!sz(st[l]) || 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; while(sz(st[mid]) && rekt[st[mid].back().second]) st[mid].pop_back(); if(!sz(st[mid])) return st[mid].push_back(make_pair(curLine, global_version)), void(); 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(st[mid].back().first != version) 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; while(sz(st[mid]) && rekt[st[mid].back().second]) st[mid].pop_back(); ll cur = (st[mid].size() ? st[mid].back().first.f(x) : 0); 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){ ++global_version; update(0, trsz, Line(a, b)); return global_version; } void rmvLine(int version){ rekt[version] = true; return; } }; int speed[N]; ll dp[N], cur_dist[N]; pair<int,int> E[N]; basic_string<int> adj[N]; LichaoOptMem lichao; void dfs(int x){ if(x) dp[x] += lichao.query(speed[x]) + (1ll * cur_dist[x] * speed[x]); int rem_version = lichao.addLine(-cur_dist[x], dp[x]); for(int id : adj[x]){ int v = E[id].first ^ x, w = E[id].second; if(v && !cur_dist[v]){ cur_dist[v] = cur_dist[x] + 1ll * w; dfs(v); } } lichao.rmvLine(rem_version); return; } void solve() { int n; cin >> n; FOR(i, 1, n-1){ int u,v,w; cin >> u >> v >> w; u--, v--; E[i] = make_pair(u^v, w); adj[u].push_back(i); adj[v].push_back(i); } vector<int> compress; FOR(i, 1, n-1){ cin >> dp[i] >> speed[i]; compress.push_back(speed[i]); } sort(compress.begin(), compress.end()); compress.erase(unique(compress.begin(), compress.end()), compress.end()); lichao.init(n, compress); dfs(0); FOR(i, 1, n-1) cout << dp[i] <<" \n"[i == n-1]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "InvMOD" if(fopen(name".INP", "r")){ freopen(name".INP","r",stdin); freopen(name".OUT","w",stdout); } int t = 1; //cin >> t; while(t--) solve(); return 0; }

Compilation message (stderr)

harbingers.cpp: In constructor 'LichaoOptMem::LichaoOptMem()':
harbingers.cpp:30:45: warning: 'LichaoOptMem::trsz' will be initialized after [-Wreorder]
   30 |     vector<vector<pair<Line, int>>> st; int trsz;
      |                                             ^~~~
harbingers.cpp:28:31: warning:   'int LichaoOptMem::global_version' [-Wreorder]
   28 |     vector<int> compress; int global_version = -1;
      |                               ^~~~~~~~~~~~~~
harbingers.cpp:32:5: warning:   when initialized here [-Wreorder]
   32 |     LichaoOptMem(): trsz(0), global_version(0), compress(0), st(0), rekt(0) {}
      |     ^~~~~~~~~~~~
harbingers.cpp:28:31: warning: 'LichaoOptMem::global_version' will be initialized after [-Wreorder]
   28 |     vector<int> compress; int global_version = -1;
      |                               ^~~~~~~~~~~~~~
harbingers.cpp:28:17: warning:   'std::vector<int> LichaoOptMem::compress' [-Wreorder]
   28 |     vector<int> compress; int global_version = -1;
      |                 ^~~~~~~~
harbingers.cpp:32:5: warning:   when initialized here [-Wreorder]
   32 |     LichaoOptMem(): trsz(0), global_version(0), compress(0), st(0), rekt(0) {}
      |     ^~~~~~~~~~~~
harbingers.cpp:30:37: warning: 'LichaoOptMem::st' will be initialized after [-Wreorder]
   30 |     vector<vector<pair<Line, int>>> st; int trsz;
      |                                     ^~
harbingers.cpp:26:18: warning:   'std::vector<bool> LichaoOptMem::rekt' [-Wreorder]
   26 |     vector<bool> rekt;
      |                  ^~~~
harbingers.cpp:32:5: warning:   when initialized here [-Wreorder]
   32 |     LichaoOptMem(): trsz(0), global_version(0), compress(0), st(0), rekt(0) {}
      |     ^~~~~~~~~~~~
harbingers.cpp: In member function 'void LichaoOptMem::update(int, int, LichaoOptMem::Line)':
harbingers.cpp:53:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   53 |             int mid = l+r>>1;
      |                       ~^~
harbingers.cpp: In member function 'll LichaoOptMem::query(int, int, ll)':
harbingers.cpp:70:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |         int mid = l+r>>1;
      |                   ~^~
harbingers.cpp: In function 'int main()':
harbingers.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:157:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...