#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[17005];
vector<long long> ans(Q);
for (int i = 0; i < Q; ++i) {
cin >> in[i][0] >> in[i][1];
qry[in[i][0] / 600].eack(in[i][1], i);
}
for (int i = 1; i <= N; ++i) sort(all(qry[i]));
int s = 1, e = 0;
for (int ii = 0; ii <= N / 600; ++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 |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
2 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
3 ms |
764 KB |
Output is correct |
6 |
Correct |
3 ms |
760 KB |
Output is correct |
7 |
Correct |
3 ms |
760 KB |
Output is correct |
8 |
Correct |
3 ms |
760 KB |
Output is correct |
9 |
Correct |
3 ms |
760 KB |
Output is correct |
10 |
Correct |
3 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
760 KB |
Output is correct |
12 |
Correct |
3 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
16 ms |
808 KB |
Output is correct |
4 |
Correct |
34 ms |
888 KB |
Output is correct |
5 |
Correct |
78 ms |
1016 KB |
Output is correct |
6 |
Correct |
122 ms |
1100 KB |
Output is correct |
7 |
Correct |
122 ms |
1144 KB |
Output is correct |
8 |
Correct |
129 ms |
1144 KB |
Output is correct |
9 |
Correct |
131 ms |
1120 KB |
Output is correct |
10 |
Correct |
126 ms |
1144 KB |
Output is correct |
11 |
Correct |
121 ms |
1180 KB |
Output is correct |
12 |
Correct |
119 ms |
1176 KB |
Output is correct |
13 |
Correct |
118 ms |
1144 KB |
Output is correct |
14 |
Correct |
120 ms |
1144 KB |
Output is correct |
15 |
Correct |
122 ms |
1144 KB |
Output is correct |
16 |
Correct |
130 ms |
1116 KB |
Output is correct |
17 |
Correct |
152 ms |
1272 KB |
Output is correct |
18 |
Correct |
120 ms |
1244 KB |
Output is correct |
19 |
Correct |
116 ms |
1272 KB |
Output is correct |
20 |
Correct |
120 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
4 ms |
760 KB |
Output is correct |
5 |
Correct |
11 ms |
888 KB |
Output is correct |
6 |
Correct |
4 ms |
888 KB |
Output is correct |
7 |
Correct |
6 ms |
1016 KB |
Output is correct |
8 |
Correct |
8 ms |
1144 KB |
Output is correct |
9 |
Runtime error |
14 ms |
2168 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
1356 KB |
Output is correct |
2 |
Runtime error |
16 ms |
3192 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |