#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int A[500000];
vi S;
struct event { int l,r,t; };
bool comp(event a,event b) {
if (a.l == b.l) return a.t < b.t;
else return a.l > b.l;
}
vector<event> events;
struct node { int a,b,ab; };
node tree[1 << 20];
node com(node a,node b) {
node c;
c.a = max(a.a,b.a),c.b = max(a.b,b.b);
c.ab = max(a.a+b.b,max(a.ab,b.ab));
return c;
}
int build(int s,int e,int i) {
if (s == e) {
tree[i] = (node){0,A[s],0};
return 0;
}
int mid = (s+e) / 2;
build(s,mid,2*i+1),build(mid+1,e,2*i+2);
tree[i] = com(tree[2*i+1],tree[2*i+2]);
return 0;
}
int update(int s,int e,int ai,int i,int num) {
if ((s > ai) || (e < ai)) return 0;
else if (s == e) {
tree[i].a = max(tree[i].a,num);
return 0;
}
int mid = (s+e) / 2;
update(s,mid,ai,2*i+1,num),update(mid+1,e,ai,2*i+2,num);
tree[i] = com(tree[2*i+1],tree[2*i+2]);
return 0;
}
node query(int s,int e,int qs,int qe,int i) {
if ((s > qe) || (e < qs)) return (node){0,0,0};
else if ((s >= qs) && (e <= qe)) return tree[i];
int mid = (s+e) / 2;
return com(query(s,mid,qs,qe,2*i+1),query(mid+1,e,qs,qe,2*i+2));
}
int ans[500000];
int main() {
int i;
int N,Q,L,R;
scanf("%d",&N);
for (i = 0; i < N; i++) scanf("%d",&A[i]);
scanf("%d",&Q);
for (i = 0; i < Q; i++) scanf("%d %d",&L,&R),events.pb((event){L-1,R-1,i});
for (i = 0; i < N; i++) {
while (!S.empty() && (A[S.back()] <= A[i])) events.pb((event){S.back(),i,-1}),S.pop_back();
if (!S.empty()) events.pb((event){S.back(),i,-1});
S.pb(i);
}
sort(events.begin(),events.end(),comp);
build(0,N-1,0);
for (i = 0; i < events.size(); i++) {
if (events[i].t != -1) ans[events[i].t] = query(0,N-1,events[i].l,events[i].r,0).ab;
else update(0,N-1,2*events[i].r-events[i].l-1,0,A[events[i].l]+A[events[i].r]);
}
for (i = 0; i < Q; i++) printf("%d\n",ans[i]);
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:120:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < events.size(); i++) {
~~^~~~~~~~~~~~~~~
jumps.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
jumps.cpp:109:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < N; i++) scanf("%d",&A[i]);
~~~~~^~~~~~~~~~~~
jumps.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&Q);
~~~~~^~~~~~~~~
jumps.cpp:111:49: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < Q; i++) scanf("%d %d",&L,&R),events.pb((event){L-1,R-1,i});
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
276 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
276 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
529 ms |
13672 KB |
Output is correct |
12 |
Correct |
531 ms |
13636 KB |
Output is correct |
13 |
Correct |
546 ms |
13664 KB |
Output is correct |
14 |
Correct |
528 ms |
13784 KB |
Output is correct |
15 |
Correct |
553 ms |
13736 KB |
Output is correct |
16 |
Correct |
575 ms |
13028 KB |
Output is correct |
17 |
Correct |
551 ms |
13080 KB |
Output is correct |
18 |
Correct |
571 ms |
12972 KB |
Output is correct |
19 |
Correct |
549 ms |
13532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
217 ms |
12116 KB |
Output is correct |
2 |
Correct |
121 ms |
9704 KB |
Output is correct |
3 |
Correct |
121 ms |
11424 KB |
Output is correct |
4 |
Correct |
240 ms |
12156 KB |
Output is correct |
5 |
Correct |
217 ms |
12120 KB |
Output is correct |
6 |
Correct |
209 ms |
12144 KB |
Output is correct |
7 |
Correct |
210 ms |
12152 KB |
Output is correct |
8 |
Correct |
210 ms |
12120 KB |
Output is correct |
9 |
Correct |
219 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
276 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
529 ms |
13672 KB |
Output is correct |
12 |
Correct |
531 ms |
13636 KB |
Output is correct |
13 |
Correct |
546 ms |
13664 KB |
Output is correct |
14 |
Correct |
528 ms |
13784 KB |
Output is correct |
15 |
Correct |
553 ms |
13736 KB |
Output is correct |
16 |
Correct |
575 ms |
13028 KB |
Output is correct |
17 |
Correct |
551 ms |
13080 KB |
Output is correct |
18 |
Correct |
571 ms |
12972 KB |
Output is correct |
19 |
Correct |
549 ms |
13532 KB |
Output is correct |
20 |
Correct |
217 ms |
12116 KB |
Output is correct |
21 |
Correct |
121 ms |
9704 KB |
Output is correct |
22 |
Correct |
121 ms |
11424 KB |
Output is correct |
23 |
Correct |
240 ms |
12156 KB |
Output is correct |
24 |
Correct |
217 ms |
12120 KB |
Output is correct |
25 |
Correct |
209 ms |
12144 KB |
Output is correct |
26 |
Correct |
210 ms |
12152 KB |
Output is correct |
27 |
Correct |
210 ms |
12120 KB |
Output is correct |
28 |
Correct |
219 ms |
12124 KB |
Output is correct |
29 |
Correct |
1451 ms |
39332 KB |
Output is correct |
30 |
Correct |
1152 ms |
33476 KB |
Output is correct |
31 |
Correct |
1152 ms |
37224 KB |
Output is correct |
32 |
Correct |
1431 ms |
39336 KB |
Output is correct |
33 |
Correct |
1459 ms |
39340 KB |
Output is correct |
34 |
Correct |
1432 ms |
38580 KB |
Output is correct |
35 |
Correct |
1437 ms |
38556 KB |
Output is correct |
36 |
Correct |
1430 ms |
38584 KB |
Output is correct |
37 |
Correct |
1424 ms |
39204 KB |
Output is correct |
38 |
Correct |
1167 ms |
39240 KB |
Output is correct |
39 |
Correct |
1174 ms |
39332 KB |
Output is correct |
40 |
Correct |
1132 ms |
37724 KB |
Output is correct |
41 |
Correct |
1151 ms |
37396 KB |
Output is correct |
42 |
Correct |
1178 ms |
37548 KB |
Output is correct |
43 |
Correct |
1145 ms |
38312 KB |
Output is correct |
44 |
Correct |
1225 ms |
39340 KB |
Output is correct |
45 |
Correct |
1214 ms |
39568 KB |
Output is correct |
46 |
Correct |
1205 ms |
38060 KB |
Output is correct |
47 |
Correct |
1210 ms |
37724 KB |
Output is correct |
48 |
Correct |
1186 ms |
37768 KB |
Output is correct |
49 |
Correct |
1197 ms |
38768 KB |
Output is correct |
50 |
Correct |
1279 ms |
39452 KB |
Output is correct |
51 |
Correct |
1305 ms |
39284 KB |
Output is correct |
52 |
Correct |
1274 ms |
38624 KB |
Output is correct |
53 |
Correct |
1248 ms |
38476 KB |
Output is correct |
54 |
Correct |
1278 ms |
38508 KB |
Output is correct |
55 |
Correct |
1292 ms |
39468 KB |
Output is correct |