Submission #1011463

#TimeUsernameProblemLanguageResultExecution timeMemory
1011463thinknoexitFish 3 (JOI24_fish3)C++17
100 / 100
494 ms63236 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 300300; int n; ll a[N], b[N], p[N], ans[N], qs[N]; ll tree[4 * N], lazy[4 * N], mn[4 * N]; vector<pair<int, int>> Q[N]; void build(int now = 1, int l = 1, int r = n) { lazy[now] = -1e18; if (l == r) { mn[now] = a[l]; return; } int mid = (l + r) / 2; build(2 * now, l, mid), build(2 * now + 1, mid + 1, r); mn[now] = min(mn[2 * now], mn[2 * now + 1]); } ll qmn(int ql, int qr, int now = 1, int l = 1, int r = n) { if (l > qr || r < ql) return 1e18; if (ql <= l && r <= qr) return mn[now]; int mid = (l + r) / 2; return min(qmn(ql, qr, 2 * now, l, mid), qmn(ql, qr, 2 * now + 1, mid + 1, r)); } void lzy(int now, int l, int r) { if (lazy[now] == -1e18) return; tree[now] = lazy[now] * (r - l + 1); if (l != r) lazy[2 * now] = lazy[2 * now + 1] = lazy[now]; lazy[now] = -1e18; } void update(int ql, int qr, ll w, int now = 1, int l = 1, int r = n) { lzy(now, l, r); if (l > qr || r < ql) return; if (ql <= l && r <= qr) { lazy[now] = w; lzy(now, l, r); return; } int mid = (l + r) / 2; update(ql, qr, w, 2 * now, l, mid), update(ql, qr, w, 2 * now + 1, mid + 1, r); tree[now] = tree[2 * now] + tree[2 * now + 1]; } ll query(int ql, int qr, int now = 1, int l = 1, int r = n) { lzy(now, l, r); if (l > qr || r < ql) return 0ll; if (ql <= l && r <= qr) return tree[now]; int mid = (l + r) / 2; return query(ql, qr, 2 * now, l, mid) + query(ql, qr, 2 * now + 1, mid + 1, r); } int main() { cin.tie(nullptr)->sync_with_stdio(false); ll d; cin >> n >> d; for (int i = 1;i <= n;i++) { // c[i] = d * a[i] + b[i] ll c; cin >> c; a[i] = c / d; b[i] = c % d; } for (int i = 2;i <= n;i++) { p[i] = p[i - 1] + (b[i - 1] > b[i]); } for (int i = 1;i <= n;i++) { a[i] -= p[i]; qs[i] = qs[i - 1] + a[i]; } build(); int q; cin >> q; for (int i = 1;i <= q;i++) { int l, r; cin >> l >> r; Q[r].push_back({ l, i }); } // do like subtask 3 stack<pair<ll, int>> st; for (int i = 1;i <= n;i++) { while (!st.empty() && st.top().first >= a[i]) { st.pop(); } if (st.empty()) update(1, i, a[i]); else update(st.top().second + 1, i, a[i]); st.push({ a[i], i }); for (auto& [l, idx] : Q[i]) { if (qmn(l, i) + p[l] < 0) ans[idx] = -1; else ans[idx] = qs[i] - qs[l - 1] - query(l, i); } } for (int i = 1;i <= q;i++) { cout << ans[i] << '\n'; } return 0; }
#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...