#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
const int N = 6e5+5, inf = 1e16, mod = 1e9+7;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
struct node{
int ans, ab, c;
node operator +(node nw){
node x;
x.ans = max(ans, nw.ans);
x.ab = max(ab, nw.ab);
x.c = max(c, nw.c);
return x;
}
};
struct segtree{
vector<node> t;
vector<int> b;
segtree(){
t.resize(N*4);
b.resize(N*4, 0);
};
void build(int i, int l, int r, vector<int> &a){
if(l == r){
t[i] = {a[l], 0ll, a[l]};
return;
}
int m = (l+r)>>1;
build(i*2, l, m, a);
build(i*2+1, m+1, r, a);
t[i] = t[i*2] + t[i*2+1];
};
void push(int i, int l, int r){
if(l!=r){
chmax(b[i*2],b[i]);
chmax(b[i*2+1],b[i]);
}
chmax(t[i].ab, b[i]);
chmax(t[i].ans, b[i] + t[i].c);
b[i] = 0;
return;
};
void upd(int i, int tl, int tr, int l, int r, int val){
push(i, l, r);
if(l > tr || r < tl)return;
if(tl <= l && r <= tr){
b[i] = val;
push(i,l,r);
return;
}
int m = (l+r)>>1;
upd(i*2, tl, tr, l, m, val);
upd(i*2+1, tl, tr, m+1, r, val);
t[i] = t[i] + t[i*2];
t[i] = t[i] + t[i*2+1];
return;
};
int get(int i, int l, int r, int tl, int tr){
push(i, l, r);
if(l > tr || r < tl)return 0ll;
if(tl <= l && r <= tr)return t[i].ans;
int m = (l+r)>>1;
return max(
get(i*2, l, m, tl, tr),
get(i*2+1, m+1, r, tl, tr)
);
}
};
segtree T;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<int> a(n+1);
for(int i = 1; i <= n; i++)cin >> a[i];
T.build(1, 1, n, a);
vector<vector<pair<int,int>>> qq(n+1);
int q;cin >> q;
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
qq[l].pb({r, i});
}
vector<int> ans(q+1);
vector<pair<int,int>> cur;
for(int i = n; i > 0; i--){
while(cur.size() && cur.back().ff < a[i]){
int A = i, B = cur.back().ss;
if(2*B-A <= n)T.upd(1, 2*B-A, n, 1, n, a[A] + a[B]);
cur.pop_back();
}
if(cur.size()){
int A = i, B = cur.back().ss;
if(2*B-A <= n)T.upd(1, 2*B-A, n, 1, n, a[A] + a[B]);
}
cur.pb({a[i], i});
for(auto j:qq[i]){
ans[j.ss] = T.get(1, 1, n, i, j.ff);
}
}
for(int i = 0; i < q; i++)cout << ans[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
75608 KB |
Output is correct |
2 |
Correct |
25 ms |
75612 KB |
Output is correct |
3 |
Correct |
32 ms |
75452 KB |
Output is correct |
4 |
Correct |
27 ms |
75348 KB |
Output is correct |
5 |
Correct |
25 ms |
75612 KB |
Output is correct |
6 |
Correct |
26 ms |
75540 KB |
Output is correct |
7 |
Correct |
25 ms |
75620 KB |
Output is correct |
8 |
Correct |
25 ms |
75448 KB |
Output is correct |
9 |
Correct |
26 ms |
75572 KB |
Output is correct |
10 |
Correct |
30 ms |
75344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
75608 KB |
Output is correct |
2 |
Correct |
25 ms |
75612 KB |
Output is correct |
3 |
Correct |
32 ms |
75452 KB |
Output is correct |
4 |
Correct |
27 ms |
75348 KB |
Output is correct |
5 |
Correct |
25 ms |
75612 KB |
Output is correct |
6 |
Correct |
26 ms |
75540 KB |
Output is correct |
7 |
Correct |
25 ms |
75620 KB |
Output is correct |
8 |
Correct |
25 ms |
75448 KB |
Output is correct |
9 |
Correct |
26 ms |
75572 KB |
Output is correct |
10 |
Correct |
30 ms |
75344 KB |
Output is correct |
11 |
Correct |
257 ms |
102740 KB |
Output is correct |
12 |
Correct |
234 ms |
102864 KB |
Output is correct |
13 |
Correct |
233 ms |
102704 KB |
Output is correct |
14 |
Correct |
229 ms |
102736 KB |
Output is correct |
15 |
Correct |
267 ms |
102812 KB |
Output is correct |
16 |
Correct |
234 ms |
101968 KB |
Output is correct |
17 |
Correct |
241 ms |
102228 KB |
Output is correct |
18 |
Correct |
237 ms |
101844 KB |
Output is correct |
19 |
Correct |
236 ms |
102736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
81744 KB |
Output is correct |
2 |
Correct |
91 ms |
85964 KB |
Output is correct |
3 |
Correct |
89 ms |
81860 KB |
Output is correct |
4 |
Correct |
146 ms |
81748 KB |
Output is correct |
5 |
Correct |
142 ms |
81744 KB |
Output is correct |
6 |
Correct |
136 ms |
81752 KB |
Output is correct |
7 |
Correct |
138 ms |
81856 KB |
Output is correct |
8 |
Correct |
135 ms |
81860 KB |
Output is correct |
9 |
Correct |
137 ms |
81744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
75608 KB |
Output is correct |
2 |
Correct |
25 ms |
75612 KB |
Output is correct |
3 |
Correct |
32 ms |
75452 KB |
Output is correct |
4 |
Correct |
27 ms |
75348 KB |
Output is correct |
5 |
Correct |
25 ms |
75612 KB |
Output is correct |
6 |
Correct |
26 ms |
75540 KB |
Output is correct |
7 |
Correct |
25 ms |
75620 KB |
Output is correct |
8 |
Correct |
25 ms |
75448 KB |
Output is correct |
9 |
Correct |
26 ms |
75572 KB |
Output is correct |
10 |
Correct |
30 ms |
75344 KB |
Output is correct |
11 |
Correct |
257 ms |
102740 KB |
Output is correct |
12 |
Correct |
234 ms |
102864 KB |
Output is correct |
13 |
Correct |
233 ms |
102704 KB |
Output is correct |
14 |
Correct |
229 ms |
102736 KB |
Output is correct |
15 |
Correct |
267 ms |
102812 KB |
Output is correct |
16 |
Correct |
234 ms |
101968 KB |
Output is correct |
17 |
Correct |
241 ms |
102228 KB |
Output is correct |
18 |
Correct |
237 ms |
101844 KB |
Output is correct |
19 |
Correct |
236 ms |
102736 KB |
Output is correct |
20 |
Correct |
146 ms |
81744 KB |
Output is correct |
21 |
Correct |
91 ms |
85964 KB |
Output is correct |
22 |
Correct |
89 ms |
81860 KB |
Output is correct |
23 |
Correct |
146 ms |
81748 KB |
Output is correct |
24 |
Correct |
142 ms |
81744 KB |
Output is correct |
25 |
Correct |
136 ms |
81752 KB |
Output is correct |
26 |
Correct |
138 ms |
81856 KB |
Output is correct |
27 |
Correct |
135 ms |
81860 KB |
Output is correct |
28 |
Correct |
137 ms |
81744 KB |
Output is correct |
29 |
Correct |
794 ms |
124244 KB |
Output is correct |
30 |
Correct |
695 ms |
132088 KB |
Output is correct |
31 |
Correct |
618 ms |
124316 KB |
Output is correct |
32 |
Correct |
780 ms |
124108 KB |
Output is correct |
33 |
Correct |
775 ms |
124116 KB |
Output is correct |
34 |
Correct |
861 ms |
121940 KB |
Output is correct |
35 |
Correct |
848 ms |
121548 KB |
Output is correct |
36 |
Correct |
749 ms |
121680 KB |
Output is correct |
37 |
Correct |
827 ms |
122908 KB |
Output is correct |
38 |
Correct |
646 ms |
126740 KB |
Output is correct |
39 |
Correct |
521 ms |
126800 KB |
Output is correct |
40 |
Correct |
582 ms |
123532 KB |
Output is correct |
41 |
Correct |
597 ms |
122964 KB |
Output is correct |
42 |
Correct |
589 ms |
123012 KB |
Output is correct |
43 |
Correct |
590 ms |
124752 KB |
Output is correct |
44 |
Correct |
691 ms |
127020 KB |
Output is correct |
45 |
Correct |
636 ms |
127060 KB |
Output is correct |
46 |
Correct |
625 ms |
123816 KB |
Output is correct |
47 |
Correct |
641 ms |
123580 KB |
Output is correct |
48 |
Correct |
630 ms |
123596 KB |
Output is correct |
49 |
Correct |
621 ms |
125520 KB |
Output is correct |
50 |
Correct |
704 ms |
127220 KB |
Output is correct |
51 |
Correct |
723 ms |
127252 KB |
Output is correct |
52 |
Correct |
660 ms |
124752 KB |
Output is correct |
53 |
Correct |
657 ms |
124556 KB |
Output is correct |
54 |
Correct |
724 ms |
124572 KB |
Output is correct |
55 |
Correct |
652 ms |
126152 KB |
Output is correct |