Submission #970547

#TimeUsernameProblemLanguageResultExecution timeMemory
970547DAleksaTriple Jump (JOI19_jumps)C++17
0 / 100
116 ms44236 KiB
#include <bits/stdc++.h> using namespace std; struct query { int l, r; int ans; }; struct node { int mx; int ans; int lzy; }; const int N = 5e5 + 10; int n, q; int a[N]; query Q[N]; node st[4 * N]; vector<int> qs[N]; vector<int> upd[N]; stack<int> stk; void build(int index, int l, int r) { if(l > r) return; if(l == r) { st[index].mx = a[l]; st[index].ans = 0; st[index].lzy = 0; return; } int mid = (l + r) / 2; build(2 * index, l, mid); build(2 * index + 1, mid + 1, r); st[index].mx = max(st[2 * index].mx, st[2 * index + 1].mx); st[index].ans = 0; st[index].lzy = 0; } void propagate(int index) { if(2 * index + 1 < 4 * N && st[index].lzy > 0) { st[2 * index].ans = max(st[2 * index].ans, st[2 * index].mx + st[index].lzy); st[2 * index].lzy = max(st[2 * index].lzy, st[index].lzy); st[2 * index + 1].ans = max(st[2 * index + 1].ans, st[2 * index + 1].mx + st[index].lzy); st[2 * index + 1].lzy = max(st[2 * index + 1].lzy, st[index].lzy); st[index].lzy = 0; } } void update(int index, int l, int r, int L, int R, int val) { if(l > r || r < L || R < l) return; if(L <= l && r <= R) { st[index].ans = max(st[index].ans, val + st[index].mx); st[index].lzy = max(st[index].lzy, val); return; } propagate(index); int mid = (l + r) / 2; update(2 * index, l, mid, L, R, val); update(2 * index + 1, mid + 1, r, L, R, val); st[index].ans = max(st[2 * index].ans, st[2 * index + 1].ans); } int get(int index, int l, int r, int L, int R) { if(l > r || r < L || R < l) return 0; if(L <= l && r <= R) return st[index].ans; propagate(index); int mid = (l + r) / 2; return max(get(2 * index, l, mid, L, R), get(2 * index + 1, mid + 1, r, L, R)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; cin >> q; for(int i = 1; i <= q; i++) { cin >> Q[i].l >> Q[i].r; qs[Q[i].l].push_back(i); } for(int i = 1; i <= n; i++) { while(!stk.empty() && a[stk.top()] <= a[i]) stk.pop(); if(!stk.empty()) { int x = stk.top(), y = i; if(y + (y - x) <= n) upd[x].push_back(y); } stk.push(i); } while(!stk.empty()) stk.pop(); for(int i = n; i >= 1; i--) { while(!stk.empty() && a[stk.top()] <= a[i]) stk.pop(); if(!stk.empty()) { int x = i, y = stk.top(); if(y + (y - x) <= n) upd[x].push_back(y); } stk.push(i); } build(1, 1, n); for(int l = n; l >= 1; l--) { for(int y : upd[l]) { int x = l; int z = y + (y - x); update(1, 1, n, z, n, a[x] + a[y]); } for(int ind : qs[l]) { int r = Q[ind].r; Q[ind].ans = get(1, 1, n, l, r); } } for(int i = 1; i <= q; i++) cout << Q[i].ans << "\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...