Submission #1168323

#TimeUsernameProblemLanguageResultExecution timeMemory
1168323vibeduckSolar Storm (NOI20_solarstorm)C++20
100 / 100
549 ms226504 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define all(x) (x).begin(), (x).end() typedef long double ld; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<vector<int>> vvi; typedef vector<vector<bool>> vvb; typedef vector<vector<ll>> vvll; typedef vector<string> vs; typedef vector<vector<string>> vvs; typedef vector<char> vc; typedef vector<vector<char>> vvc; typedef map<int, int> mii; typedef unordered_map<int, int> umii; const int mod = 1e9 + 7; const int inf = INTMAX_MAX; const bool tc = false; ll n, s, k; const int mxn = 1e6 + 5; ll p[mxn], vp[mxn]; pii bounds[mxn]; int up[mxn][21]; ll sroot[mxn]; vi adj[mxn]; ll sv(ll l, ll r) { return (ll)vp[r + 1] - (ll)vp[l]; } ll sex(ll st) { ll ans = (ll)sv(bounds[st].fi, bounds[st].se); if (bounds[up[st][0]].fi <= bounds[st].se) ans -= (ll)sv(bounds[up[st][0]].fi, bounds[st].se); return (ll)ans; } void dfs(int node, int prev) { if (node != n) sroot[node] = (ll)sroot[prev] + (ll)sex(node); for (auto &neighbour : adj[node]) { if (neighbour == prev) continue; dfs(neighbour, node); } } int kanc(int node, int k) { for (int i = 0; i < 21; i++) { if (k & (1 << i)) node = up[node][i]; } return node; } inline void solve() { // input cin >> n >> s >> k; for (int i = 0; i < n - 1; i++) cin >> p[i + 1]; for (int i = 1; i < n; i++) p[i] += p[i - 1]; for (int i = 0; i < n; i++) cin >> vp[i + 1]; for (int i = 1; i <= n; i++) vp[i] += vp[i - 1]; // calculate bounds for (int t = 0; t < n; t++) { int lo = t; int hi = n; while (hi > lo + 1) { int mid = (lo + hi) / 2; if (p[mid] - p[t] > k) hi = mid; else lo = mid; } int r = lo; int l; if (p[t] <= k) l = 0; else { int lo = 0; int hi = t; while (hi > lo + 1) { int mid = (lo + hi) / 2; if (p[t] - p[t-mid] > k) hi = mid; else lo = mid; } l = t-lo; } bounds[t] = {l, r}; //cout << t << " bounds " << l << " -> " << r << '\n'; } bounds[n] = {1e9, 1e9}; // find next segment for (int st = 0; st < n; st++) { int nxt; if (bounds[st].se != n - 1 && bounds[st + 1].fi <= bounds[st].se + 1) { int lo = st; int hi = n; while (hi > lo + 1) { int mid = (lo + hi) / 2; if (bounds[mid].fi <= bounds[st].se + 1) lo = mid; else hi = mid; } nxt = lo; } else { nxt = n; } up[st][0] = nxt; } for (int i = 0; i < n; i++) { adj[up[i][0]].pb(i); adj[i].pb(up[i][0]); } dfs(n, n); for (int i = 0; i < 21; i++) { up[n][i] = n; } for (int l = 1; l < 21; l++) { for (int i = 0; i < n; i++) { up[i][l] = up[up[i][l - 1]][l - 1]; } } sroot[n] = 0; pair<ll, int> ans = {-100, -1}; for (int i = 0; i < n; i++) { //cout << "look at " << i+1 << '\n'; //cout << "direct par is " << up[i][0] + 1 << '\n'; int high = kanc(i, s - 1); //cout << "max till is " << high+1 << '\n'; pair<ll, int> cur = {-100, i}; if (high == n) { //cout << "case1\n"; //cout << "val: " << sroot[i] - sex(n - 1) + sv(bounds[n - 1].fi, bounds[n - 1].se) << '\n'; cur = max(cur, {sroot[i] - sex(n - 1) + sv(bounds[n - 1].fi, bounds[n - 1].se), i}); } else { //cout << "case2\n"; //cout << sroot[i] << " - " << sroot[up[high][0]] << " - " << sex(high) << " + " << sv(bounds[high].fi, bounds[high].se) << '\n'; //cout << "val: " << sroot[i] - sroot[up[high][0]] - sex(high) + sv(bounds[high].fi, bounds[high].se) << '\n'; cur = max(cur, {sroot[i] - sroot[up[high][0]] - sex(high) + sv(bounds[high].fi, bounds[high].se), i}); } //cout << i + 1 << " gave us " << cur.fi << '\n'; //cout << '\n'; ans = max(ans, cur); } vi answer; int cur = ans.se; for (int i = 0; i < s; i++) { if (cur == n) break; answer.pb(cur); cur = up[cur][0]; } cout << answer.size() << '\n'; for (auto &x : answer) cout << x + 1 << " "; cout << '\n'; } void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } signed main() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); //setIO(); int t = 1; if (tc) { cin >> t; } while (t--) { solve(); } }

Compilation message (stderr)

SolarStorm.cpp:29:17: warning: overflow in conversion from 'long int' to 'int' changes value from '9223372036854775807' to '-1' [-Woverflow]
   29 | const int inf = INTMAX_MAX;
      |                 ^~~~~~~~~~
SolarStorm.cpp: In function 'void setIO(std::string)':
SolarStorm.cpp:175:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  175 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SolarStorm.cpp:176:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...