Submission #1064681

#TimeUsernameProblemLanguageResultExecution timeMemory
1064681SharkyTriple Jump (JOI19_jumps)C++17
100 / 100
711 ms90452 KiB
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 8; int n, q, a[N], ans[N], mx[N]; vector<int> pr[N]; vector<pair<int, int>> qry[N]; stack<pair<int, int>> stk; struct Node { int pf, sf, sum; } seg[4 * N]; Node merge(Node lhs, Node rhs) { Node res; res.pf = max(lhs.pf, rhs.pf); res.sf = max(lhs.sf, rhs.sf); res.sum = max({lhs.sum, rhs.sum, lhs.pf + rhs.sf}); return res; } void build(int l, int r, int id) { if (l == r) { seg[id].pf = 0; seg[id].sf = seg[id].sum = a[l]; return; } int mid = (l + r) >> 1; build(l, mid, id * 2); build(mid + 1, r, id * 2 + 1); seg[id] = merge(seg[id * 2], seg[id * 2 + 1]); } void update(int pos, int val, int l, int r, int id) { if (l == r) { seg[id].pf = val; seg[id].sum = seg[id].pf + seg[id].sf; return; } int mid = (l + r) >> 1; if (pos <= mid) update(pos, val, l, mid, id * 2); else update(pos, val, mid + 1, r, id * 2 + 1); seg[id] = merge(seg[id * 2], seg[id * 2 + 1]); } Node query(int ql, int qr, int l, int r, int id) { if (qr < l || r < ql) return {0, 0}; if (ql <= l && r <= qr) return seg[id]; int mid = (l + r) >> 1; return merge(query(ql, qr, l, mid, id * 2), query(ql, qr, mid + 1, r, id * 2 + 1)); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (!stk.empty() && stk.top().first <= a[i]) { pr[stk.top().second].push_back(i); stk.pop(); } if (!stk.empty()) pr[stk.top().second].push_back(i); stk.push({a[i], i}); } cin >> q; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; qry[l].push_back({r, i}); } build(1, n, 1); for (int i = n; i >= 1; i--) { for (int& j : pr[i]) { if (2 * j - i > n) continue; int nw = a[i] + a[j]; if (nw > mx[2 * j - i]) { update(2 * j - i, nw, 1, n, 1); mx[2 * j - i] = nw; // cout << "upd: " << 2 * j - i << " " << nw << "\n"; } } for (auto& [j, idx] : qry[i]) { // cout << "idx = " << idx << '\n'; ans[idx] = query(i, j, 1, n, 1).sum; } } for (int i = 1; i <= q; i++) cout << ans[i] << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...