Submission #1280000

#TimeUsernameProblemLanguageResultExecution timeMemory
1280000tvgkFish 3 (JOI24_fish3)C++20
0 / 100
3 ms2364 KiB
#include<bits/stdc++.h> using namespace std; #define task "a" #define se second #define fi first #define ll long long #define ii pair<ll, ll> const long mxN = 3e5 + 7, nB = 550; const ll inf = 1e18; int n, l[mxN], r[mxN], q; ll dif, a[mxN], ans[mxN]; vector<int> query[mxN]; bool cmp(int u, int v) { return r[u] < r[v]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); freopen(task".INP", "r", stdin); freopen(task".OUT", "w", stdout); cin >> n >> dif; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> q; for (int i = 1; i <= q; i++) { cin >> l[i] >> r[i]; if (l[i] / nB == r[i] / nB) { ll cur = inf; for (; r[i] >= l[i]; r[i]--) { ll tmp = 0; if (a[r[i]] > cur) tmp = (a[r[i]] - cur + dif - 1) / dif; ans[i] += tmp; cur = a[r[i]] - tmp * dif; if (cur < 0) { ans[i] = -1; break; } } } else query[l[i] / nB].push_back(i); } for (int i = 0; i <= n / nB; i++) { sort(query[i].begin(), query[i].end(), cmp); ii cur = {0, 0}; ll sum = 0, mem = 0; int ctr = nB * (i + 1); stack<ii> stk; for (int id : query[i]) { while (ctr <= r[id]) { if (a[ctr] % dif < cur.se) cur.fi++; cur.se = a[ctr] % dif; int l = ctr; ll tmp = a[ctr] / dif - cur.fi; while (stk.size() && stk.top().se >= tmp) { sum += (l - stk.top().fi) * (stk.top().se - tmp); l = stk.top().fi; stk.pop(); } if (stk.empty()) mem = tmp; stk.push({l, tmp}); ctr++; } ans[id] = sum; ll cur = mem * dif + a[nB * (i + 1)] % dif; for (int j = nB * (i + 1) - 1; j >= l[id]; j--) { ll tmp = 0; if (a[j] > cur) tmp = (a[j] - cur + dif - 1) / dif; ans[id] += tmp; cur = a[j] - tmp * dif; if (cur < 0) { ans[id] = -1; break; } } } } for (int i = 1; i <= q; i++) cout << ans[i] << '\n'; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:25:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     freopen(task".INP", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     freopen(task".OUT", "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...