#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int BUCKET = 320;
struct Query{
int s, e, x;
Query(){}
Query(int _s, int _e, int _x): s(_s), e(_e), x(_x) {}
bool operator<(const Query &Q) const{
if (s/BUCKET==Q.s/BUCKET) return e<Q.e;
return s/BUCKET<Q.s/BUCKET;
}
}query[100100];
int a[100100], idx[100100];
ll ans[100100];
vector<int> v;
struct Seg{
ll tree[200200];
int sz;
void init(int n) {sz=n;}
void update(int p, int val){
for (tree[p+=sz]+=val;p>1;p>>=1) tree[p>>1] = max(tree[p], tree[p^1]);
}
ll query(int l){
int r = sz;
ll ret = 0;
for (l +=sz, r += sz;l<r;l>>=1, r>>=1){
if (l&1) ret = max(ret, tree[l++]);
if (r&1) ret = max(tree[--r], ret);
}
return ret;
}
}tree;
int main(){
int n, q;
scanf("%d %d", &n, &q);
for (int i=0;i<n;i++){
scanf("%d", a+i);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i=0;i<n;i++) idx[i] = lower_bound(v.begin(), v.end(), a[i])-v.begin();
for (int i=0;i<q;i++){
scanf("%d %d", &query[i].s, &query[i].e);
query[i].s--;
query[i].x = i;
}
sort(query, query+q);
tree.init((int)v.size());
int s = 0, e = 0;
for (int i=0;i<q;i++){
//printf("%d %d %d\n", query[i].s, query[i].e, query[i].x);
while(e<query[i].e){
tree.update(idx[e], v[idx[e]]);
e++;
}
while(s>query[i].s){
--s;
tree.update(idx[s], v[idx[s]]);
}
while(e>query[i].e){
--e;
tree.update(idx[e], -v[idx[e]]);
}
while(s<query[i].s){
tree.update(idx[s], -v[idx[s]]);
s++;
}
ans[query[i].x] = tree.query(0);
}
for (int i=0;i<q;i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
historic.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d %d", &query[i].s, &query[i].e);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
4 ms |
332 KB |
Output is correct |
20 |
Correct |
8 ms |
460 KB |
Output is correct |
21 |
Correct |
12 ms |
556 KB |
Output is correct |
22 |
Correct |
12 ms |
588 KB |
Output is correct |
23 |
Correct |
11 ms |
460 KB |
Output is correct |
24 |
Correct |
11 ms |
460 KB |
Output is correct |
25 |
Correct |
16 ms |
584 KB |
Output is correct |
26 |
Correct |
17 ms |
588 KB |
Output is correct |
27 |
Correct |
17 ms |
592 KB |
Output is correct |
28 |
Correct |
16 ms |
592 KB |
Output is correct |
29 |
Correct |
16 ms |
588 KB |
Output is correct |
30 |
Correct |
15 ms |
588 KB |
Output is correct |
31 |
Correct |
11 ms |
588 KB |
Output is correct |
32 |
Correct |
8 ms |
588 KB |
Output is correct |
33 |
Correct |
15 ms |
580 KB |
Output is correct |
34 |
Correct |
19 ms |
600 KB |
Output is correct |
35 |
Correct |
17 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
324 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
10 ms |
912 KB |
Output is correct |
10 |
Correct |
12 ms |
1080 KB |
Output is correct |
11 |
Correct |
61 ms |
3852 KB |
Output is correct |
12 |
Correct |
28 ms |
1640 KB |
Output is correct |
13 |
Correct |
46 ms |
2380 KB |
Output is correct |
14 |
Correct |
68 ms |
3684 KB |
Output is correct |
15 |
Correct |
95 ms |
5528 KB |
Output is correct |
16 |
Correct |
67 ms |
4660 KB |
Output is correct |
17 |
Correct |
38 ms |
2364 KB |
Output is correct |
18 |
Correct |
55 ms |
3704 KB |
Output is correct |
19 |
Correct |
62 ms |
5560 KB |
Output is correct |
20 |
Correct |
33 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
4 ms |
332 KB |
Output is correct |
20 |
Correct |
8 ms |
460 KB |
Output is correct |
21 |
Correct |
12 ms |
556 KB |
Output is correct |
22 |
Correct |
12 ms |
588 KB |
Output is correct |
23 |
Correct |
11 ms |
460 KB |
Output is correct |
24 |
Correct |
11 ms |
460 KB |
Output is correct |
25 |
Correct |
16 ms |
584 KB |
Output is correct |
26 |
Correct |
17 ms |
588 KB |
Output is correct |
27 |
Correct |
17 ms |
592 KB |
Output is correct |
28 |
Correct |
16 ms |
592 KB |
Output is correct |
29 |
Correct |
16 ms |
588 KB |
Output is correct |
30 |
Correct |
15 ms |
588 KB |
Output is correct |
31 |
Correct |
11 ms |
588 KB |
Output is correct |
32 |
Correct |
8 ms |
588 KB |
Output is correct |
33 |
Correct |
15 ms |
580 KB |
Output is correct |
34 |
Correct |
19 ms |
600 KB |
Output is correct |
35 |
Correct |
17 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
2 ms |
324 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
3 ms |
460 KB |
Output is correct |
43 |
Correct |
5 ms |
588 KB |
Output is correct |
44 |
Correct |
10 ms |
912 KB |
Output is correct |
45 |
Correct |
12 ms |
1080 KB |
Output is correct |
46 |
Correct |
61 ms |
3852 KB |
Output is correct |
47 |
Correct |
28 ms |
1640 KB |
Output is correct |
48 |
Correct |
46 ms |
2380 KB |
Output is correct |
49 |
Correct |
68 ms |
3684 KB |
Output is correct |
50 |
Correct |
95 ms |
5528 KB |
Output is correct |
51 |
Correct |
67 ms |
4660 KB |
Output is correct |
52 |
Correct |
38 ms |
2364 KB |
Output is correct |
53 |
Correct |
55 ms |
3704 KB |
Output is correct |
54 |
Correct |
62 ms |
5560 KB |
Output is correct |
55 |
Correct |
33 ms |
3712 KB |
Output is correct |
56 |
Correct |
32 ms |
836 KB |
Output is correct |
57 |
Correct |
76 ms |
1296 KB |
Output is correct |
58 |
Correct |
175 ms |
1672 KB |
Output is correct |
59 |
Correct |
321 ms |
2284 KB |
Output is correct |
60 |
Correct |
402 ms |
2752 KB |
Output is correct |
61 |
Correct |
503 ms |
3112 KB |
Output is correct |
62 |
Correct |
429 ms |
3496 KB |
Output is correct |
63 |
Correct |
400 ms |
4156 KB |
Output is correct |
64 |
Correct |
329 ms |
4616 KB |
Output is correct |
65 |
Correct |
257 ms |
4792 KB |
Output is correct |
66 |
Correct |
566 ms |
4920 KB |
Output is correct |
67 |
Correct |
764 ms |
4788 KB |
Output is correct |
68 |
Correct |
1193 ms |
4880 KB |
Output is correct |
69 |
Correct |
1560 ms |
5284 KB |
Output is correct |
70 |
Correct |
1691 ms |
5664 KB |
Output is correct |
71 |
Correct |
1758 ms |
5464 KB |
Output is correct |
72 |
Correct |
1728 ms |
5452 KB |
Output is correct |
73 |
Correct |
1583 ms |
5344 KB |
Output is correct |
74 |
Correct |
1531 ms |
5296 KB |
Output is correct |
75 |
Correct |
1493 ms |
5272 KB |
Output is correct |
76 |
Correct |
1532 ms |
5224 KB |
Output is correct |
77 |
Correct |
1504 ms |
5192 KB |
Output is correct |
78 |
Correct |
1490 ms |
5180 KB |
Output is correct |
79 |
Correct |
1468 ms |
5156 KB |
Output is correct |
80 |
Correct |
230 ms |
5168 KB |
Output is correct |