#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 5e5;
int n,q;
int v[NMAX + 5];
int st[NMAX + 5],len;
vector<int> segments[NMAX + 5];
vector<pair<int,int> > queries[NMAX + 5];
int ans[NMAX + 5];
struct node_t {
int ma_val;
int lazy;
int best_ans;
int offset;
node_t operator + (const node_t &other)const {
node_t ans;
ans.ma_val = max(this->ma_val,other.ma_val);
ans.lazy = 0;
ans.offset = (this->offset == other.offset ? this->offset:-(1 << 30));
ans.best_ans = max(this->best_ans,other.best_ans);
return ans;
}
} aint[NMAX * 4 + 5];
void propag(int nod,int st,int dr) {
if(st == dr || aint[nod].lazy == 0) {
return ;
}
aint[nod * 2].lazy = max(aint[nod * 2].lazy,aint[nod].lazy);
aint[nod * 2 + 1].lazy = max(aint[nod * 2 + 1].lazy,aint[nod].lazy);
aint[nod * 2].offset = max(aint[nod * 2].offset,aint[nod].lazy);
aint[nod * 2 + 1].offset = max(aint[nod * 2 + 1].offset,aint[nod].lazy);
aint[nod * 2].best_ans = aint[nod * 2].ma_val + aint[nod * 2].offset;
aint[nod * 2 + 1].best_ans = aint[nod * 2 + 1].ma_val + aint[nod * 2 + 1].offset;
aint[nod].lazy = 0;
}
void init(int nod,int st,int dr) {
if(st == dr) {
aint[nod].ma_val = v[st];
aint[nod].lazy = 0;
aint[nod].best_ans = -1;
aint[nod].offset = -(1 << 28);
return ;
}
int mid = (st + dr) / 2;
init(nod * 2,st,mid);
init(nod * 2 + 1,mid + 1,dr);
aint[nod] = aint[nod * 2] + aint[nod * 2 + 1];
}
void update(int nod,int st,int dr,int l,int r,int val) {
propag(nod,st,dr);
if(l > dr || r < st) {
return ;
}
if(l <= st && dr <= r && aint[nod].offset != -(1 << 30)) {
aint[nod].lazy = val;
aint[nod].offset = max(aint[nod].offset,val);
aint[nod].best_ans = aint[nod].ma_val + aint[nod].offset;
return ;
}
int mid = (st + dr) / 2;
update(nod * 2,st,mid,l,r,val);
update(nod * 2 + 1,mid + 1,dr,l,r,val);
aint[nod] = aint[nod * 2] + aint[nod * 2 + 1];
}
int query(int nod,int st,int dr,int l,int r) {
propag(nod,st,dr);
if(l > dr || r < st) {
return 0;
}
if(l <= st && dr <= r) {
return aint[nod].best_ans;
}
int mid = (st + dr) / 2;
return max(query(nod * 2,st,mid,l,r),query(nod * 2 + 1,mid + 1,dr,l,r));
}
int main() {
scanf("%d",&n);
for(int i = 1; i <= n; i++) {
scanf("%d",&v[i]);
}
scanf("%d",&q);
for(int i = 1; i <= q; i++) {
int l,r;
scanf("%d %d",&l,&r);
queries[l].push_back({r,i});
}
for(int i = 1; i <= n; i++) {
while(len > 0 && v[st[len]] < v[i]) {
segments[st[len]].push_back(i);
len--;
}
if(len > 0) {
segments[st[len]].push_back(i);
}
st[++len] = i;
}
init(1,1,n);
for(int i = n; i; i--) {
for(auto it:segments[i]) {
if(2 * it - i <= n){
update(1,1,n,2 * it - i,n,v[i] + v[it]);
}
}
for(auto it:queries[i]) {
ans[it.second] = query(1,1,n,i,it.first);
}
}
for(int i = 1; i <= q; i++) {
printf("%d\n",ans[i]);
}
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
jumps.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&v[i]);
~~~~~^~~~~~~~~~~~
jumps.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
jumps.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&l,&r);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23756 KB |
Output is correct |
11 |
Correct |
443 ms |
39196 KB |
Output is correct |
12 |
Correct |
616 ms |
39128 KB |
Output is correct |
13 |
Correct |
419 ms |
39192 KB |
Output is correct |
14 |
Correct |
432 ms |
39276 KB |
Output is correct |
15 |
Correct |
443 ms |
39280 KB |
Output is correct |
16 |
Correct |
449 ms |
38536 KB |
Output is correct |
17 |
Correct |
458 ms |
38660 KB |
Output is correct |
18 |
Correct |
434 ms |
38520 KB |
Output is correct |
19 |
Correct |
440 ms |
39032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1708 ms |
41068 KB |
Output is correct |
2 |
Execution timed out |
4103 ms |
40828 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23756 KB |
Output is correct |
11 |
Correct |
443 ms |
39196 KB |
Output is correct |
12 |
Correct |
616 ms |
39128 KB |
Output is correct |
13 |
Correct |
419 ms |
39192 KB |
Output is correct |
14 |
Correct |
432 ms |
39276 KB |
Output is correct |
15 |
Correct |
443 ms |
39280 KB |
Output is correct |
16 |
Correct |
449 ms |
38536 KB |
Output is correct |
17 |
Correct |
458 ms |
38660 KB |
Output is correct |
18 |
Correct |
434 ms |
38520 KB |
Output is correct |
19 |
Correct |
440 ms |
39032 KB |
Output is correct |
20 |
Correct |
1708 ms |
41068 KB |
Output is correct |
21 |
Execution timed out |
4103 ms |
40828 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |