Submission #1078116

#TimeUsernameProblemLanguageResultExecution timeMemory
1078116TrinhKhanhDungTriple Jump (JOI19_jumps)C++14
100 / 100
852 ms124788 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1e9 + 7) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} struct seg{ vector<ll> a, b, laz; int n; seg(int _n = 0){ n = _n; a.resize(n * 4 + 3, 0); b.resize(n * 4 + 3, 0); laz.resize(n * 4 + 3, 0); } void build(int val[], int i, int l, int r){ if(l == r){ a[i] = val[l]; b[i] = val[l]; return; } int m = (l + r) >> 1; build(val, i * 2, l, m); build(val, i * 2 + 1, m + 1, r); a[i] = max(a[i * 2], a[i * 2 + 1]); b[i] = max(b[i * 2], b[i * 2 + 1]); } void push(int i){ maximize(laz[i * 2], laz[i]); maximize(laz[i * 2 + 1], laz[i]); maximize(b[i * 2], a[i * 2] + laz[i]); maximize(b[i * 2 + 1], a[i * 2 + 1] + laz[i]); } void upd(int i, int l, int r, int u, int v, ll c){ if(r < u || v < l) return; if(u <= l && r <= v){ maximize(laz[i], c); maximize(b[i], a[i] + c); return; } push(i); int m = (l + r) >> 1; upd(i * 2, l, m, u, v, c); upd(i * 2 + 1, m + 1, r, u, v, c); b[i] = max(b[i * 2], b[i * 2 + 1]); } ll get(int i, int l, int r, int u, int v){ if(r < u || v < l) return 0; if(u <= l && r <= v) return b[i]; push(i); int m = (l + r) >> 1; return max(get(i * 2, l, m, u, v), get(i * 2 + 1, m + 1, r, u, v)); } }; const int MAX = 5e5 + 10; int N, Q; int a[MAX]; ll ans[MAX]; vector<int> change[MAX]; vector<pair<int, int>> ask[MAX]; void solve(){ cin >> N; for(int i=1; i<=N; i++){ cin >> a[i]; } seg it(N); it.build(a, 1, 1, N); stack<int> st; for(int i=N; i>=1; i--){ while(sz(st)){ change[i].push_back(st.top()); if(a[st.top()] > a[i]) break; st.pop(); } st.push(i); } cin >> Q; for(int i=1; i<=Q; i++){ int l, r; cin >> l >> r; ask[l].push_back({r, i}); } for(int i=N; i>=1; i--){ for(int x: change[i]){ if(x * 2 - i <= N){ it.upd(1, 1, N, x * 2 - i, N, a[i] + a[x]); } } for(auto o: ask[i]){ ans[o.se] = it.get(1, 1, N, i, o.fi); } } for(int i=1; i<=Q; i++){ cout << ans[i] << '\n'; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("task.inp", "r", stdin); // freopen("task.out", "w", stdout); int t = 1; // cin >> t; while(t--){ solve(); } 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...