#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define ff first
#define ss second
#define eack emplace_back
#define all(x) (x).begin(), (x).end()
int X[100005], in[100005][2];
vector<int> xx;
const int SIZE = 1 << 17;
struct ST {
long long A[2 * SIZE];
int sz;
void update(int x, long long v) {
x += SIZE; A[x] += v;
for (x >>= 1; x; x >>= 1) A[x] = max(A[x << 1], A[x << 1 | 1]);
}
long long query() { return A[1]; }
} seg;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int N, Q; cin >> N >> Q;
for (int i = 1; i <= N; ++i) cin >> X[i], xx.eack(X[i]);
sort(all(xx));
xx.erase(unique(all(xx)), xx.end());
for (int i = 1; i <= N; ++i) {
X[i] = lower_bound(all(xx), X[i]) - xx.begin();
}
seg.sz = xx.size() - 1;
vector<pair<pii, int> > qry(Q);
vector<long long> ans(Q);
for (int i = 0; i < Q; ++i) {
cin >> in[i][0] >> in[i][1];
qry.eack(pii(in[i][0] / 320, in[i][1]), i);
}
sort(all(qry));
int s = 1, e = 0;
for (auto i : qry) {
int ns = in[i.ss][0], ne = in[i.ss][1];
for (; s < ns; ++s) seg.update(X[s], -xx[X[s]]);
for (; s > ns; --s) seg.update(X[s - 1], xx[X[s - 1]]);
for (; e < ne; ++e) seg.update(X[e + 1], xx[X[e + 1]]);
for (; e > ne; --e) seg.update(X[e], -xx[X[e]]);
ans[i.ss] = seg.query();
}
for (auto i : ans) cout << i << '\n';
return 0;
}
# |
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 |
380 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 |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
504 KB |
Output is correct |
4 |
Correct |
10 ms |
504 KB |
Output is correct |
5 |
Correct |
20 ms |
632 KB |
Output is correct |
6 |
Correct |
32 ms |
760 KB |
Output is correct |
7 |
Correct |
31 ms |
764 KB |
Output is correct |
8 |
Correct |
32 ms |
764 KB |
Output is correct |
9 |
Correct |
33 ms |
764 KB |
Output is correct |
10 |
Correct |
31 ms |
760 KB |
Output is correct |
11 |
Correct |
34 ms |
764 KB |
Output is correct |
12 |
Correct |
31 ms |
760 KB |
Output is correct |
13 |
Correct |
31 ms |
760 KB |
Output is correct |
14 |
Correct |
32 ms |
760 KB |
Output is correct |
15 |
Correct |
32 ms |
760 KB |
Output is correct |
16 |
Correct |
32 ms |
748 KB |
Output is correct |
17 |
Correct |
39 ms |
808 KB |
Output is correct |
18 |
Correct |
33 ms |
760 KB |
Output is correct |
19 |
Correct |
32 ms |
888 KB |
Output is correct |
20 |
Correct |
32 ms |
892 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 |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
504 KB |
Output is correct |
8 |
Correct |
9 ms |
632 KB |
Output is correct |
9 |
Correct |
16 ms |
892 KB |
Output is correct |
10 |
Correct |
20 ms |
1016 KB |
Output is correct |
11 |
Correct |
79 ms |
5196 KB |
Output is correct |
12 |
Correct |
48 ms |
1396 KB |
Output is correct |
13 |
Correct |
67 ms |
2548 KB |
Output is correct |
14 |
Correct |
92 ms |
4392 KB |
Output is correct |
15 |
Correct |
114 ms |
7136 KB |
Output is correct |
16 |
Correct |
70 ms |
5288 KB |
Output is correct |
17 |
Correct |
38 ms |
2580 KB |
Output is correct |
18 |
Correct |
62 ms |
4796 KB |
Output is correct |
19 |
Correct |
67 ms |
6724 KB |
Output is correct |
20 |
Correct |
40 ms |
4100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
1108 KB |
Output is correct |
2 |
Correct |
160 ms |
1676 KB |
Output is correct |
3 |
Correct |
282 ms |
2328 KB |
Output is correct |
4 |
Correct |
479 ms |
2976 KB |
Output is correct |
5 |
Correct |
678 ms |
3628 KB |
Output is correct |
6 |
Correct |
801 ms |
4104 KB |
Output is correct |
7 |
Correct |
1020 ms |
4656 KB |
Output is correct |
8 |
Correct |
1349 ms |
5420 KB |
Output is correct |
9 |
Correct |
1841 ms |
5980 KB |
Output is correct |
10 |
Correct |
2341 ms |
6292 KB |
Output is correct |
11 |
Correct |
1980 ms |
6324 KB |
Output is correct |
12 |
Correct |
1822 ms |
6308 KB |
Output is correct |
13 |
Correct |
1867 ms |
6420 KB |
Output is correct |
14 |
Correct |
2339 ms |
6808 KB |
Output is correct |
15 |
Correct |
2469 ms |
7184 KB |
Output is correct |
16 |
Correct |
2417 ms |
7028 KB |
Output is correct |
17 |
Correct |
2387 ms |
6960 KB |
Output is correct |
18 |
Correct |
2359 ms |
6880 KB |
Output is correct |
19 |
Correct |
2359 ms |
6824 KB |
Output is correct |
20 |
Correct |
2303 ms |
6880 KB |
Output is correct |
21 |
Correct |
2320 ms |
6880 KB |
Output is correct |
22 |
Correct |
2299 ms |
6852 KB |
Output is correct |
23 |
Correct |
2284 ms |
6720 KB |
Output is correct |
24 |
Correct |
2260 ms |
6880 KB |
Output is correct |
25 |
Correct |
2347 ms |
6724 KB |
Output is correct |