Submission #579039

#TimeUsernameProblemLanguageResultExecution timeMemory
579039wiwihoTriple Jump (JOI19_jumps)C++14
100 / 100
1256 ms108440 KiB
#include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define ef(a) emplace_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define tomax(a, b) ((a) = max((a), (b))) #define tomin(a, b) ((a) = min((a), (b))) #define topos(a) ((a) = (((a) % MOD + MOD) % MOD)) #define uni(a) a.resize(unique(iter(a)) - a.begin()) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } ll ifloor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a < 0) return (a - b + 1) / b; else return a / b; } ll iceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a > 0) return (a + b - 1) / b; else return a / b; } struct Node{ int v = 0, tag = 0; }; int n; vector<Node> st; #define lc 2 * id + 1 #define rc 2 * id + 2 void addtag(int v, int id){ st[id].v += v; st[id].tag += v; } void push(int id){ addtag(st[id].tag, lc); addtag(st[id].tag, rc); st[id].tag = 0; } void pull(int id){ st[id].v = max(st[lc].v, st[rc].v); } void modify(int l, int r, int v, int L = 1, int R = n, int id = 0){ if(l <= L && R <= r){ addtag(v, id); return; } //cerr << l << " " << r << " " << L << " " << R << "\n"; push(id); int M = (L + R) / 2; if(r <= M) modify(l, r, v, L, M, lc); else if(l > M) modify(l, r, v, M + 1, R, rc); else{ modify(l, r, v, L, M, lc); modify(l, r, v, M + 1, R, rc); } pull(id); } int query(int l, int r, int L = 1, int R = n, int id = 0){ if(l <= L && R <= r){ return st[id].v; } push(id); int M = (L + R) / 2; if(r <= M) return query(l, r, L, M, lc); else if(l > M) return query(l, r, M + 1, R, rc); else return max(query(l, r, L, M, lc), query(l, r, M + 1, R, rc)); } int main(){ StarBurstStream cin >> n; vector<int> a(n + 1); vector<vector<pii>> ev(n + 1); deque<int> dq; st.resize(4 * n); auto add = [&](int i, int j){ int len = j - i; int k = i + 2 * len; ev[i].eb(mp(k, a[i] + a[j])); }; for(int i = 1; i <= n; i++){ cin >> a[i]; modify(i, i, a[i]); while(!dq.empty() && a[dq.back()] < a[i]){ add(dq.back(), i); dq.pob; } if(!dq.empty()) add(dq.back(), i); dq.eb(i); } vector<vector<pii>> qry(n + 1); int q; cin >> q; for(int i = 0; i < q; i++){ int l, r; cin >> l >> r; qry[l].eb(mp(r, i)); } map<int, int> mx; vector<int> ans(q); mx[1] = 0; auto upd = [&](int p, int v){ if(p > n) return; //cerr << "upd " << p << " " << v << "\n"; auto it = mx.upper_bound(p); it--; if(v <= it->S) return; int lp = max(p, it->F); int nxt = next(it) == mx.end() ? n + 1 : next(it)->F; modify(lp, nxt - 1, v - it->S); mx[p] = v; it = mx.upper_bound(p); while(it != mx.end() && it->S <= v){ nxt = next(it) == mx.end() ? n + 1 : next(it)->F; modify(it->F, nxt - 1, v - it->S); it = mx.erase(it); } //printv(mx, cerr); }; for(int i = n; i >= 1; i--){ for(pii t : ev[i]){ upd(t.F, t.S); } for(pii t : qry[i]){ int r, id; tie(r, id) = t; ans[id] = query(i, r); } } for(int i : ans) cout << 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...