#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 nn, int ns, int ne, int x, int v) {
if (ns > x || ne < x) return;
if (x <= ns && ne <= x) {
A[nn] += v;
return;
}
int m = ns + ne >> 1;
update(nn << 1, ns, m, x, v);
update(nn << 1 | 1, m + 1, ne, x, v);
A[nn] = max(A[nn << 1], A[nn << 1 | 1]);
}
void update(int x, int v) { update(1, 0, sz, x, v); }
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;
}
Compilation message
historic.cpp: In member function 'void ST::update(int, int, int, int, int)':
historic.cpp:22:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = ns + ne >> 1;
~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 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 |
3 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 |
504 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 |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
7 ms |
476 KB |
Output is correct |
4 |
Correct |
13 ms |
508 KB |
Output is correct |
5 |
Correct |
27 ms |
760 KB |
Output is correct |
6 |
Correct |
40 ms |
760 KB |
Output is correct |
7 |
Correct |
48 ms |
760 KB |
Output is correct |
8 |
Correct |
26 ms |
764 KB |
Output is correct |
9 |
Correct |
28 ms |
760 KB |
Output is correct |
10 |
Correct |
75 ms |
892 KB |
Output is correct |
11 |
Correct |
76 ms |
888 KB |
Output is correct |
12 |
Correct |
75 ms |
888 KB |
Output is correct |
13 |
Correct |
73 ms |
896 KB |
Output is correct |
14 |
Correct |
67 ms |
832 KB |
Output is correct |
15 |
Correct |
72 ms |
888 KB |
Output is correct |
16 |
Correct |
31 ms |
888 KB |
Output is correct |
17 |
Correct |
12 ms |
888 KB |
Output is correct |
18 |
Correct |
70 ms |
888 KB |
Output is correct |
19 |
Correct |
79 ms |
888 KB |
Output is correct |
20 |
Correct |
79 ms |
888 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 |
3 ms |
380 KB |
Output is correct |
5 |
Correct |
4 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
8 ms |
504 KB |
Output is correct |
8 |
Correct |
13 ms |
760 KB |
Output is correct |
9 |
Correct |
31 ms |
1144 KB |
Output is correct |
10 |
Correct |
12 ms |
1144 KB |
Output is correct |
11 |
Correct |
75 ms |
6728 KB |
Output is correct |
12 |
Correct |
77 ms |
2040 KB |
Output is correct |
13 |
Correct |
132 ms |
3700 KB |
Output is correct |
14 |
Correct |
158 ms |
6412 KB |
Output is correct |
15 |
Correct |
182 ms |
9340 KB |
Output is correct |
16 |
Correct |
101 ms |
7464 KB |
Output is correct |
17 |
Correct |
42 ms |
3092 KB |
Output is correct |
18 |
Correct |
69 ms |
6504 KB |
Output is correct |
19 |
Correct |
90 ms |
9160 KB |
Output is correct |
20 |
Correct |
53 ms |
5488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
1268 KB |
Output is correct |
2 |
Correct |
362 ms |
2064 KB |
Output is correct |
3 |
Correct |
908 ms |
2964 KB |
Output is correct |
4 |
Correct |
1616 ms |
3744 KB |
Output is correct |
5 |
Correct |
2285 ms |
4524 KB |
Output is correct |
6 |
Correct |
2689 ms |
5432 KB |
Output is correct |
7 |
Correct |
2369 ms |
6160 KB |
Output is correct |
8 |
Correct |
1558 ms |
7020 KB |
Output is correct |
9 |
Correct |
819 ms |
7944 KB |
Output is correct |
10 |
Correct |
314 ms |
8032 KB |
Output is correct |
11 |
Correct |
1261 ms |
8160 KB |
Output is correct |
12 |
Correct |
3870 ms |
8180 KB |
Output is correct |
13 |
Execution timed out |
4022 ms |
7264 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |