#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define mid ((l+r)>>1)
const int sq = 400;
inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
if (pow == 0) {
return 0;
}
int hpow = 1 << (pow-1);
int seg = (x < hpow) ? (
(y < hpow) ? 0 : 3
) : (
(y < hpow) ? 1 : 2
);
seg = (seg + rotate) & 3;
const int rotateDelta[4] = {3, 0, 0, 1};
int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
int nrot = (rotate + rotateDelta[seg]) & 3;
int64_t subSquareSize = int64_t(1) << (2*pow - 2);
int64_t ans = seg * subSquareSize;
int64_t add = hilbertOrder(nx, ny, pow-1, nrot);
ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
return ans;
}
struct query{
int l, r, id;
long long order;
void init(){
order = hilbertOrder(l, r, 21, 0);
}
bool operator < (const query &o) const{
return order < o.order;
}
}q[100100];
int n, m, cnt, id[100100];
ll x[100100], tree[400400], ans[100100];
vector<ll> v;
void update(int id, ll val, int node = 1, int l = 0, int r = v.size()-1){
if(id < l || r < id) return;
if(l == r){
tree[node] += val;
return;
}
update(id, val, node*2, l, mid), update(id, val, node*2+1, mid+1, r);
tree[node] = max(tree[node*2], tree[node*2+1]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> x[i], v.push_back(x[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i=1;i<=n;i++) id[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin();
for(int i=1;i<=m;i++) cin >> q[i].l >> q[i].r, q[i].id = i, q[i].init();
sort(q+1, q+1+m);
int l = q[1].l, r = q[1].l-1;
for(int i=1;i<=m;i++){
while(l > q[i].l) l--, update(id[l], x[l]);
while(r < q[i].r) r++, update(id[r], x[r]);
while(l < q[i].l) update(id[l], -x[l]), l++;
while(r > q[i].r) update(id[r], -x[r]), r--;
ans[q[i].id] = tree[1];
}
for(int i=1;i<=m;i++) cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
7 ms |
384 KB |
Output is correct |
4 |
Correct |
10 ms |
512 KB |
Output is correct |
5 |
Correct |
21 ms |
640 KB |
Output is correct |
6 |
Correct |
33 ms |
764 KB |
Output is correct |
7 |
Correct |
36 ms |
760 KB |
Output is correct |
8 |
Correct |
28 ms |
768 KB |
Output is correct |
9 |
Correct |
29 ms |
760 KB |
Output is correct |
10 |
Correct |
48 ms |
768 KB |
Output is correct |
11 |
Correct |
47 ms |
768 KB |
Output is correct |
12 |
Correct |
47 ms |
768 KB |
Output is correct |
13 |
Correct |
45 ms |
768 KB |
Output is correct |
14 |
Correct |
51 ms |
768 KB |
Output is correct |
15 |
Correct |
45 ms |
768 KB |
Output is correct |
16 |
Correct |
29 ms |
768 KB |
Output is correct |
17 |
Correct |
15 ms |
896 KB |
Output is correct |
18 |
Correct |
43 ms |
760 KB |
Output is correct |
19 |
Correct |
47 ms |
768 KB |
Output is correct |
20 |
Correct |
48 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
8 ms |
512 KB |
Output is correct |
7 |
Correct |
10 ms |
640 KB |
Output is correct |
8 |
Correct |
12 ms |
768 KB |
Output is correct |
9 |
Correct |
29 ms |
1152 KB |
Output is correct |
10 |
Correct |
16 ms |
1532 KB |
Output is correct |
11 |
Correct |
124 ms |
5612 KB |
Output is correct |
12 |
Correct |
71 ms |
2424 KB |
Output is correct |
13 |
Correct |
197 ms |
3440 KB |
Output is correct |
14 |
Correct |
268 ms |
5604 KB |
Output is correct |
15 |
Correct |
140 ms |
7664 KB |
Output is correct |
16 |
Correct |
215 ms |
6700 KB |
Output is correct |
17 |
Correct |
69 ms |
3564 KB |
Output is correct |
18 |
Correct |
135 ms |
5356 KB |
Output is correct |
19 |
Correct |
198 ms |
7792 KB |
Output is correct |
20 |
Correct |
74 ms |
5360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
1144 KB |
Output is correct |
2 |
Correct |
299 ms |
1760 KB |
Output is correct |
3 |
Correct |
708 ms |
2496 KB |
Output is correct |
4 |
Correct |
1196 ms |
3184 KB |
Output is correct |
5 |
Correct |
1687 ms |
3744 KB |
Output is correct |
6 |
Correct |
1934 ms |
4592 KB |
Output is correct |
7 |
Correct |
1770 ms |
4956 KB |
Output is correct |
8 |
Correct |
1340 ms |
5744 KB |
Output is correct |
9 |
Correct |
904 ms |
6376 KB |
Output is correct |
10 |
Correct |
359 ms |
6768 KB |
Output is correct |
11 |
Correct |
1242 ms |
6712 KB |
Output is correct |
12 |
Correct |
2802 ms |
6636 KB |
Output is correct |
13 |
Execution timed out |
4097 ms |
5744 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |