/**
* author: zhougz
* created: 03/01/2022 10:55:06
**/
#include "bits/stdc++.h"
using namespace std;
int blk_sz;
struct query {
int l, r, idx;
bool operator<(const query &rhs) const {
return l / blk_sz != rhs.l / blk_sz ? l < rhs.l : (r < rhs.r) ^ ((l / blk_sz) & 1);
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
blk_sz = n / sqrt(q);
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vector<query> queries(q);
for (int i = 0; i < q; i++) {
cin >> queries[i].l >> queries[i].r;
queries[i].l--;
queries[i].r--;
queries[i].idx = i;
}
sort(queries.begin(), queries.end());
vector<int> v(arr.begin(), arr.end());
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
vector<int> idx(n);
for (int i = 0; i < n; i++) {
idx[i] = lower_bound(v.begin(), v.end(), arr[i]) - v.begin();
}
const int N = 1 << (int)ceil(log2(v.size()));
vector<long long> t(2 * N);
auto update = [&](int x, int val) {
t[x += N] += val;
while ((x >>= 1) != 0) {
t[x] = max(t[x << 1], t[(x << 1) + 1]);
}
};
auto add = [&](int x) {
update(idx[x], arr[x]);
};
auto remove = [&](int x) {
update(idx[x], -arr[x]);
};
vector<long long> res(q);
int cur_l = 0, cur_r = -1;
for (const auto &[l, r, idx] : queries) {
while (cur_l < l) {
remove(cur_l++);
}
while (cur_l > l) {
add(--cur_l);
}
while (cur_r < r) {
add(++cur_r);
}
while (cur_r > r) {
remove(cur_r--);
}
res[idx] = t[1];
}
for (auto x : res) {
cout << x << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
4 ms |
412 KB |
Output is correct |
21 |
Correct |
8 ms |
460 KB |
Output is correct |
22 |
Correct |
7 ms |
508 KB |
Output is correct |
23 |
Correct |
6 ms |
456 KB |
Output is correct |
24 |
Correct |
6 ms |
456 KB |
Output is correct |
25 |
Correct |
9 ms |
516 KB |
Output is correct |
26 |
Correct |
9 ms |
580 KB |
Output is correct |
27 |
Correct |
10 ms |
588 KB |
Output is correct |
28 |
Correct |
11 ms |
600 KB |
Output is correct |
29 |
Correct |
10 ms |
664 KB |
Output is correct |
30 |
Correct |
10 ms |
572 KB |
Output is correct |
31 |
Correct |
7 ms |
552 KB |
Output is correct |
32 |
Correct |
5 ms |
560 KB |
Output is correct |
33 |
Correct |
9 ms |
588 KB |
Output is correct |
34 |
Correct |
9 ms |
588 KB |
Output is correct |
35 |
Correct |
10 ms |
628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
3 ms |
396 KB |
Output is correct |
8 |
Correct |
5 ms |
580 KB |
Output is correct |
9 |
Correct |
9 ms |
904 KB |
Output is correct |
10 |
Correct |
6 ms |
1156 KB |
Output is correct |
11 |
Correct |
48 ms |
5292 KB |
Output is correct |
12 |
Correct |
28 ms |
2244 KB |
Output is correct |
13 |
Correct |
41 ms |
3248 KB |
Output is correct |
14 |
Correct |
62 ms |
5552 KB |
Output is correct |
15 |
Correct |
83 ms |
7476 KB |
Output is correct |
16 |
Correct |
59 ms |
6460 KB |
Output is correct |
17 |
Correct |
28 ms |
2756 KB |
Output is correct |
18 |
Correct |
56 ms |
5180 KB |
Output is correct |
19 |
Correct |
59 ms |
7772 KB |
Output is correct |
20 |
Correct |
29 ms |
5160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
4 ms |
412 KB |
Output is correct |
21 |
Correct |
8 ms |
460 KB |
Output is correct |
22 |
Correct |
7 ms |
508 KB |
Output is correct |
23 |
Correct |
6 ms |
456 KB |
Output is correct |
24 |
Correct |
6 ms |
456 KB |
Output is correct |
25 |
Correct |
9 ms |
516 KB |
Output is correct |
26 |
Correct |
9 ms |
580 KB |
Output is correct |
27 |
Correct |
10 ms |
588 KB |
Output is correct |
28 |
Correct |
11 ms |
600 KB |
Output is correct |
29 |
Correct |
10 ms |
664 KB |
Output is correct |
30 |
Correct |
10 ms |
572 KB |
Output is correct |
31 |
Correct |
7 ms |
552 KB |
Output is correct |
32 |
Correct |
5 ms |
560 KB |
Output is correct |
33 |
Correct |
9 ms |
588 KB |
Output is correct |
34 |
Correct |
9 ms |
588 KB |
Output is correct |
35 |
Correct |
10 ms |
628 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
352 KB |
Output is correct |
42 |
Correct |
3 ms |
396 KB |
Output is correct |
43 |
Correct |
5 ms |
580 KB |
Output is correct |
44 |
Correct |
9 ms |
904 KB |
Output is correct |
45 |
Correct |
6 ms |
1156 KB |
Output is correct |
46 |
Correct |
48 ms |
5292 KB |
Output is correct |
47 |
Correct |
28 ms |
2244 KB |
Output is correct |
48 |
Correct |
41 ms |
3248 KB |
Output is correct |
49 |
Correct |
62 ms |
5552 KB |
Output is correct |
50 |
Correct |
83 ms |
7476 KB |
Output is correct |
51 |
Correct |
59 ms |
6460 KB |
Output is correct |
52 |
Correct |
28 ms |
2756 KB |
Output is correct |
53 |
Correct |
56 ms |
5180 KB |
Output is correct |
54 |
Correct |
59 ms |
7772 KB |
Output is correct |
55 |
Correct |
29 ms |
5160 KB |
Output is correct |
56 |
Correct |
23 ms |
744 KB |
Output is correct |
57 |
Correct |
54 ms |
1428 KB |
Output is correct |
58 |
Correct |
124 ms |
2168 KB |
Output is correct |
59 |
Correct |
213 ms |
2884 KB |
Output is correct |
60 |
Correct |
296 ms |
3516 KB |
Output is correct |
61 |
Correct |
360 ms |
4172 KB |
Output is correct |
62 |
Correct |
304 ms |
4700 KB |
Output is correct |
63 |
Correct |
263 ms |
5556 KB |
Output is correct |
64 |
Correct |
209 ms |
6212 KB |
Output is correct |
65 |
Correct |
150 ms |
6212 KB |
Output is correct |
66 |
Correct |
300 ms |
6460 KB |
Output is correct |
67 |
Correct |
487 ms |
6352 KB |
Output is correct |
68 |
Correct |
720 ms |
6588 KB |
Output is correct |
69 |
Correct |
935 ms |
7116 KB |
Output is correct |
70 |
Correct |
1039 ms |
7600 KB |
Output is correct |
71 |
Correct |
1008 ms |
7640 KB |
Output is correct |
72 |
Correct |
1004 ms |
7748 KB |
Output is correct |
73 |
Correct |
1000 ms |
7616 KB |
Output is correct |
74 |
Correct |
984 ms |
7624 KB |
Output is correct |
75 |
Correct |
912 ms |
7108 KB |
Output is correct |
76 |
Correct |
909 ms |
7228 KB |
Output is correct |
77 |
Correct |
886 ms |
7356 KB |
Output is correct |
78 |
Correct |
927 ms |
7104 KB |
Output is correct |
79 |
Correct |
905 ms |
7120 KB |
Output is correct |
80 |
Correct |
135 ms |
6976 KB |
Output is correct |