#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<long double, ll> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353, maxn = 5e5 + 5, lg = 19, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
ll a[maxn];
int n, q;
void slv2(){
vector<vector<ll>> mx(n, vector<ll>(n, -inf));
rep(i,0,n){
mx[i][i] = a[i];
rep(j,i+1,n) mx[i][j] = max(mx[i][j-1], a[j]);
}
vector<vector<ll>> ans(n, vector<ll>(n, -inf));
rep(l,3,n+1){
rep(i,0,n-l+1){
int j = i + l - 1;
ans[i][j] = max({ans[i+1][j], ans[i][j-1], a[i] + a[j] + mx[i+1][(i+j)>>1]});
}
}
while(q--){
int l, r; cin >> l >> r;
cout << ans[--l][--r] << '\n';
}
}
template<class T> struct Rmq{
vector<vector<T>> rmq;
vector<int> lgg;
T comb(T a, T b){
return max(a, b);
}
Rmq(vector<T> a){
int n = sz(a);
lgg.resize(n + 1);
rep(i,2,n + 1) lgg[i] = lgg[i>>1] + 1;
rmq.assign(lgg[n] + 1, vector<T>(n));
rmq[0] = a;
rep(i,1,lgg[n] + 1){
rep(j,0,n-(1<<i)+1){
rmq[i][j] = comb(rmq[i-1][j], rmq[i-1][j + (1 << (i-1))]);
}
}
}
T get(int l, int r){ // 0-base [l, r]
int g = lgg[r - l + 1];
return comb(rmq[g][l], rmq[g][r - (1<<g) + 1]);
}
};
void slv3(){
int l, r; cin >> l >> r; l--, r--, n = r - l + 1;
vector<int> b(a + l, a + r + 1);
Rmq<int> rmq(b);
auto get_max = [&](int l, int r){
if(l > r || r < 0) return -1;
l = max(0, l), r = min(r, n-1);
return rmq.get(l, r);
};
auto clc = [&](int i){
ll res = -inf;
// right
rep(j,0,i) res = max(res, 1ll*b[i] + b[j] + get_max(j-(i-j),j-1));
// center
rep(j,0,i) res = max(res, 1ll*b[i] + b[j] + get_max(i+(i-j),n-1));
// left
rep(j,i+1,n) res = max(res, 1ll*b[i] + b[j] + get_max(j+(j-i),n-1));
return res;
};
vector<int> c(n); iota(all(c), 0), sort(all(c), [&](int i, int j){ return b[i] > b[j]; });
ll ans = 0; int g = min(int(log2(n))+3, n);
rep(i,0,g){
ans = max(ans, clc(c[i]));
}
cout << ans << '\n';
}
ll seg[maxn<<2];
int laz[maxn<<2];
int vl[maxn<<2];
void updNode(int x, int k){
seg[x] = max(seg[x], 1ll*vl[x] + k), laz[x] = max(laz[x], k);
}
void shift(int x){
if(laz[x]){
updNode(x<<1, laz[x]);
updNode(x<<1|1, laz[x]);
laz[x] = 0;
}
}
void build(int x = 1, int lx = 0, int rx = n){
if(lx + 1 == rx) {
vl[x] = seg[x] = a[lx];
return;
}
int mid = (lx + rx)>>1;
build(x<<1, lx, mid), build(x<<1|1, mid, rx);
seg[x] = vl[x] = max(vl[x<<1], vl[x<<1|1]);
}
void upd(int l, int r, int k, int x = 1, int lx = 0, int rx = n){
if(l <= lx && r >= rx){
updNode(x, k);
return;
} if(l >= rx || r <= lx) return;
int mid = (lx + rx)>>1;
shift(x);
upd(l, r, k, x<<1, lx, mid), upd(l, r, k, x<<1|1, mid, rx);
seg[x] = max(seg[x<<1], seg[x<<1|1]);
}
ll get(int l, int r, int x = 1, int lx = 0, int rx = n){
if(l <= lx && r >= rx) return seg[x];
if(l >= rx || r <= lx) return -1;
int mid = (lx + rx)>>1;
shift(x);
return max(get(l, r, x<<1, lx, mid), get(l, r, x<<1|1, mid, rx));
}
void slv4(){
build();
vector<vector<pp>> st(n);
vector<ll> ans(q);
rep(i,0,q){
int l, r; cin >> l >> r; l--, r--;
st[l].pb({r, i});
}
vector<int> stk;
per(i,n-1,0){
vector<int> candj;
while(sz(stk) && a[stk.back()] < a[i]) candj.pb(stk.back()), stk.pop_back();
if(sz(stk)) candj.pb(stk.back());
stk.pb(i);
for(int j: candj){
int k = j+j-i;
if(k > n) continue;
upd(k, n, a[i] + a[j]);
}
for(auto[r,id]: st[i]) ans[id] = get(i, r+1);
}
for(ll c: ans) cout << c << '\n';
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> n;
rep(i,0,n) cin >> a[i];
cin >> q;
slv4();
return 0;
}
Compilation message
jumps.cpp: In function 'void slv4()':
jumps.cpp:161:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
161 | for(auto[r,id]: st[i]) ans[id] = get(i, r+1);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
324 ms |
35660 KB |
Output is correct |
12 |
Correct |
304 ms |
35900 KB |
Output is correct |
13 |
Correct |
315 ms |
35656 KB |
Output is correct |
14 |
Correct |
295 ms |
35888 KB |
Output is correct |
15 |
Correct |
298 ms |
35848 KB |
Output is correct |
16 |
Correct |
299 ms |
35124 KB |
Output is correct |
17 |
Correct |
330 ms |
35128 KB |
Output is correct |
18 |
Correct |
284 ms |
35016 KB |
Output is correct |
19 |
Correct |
327 ms |
35536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
14772 KB |
Output is correct |
2 |
Correct |
67 ms |
15724 KB |
Output is correct |
3 |
Correct |
73 ms |
14820 KB |
Output is correct |
4 |
Correct |
129 ms |
14808 KB |
Output is correct |
5 |
Correct |
111 ms |
14736 KB |
Output is correct |
6 |
Correct |
112 ms |
14700 KB |
Output is correct |
7 |
Correct |
117 ms |
14764 KB |
Output is correct |
8 |
Correct |
116 ms |
14736 KB |
Output is correct |
9 |
Correct |
114 ms |
14792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
324 ms |
35660 KB |
Output is correct |
12 |
Correct |
304 ms |
35900 KB |
Output is correct |
13 |
Correct |
315 ms |
35656 KB |
Output is correct |
14 |
Correct |
295 ms |
35888 KB |
Output is correct |
15 |
Correct |
298 ms |
35848 KB |
Output is correct |
16 |
Correct |
299 ms |
35124 KB |
Output is correct |
17 |
Correct |
330 ms |
35128 KB |
Output is correct |
18 |
Correct |
284 ms |
35016 KB |
Output is correct |
19 |
Correct |
327 ms |
35536 KB |
Output is correct |
20 |
Correct |
116 ms |
14772 KB |
Output is correct |
21 |
Correct |
67 ms |
15724 KB |
Output is correct |
22 |
Correct |
73 ms |
14820 KB |
Output is correct |
23 |
Correct |
129 ms |
14808 KB |
Output is correct |
24 |
Correct |
111 ms |
14736 KB |
Output is correct |
25 |
Correct |
112 ms |
14700 KB |
Output is correct |
26 |
Correct |
117 ms |
14764 KB |
Output is correct |
27 |
Correct |
116 ms |
14736 KB |
Output is correct |
28 |
Correct |
114 ms |
14792 KB |
Output is correct |
29 |
Correct |
1056 ms |
73976 KB |
Output is correct |
30 |
Correct |
926 ms |
76116 KB |
Output is correct |
31 |
Correct |
813 ms |
74036 KB |
Output is correct |
32 |
Correct |
988 ms |
74092 KB |
Output is correct |
33 |
Correct |
950 ms |
74020 KB |
Output is correct |
34 |
Correct |
1000 ms |
71684 KB |
Output is correct |
35 |
Correct |
964 ms |
71656 KB |
Output is correct |
36 |
Correct |
960 ms |
71516 KB |
Output is correct |
37 |
Correct |
1033 ms |
72800 KB |
Output is correct |
38 |
Correct |
654 ms |
75840 KB |
Output is correct |
39 |
Correct |
723 ms |
75892 KB |
Output is correct |
40 |
Correct |
680 ms |
72500 KB |
Output is correct |
41 |
Correct |
694 ms |
71980 KB |
Output is correct |
42 |
Correct |
854 ms |
72044 KB |
Output is correct |
43 |
Correct |
736 ms |
73760 KB |
Output is correct |
44 |
Correct |
866 ms |
76476 KB |
Output is correct |
45 |
Correct |
886 ms |
76464 KB |
Output is correct |
46 |
Correct |
776 ms |
73296 KB |
Output is correct |
47 |
Correct |
837 ms |
72848 KB |
Output is correct |
48 |
Correct |
770 ms |
72840 KB |
Output is correct |
49 |
Correct |
771 ms |
74856 KB |
Output is correct |
50 |
Correct |
844 ms |
77244 KB |
Output is correct |
51 |
Correct |
820 ms |
77272 KB |
Output is correct |
52 |
Correct |
818 ms |
74728 KB |
Output is correct |
53 |
Correct |
826 ms |
74480 KB |
Output is correct |
54 |
Correct |
795 ms |
74444 KB |
Output is correct |
55 |
Correct |
717 ms |
76100 KB |
Output is correct |