Submission #237204

#TimeUsernameProblemLanguageResultExecution timeMemory
237204manh9203Triple Jump (JOI19_jumps)C++17
100 / 100
1483 ms108280 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long const int N = 5e5 + 5; ll n, a[N]; int q, L[N], R[N]; vector<int> queRy[N]; vector<int> add[N]; ll ans[N]; struct node { ll D, C, lz; } st[4 * N]; void build(int id, int l, int r) { if (l == r) { st[id].C = a[l]; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); st[id].D = max(st[id << 1].D, st[id << 1 | 1].D); st[id].C = max(st[id << 1].C, st[id << 1 | 1].C); } void pushDown(int id, int l, int r) { if (st[id].lz != 0) { st[id].D = max(st[id].D, st[id].C + st[id].lz); if (l < r) { st[id << 1].lz = max(st[id << 1].lz, st[id].lz); st[id << 1 | 1].lz = max(st[id << 1 | 1].lz, st[id].lz); } st[id].lz = 0; } } void update(int id, int l, int r, int i, int j, int x) { pushDown(id, l, r); if (l > j || r < i || i > j) { return; } if (l >= i && r <= j) { st[id].lz = x; pushDown(id, l, r); return; } int mid = (l + r) >> 1; update(id << 1, l, mid, i, j, x); update(id << 1 | 1, mid + 1, r, i, j, x); st[id].D = max(st[id << 1].D, st[id << 1 | 1].D); } ll get(int id, int l, int r, int i, int j) { pushDown(id, l, r); if (l > j || r < i || i > j) { return 0; } if (l >= i && r <= j) { return st[id].D; } int mid = (l + r) >> 1; return max(get(id << 1, l, mid, i, j), get(id << 1 | 1, mid + 1, r, i, j)); } int main() { ios_base::sync_with_stdio(0); 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 >> L[i] >> R[i]; queRy[L[i]].push_back(i); } vector<int> s; for (int i = 1; i <= n; i++) { while (s.size() > 0 && a[s.back()] < a[i]) { s.pop_back(); } if (s.size() > 0) { add[s.back()].push_back(i); } s.push_back(i); } s.clear(); for (int i = n; i >= 1; i--) { while (s.size() > 0 && a[s.back()] < a[i]) { s.pop_back(); } if (s.size() > 0) { add[i].push_back(s.back()); } s.push_back(i); } build(1, 1, n); for (int i = n; i >= 1; i--) { for (int j : add[i]) { update(1, 1, n, 2 * j - i, n, a[i] + a[j]); } for (int j : queRy[i]) { ans[j] = get(1, 1, n, L[j], R[j]); } } 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...