#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<pii> qry[320];
vector<long long> ans(Q);
for (int i = 0; i < Q; ++i) {
cin >> in[i][0] >> in[i][1];
qry[in[i][0] / 400].eack(in[i][1], i);
}
int s = 1, e = 0;
for (int ii = 0; ii <= N / 400; ++ii) {
sort(all(qry[ii]));
for (auto i : qry[ii]) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
12 ms |
504 KB |
Output is correct |
5 |
Correct |
20 ms |
504 KB |
Output is correct |
6 |
Correct |
36 ms |
632 KB |
Output is correct |
7 |
Correct |
34 ms |
632 KB |
Output is correct |
8 |
Correct |
36 ms |
632 KB |
Output is correct |
9 |
Correct |
38 ms |
632 KB |
Output is correct |
10 |
Correct |
35 ms |
632 KB |
Output is correct |
11 |
Correct |
36 ms |
632 KB |
Output is correct |
12 |
Correct |
35 ms |
632 KB |
Output is correct |
13 |
Correct |
35 ms |
632 KB |
Output is correct |
14 |
Correct |
35 ms |
632 KB |
Output is correct |
15 |
Correct |
35 ms |
632 KB |
Output is correct |
16 |
Correct |
38 ms |
640 KB |
Output is correct |
17 |
Correct |
44 ms |
632 KB |
Output is correct |
18 |
Correct |
35 ms |
632 KB |
Output is correct |
19 |
Correct |
35 ms |
760 KB |
Output is correct |
20 |
Correct |
35 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
4 ms |
504 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
13 ms |
888 KB |
Output is correct |
10 |
Correct |
15 ms |
1016 KB |
Output is correct |
11 |
Correct |
64 ms |
4208 KB |
Output is correct |
12 |
Correct |
35 ms |
1276 KB |
Output is correct |
13 |
Correct |
50 ms |
2176 KB |
Output is correct |
14 |
Correct |
69 ms |
3572 KB |
Output is correct |
15 |
Correct |
90 ms |
5748 KB |
Output is correct |
16 |
Correct |
69 ms |
4588 KB |
Output is correct |
17 |
Correct |
35 ms |
2136 KB |
Output is correct |
18 |
Correct |
58 ms |
3828 KB |
Output is correct |
19 |
Correct |
62 ms |
5596 KB |
Output is correct |
20 |
Correct |
37 ms |
3472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
816 KB |
Output is correct |
2 |
Correct |
167 ms |
1372 KB |
Output is correct |
3 |
Correct |
285 ms |
1912 KB |
Output is correct |
4 |
Correct |
494 ms |
2400 KB |
Output is correct |
5 |
Correct |
638 ms |
2936 KB |
Output is correct |
6 |
Correct |
758 ms |
3320 KB |
Output is correct |
7 |
Correct |
941 ms |
3824 KB |
Output is correct |
8 |
Correct |
1259 ms |
4340 KB |
Output is correct |
9 |
Correct |
1703 ms |
4872 KB |
Output is correct |
10 |
Correct |
2128 ms |
5148 KB |
Output is correct |
11 |
Correct |
1789 ms |
5144 KB |
Output is correct |
12 |
Correct |
1651 ms |
5112 KB |
Output is correct |
13 |
Correct |
1697 ms |
5204 KB |
Output is correct |
14 |
Correct |
2100 ms |
5588 KB |
Output is correct |
15 |
Correct |
2187 ms |
5980 KB |
Output is correct |
16 |
Correct |
2183 ms |
5880 KB |
Output is correct |
17 |
Correct |
2176 ms |
5736 KB |
Output is correct |
18 |
Correct |
2143 ms |
5604 KB |
Output is correct |
19 |
Correct |
2114 ms |
5620 KB |
Output is correct |
20 |
Correct |
2091 ms |
5572 KB |
Output is correct |
21 |
Correct |
2077 ms |
5500 KB |
Output is correct |
22 |
Correct |
2074 ms |
5620 KB |
Output is correct |
23 |
Correct |
2089 ms |
5540 KB |
Output is correct |
24 |
Correct |
2047 ms |
5472 KB |
Output is correct |
25 |
Correct |
2128 ms |
5476 KB |
Output is correct |