Submission #546827

#TimeUsernameProblemLanguageResultExecution timeMemory
546827ArnchTriple Jump (JOI19_jumps)C++17
100 / 100
1124 ms148556 KiB
// oooo /* har chi delet mikhad bebar ~ gitar o ba khodet nabar! ~ ;Amoo_Hasan; */ #include<bits/stdc++.h> //#pragma GCC optimize("O3,no-stack-protector,unroll-loops") //#pragma GCC target("avx2,fma") using namespace std; typedef long long ll; typedef long double ld; #define Sz(x) int((x).size()) #define All(x) (x).begin(), (x).end() #define wtf(x) cout<<#x <<" : " <<x <<endl #define endl '\n' constexpr ll inf = 1e18, N = 1e6 + 10, mod = 1e9 + 7, pr = 1000696969; int a[N], lm[N], rm[N], ansl[N], ansr[N], ans[N]; pair<int, int> p[N]; vector<int> query[N], st[N]; struct node { int ans, lz, mx = 0; node() { ans = lz = mx = 0; } } x[N << 2]; void build(int l, int r, int v) { if(r - l < 2) { x[v].mx = a[l]; return; } int mid = (l + r) >> 1; build(l, mid, 2 * v); build(mid, r, 2 * v + 1); x[v].mx = max(x[2 * v].mx, x[2 * v + 1].mx); } void put(int l, int r, int v) { x[2 * v].ans = max(x[2 * v].ans, x[v].lz + x[2 * v].mx); x[2 * v].lz = max(x[2 * v].lz, x[v].lz); x[2 * v + 1].ans = max(x[2 * v + 1].ans, x[v].lz + x[2 * v + 1].mx); x[2 * v + 1].lz = max(x[2 * v + 1].lz, x[v].lz); } void change(int l, int r, int s, int e, int val, int v) { if(r <= s || l >= e) return; if(l >= s && r <= e) { x[v].lz = max(x[v].lz, val); x[v].ans = max(x[v].ans, val + x[v].mx); return; } int mid = (l + r) / 2; put(l, r, v); change(l, mid, s, e, val, 2 * v); change(mid, r, s, e, val, 2 * v + 1); x[v].ans = max(x[v].lz + x[v].mx, max(x[2 * v].ans, x[2 * v + 1].ans)); } int get(int l, int r, int s, int e, int v) { if(r <= s || l >= e) return 0; if(l >= s && r <= e) return x[v].ans; put(l, r, v); int mid = (l + r) >> 1; return max(get(l, mid, s, e, 2 * v), get(mid, r, s, e, 2 * v + 1)); } int main() { ios :: sync_with_stdio(0), cin.tie(0); int n; cin >>n; for(int i = 0; i < n; i++) { cin >>a[i]; } int q; cin >>q; for(int i = 0; i < q; i++) { cin >>p[i].first >>p[i].second; p[i].first--, p[i].second--; query[p[i].first].push_back(i); } vector<int> vc; for(int i = 0; i < n; i++) { while(!vc.empty() && a[vc.back()] < a[i]) vc.pop_back(); if(vc.empty()) lm[i] = -1; else lm[i] = vc.back(); vc.push_back(i); } vc.clear(); for(int i = n - 1; i >= 0; i--) { while(!vc.empty() && a[vc.back()] < a[i]) vc.pop_back(); if(vc.empty()) rm[i] = n; else rm[i] = vc.back(); vc.push_back(i); } build(0, n, 1); for(int i = n - 1; i >= 0; i--) { if(rm[i] != n) { int ind = rm[i] + (rm[i] - i); if(ind < n) change(0, n, ind, n, a[i] + a[rm[i]], 1); } for(auto j : st[i]) { change(0, n, j + (j - i), n, a[i] + a[j], 1); } if(lm[i] != -1) { int ind = i + (i - lm[i]); if(ind < n) { st[lm[i]].push_back(i); } } for(auto j : query[i]) { ans[j] = get(0, n, p[j].first, p[j].second + 1, 1); } } for(int i = 0; i < q; i++) cout<<ans[i] <<endl; 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...