제출 #655548

#제출 시각아이디문제언어결과실행 시간메모리
655548piOOE3단 점프 (JOI19_jumps)C++17
100 / 100
716 ms40604 KiB
#include <bits/stdc++.h> using namespace std; struct Info { int add = 0, a = 0, ans = 0; }; Info operator+(Info a, Info b) { return {max(a.add, b.add), max(a.a, b.a), max({a.ans, b.ans, a.add + b.a})}; } int sz = 1; vector<Info> t; void init(int n, vector<int> a) { sz = n; t.assign(sz << 1, {}); for (int i = 0; i < n; ++i) { t[i + sz].a = t[i + sz].ans = a[i]; } for (int i = sz - 1; i > 0; --i) { t[i] = t[i << 1] + t[i << 1 | 1]; } } Info rangeQuery(int l, int r) { Info L{}, R{}; for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) { if (l & 1) { L = L + t[l++]; } if (r & 1) { R = t[--r] + R; } } return L + R; } void modify(int i, int add) { i += sz; t[i].add = max(t[i].add, add); t[i].ans = t[i].add + t[i].a; while (i >>= 1) { t[i] = t[i << 1] + t[i << 1 | 1]; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<array<int, 3>> events; auto relax = [&](int i, int j) { int r = j + (j - i); if (r < n) { events.push_back({i, j, -1}); } }; vector<int> stk; for (int i = 0; i < n; ++i) { while (!stk.empty() && a[stk.back()] <= a[i]) { relax(stk.back(), i); stk.pop_back(); } if (!stk.empty()) { relax(stk.back(), i); } stk.push_back(i); } int q; cin >> q; for (int i = 0; i < q; ++i) { int l, r; cin >> l >> r; events.push_back({l - 1, r, i}); } vector<int> ans(q); sort(events.begin(), events.end(), [&](array<int, 3> x, array<int, 3> y) { return x[0] != y[0] ? x[0] > y[0] : x[2] < y[2]; }); init(n, a); for (auto [i, j, tp]: events) { if (tp > -1) { ans[tp] = rangeQuery(i, j).ans; } else { int k = j + (j - i); int sum = a[i] + a[j]; modify(k, sum); } } for (int i = 0; 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...