#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];
unordered_map<ll, int> mp;
void update(int id, ll val, int node = 1, int l = 1, int r = cnt){
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], mp[x[i]] = 0;
for(auto &x : mp) x.second = ++cnt;
for(int i=1;i<=n;i++) id[i] = mp[x[i]];
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 |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 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 |
20 ms |
512 KB |
Output is correct |
6 |
Correct |
33 ms |
640 KB |
Output is correct |
7 |
Correct |
37 ms |
640 KB |
Output is correct |
8 |
Correct |
26 ms |
640 KB |
Output is correct |
9 |
Correct |
26 ms |
640 KB |
Output is correct |
10 |
Correct |
44 ms |
768 KB |
Output is correct |
11 |
Correct |
44 ms |
760 KB |
Output is correct |
12 |
Correct |
46 ms |
760 KB |
Output is correct |
13 |
Correct |
44 ms |
760 KB |
Output is correct |
14 |
Correct |
41 ms |
640 KB |
Output is correct |
15 |
Correct |
43 ms |
640 KB |
Output is correct |
16 |
Correct |
28 ms |
640 KB |
Output is correct |
17 |
Correct |
14 ms |
640 KB |
Output is correct |
18 |
Correct |
42 ms |
640 KB |
Output is correct |
19 |
Correct |
46 ms |
760 KB |
Output is correct |
20 |
Correct |
52 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 |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
512 KB |
Output is correct |
8 |
Correct |
12 ms |
768 KB |
Output is correct |
9 |
Correct |
27 ms |
1152 KB |
Output is correct |
10 |
Correct |
17 ms |
1024 KB |
Output is correct |
11 |
Correct |
123 ms |
4728 KB |
Output is correct |
12 |
Correct |
60 ms |
1536 KB |
Output is correct |
13 |
Correct |
186 ms |
2812 KB |
Output is correct |
14 |
Correct |
252 ms |
6312 KB |
Output is correct |
15 |
Correct |
136 ms |
9348 KB |
Output is correct |
16 |
Correct |
194 ms |
9732 KB |
Output is correct |
17 |
Correct |
60 ms |
2680 KB |
Output is correct |
18 |
Correct |
126 ms |
4472 KB |
Output is correct |
19 |
Correct |
209 ms |
10888 KB |
Output is correct |
20 |
Correct |
81 ms |
8328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
844 KB |
Output is correct |
2 |
Correct |
285 ms |
1528 KB |
Output is correct |
3 |
Correct |
668 ms |
2424 KB |
Output is correct |
4 |
Correct |
1150 ms |
3320 KB |
Output is correct |
5 |
Correct |
1594 ms |
4088 KB |
Output is correct |
6 |
Correct |
1863 ms |
4216 KB |
Output is correct |
7 |
Correct |
1699 ms |
4344 KB |
Output is correct |
8 |
Correct |
1318 ms |
4984 KB |
Output is correct |
9 |
Correct |
858 ms |
5496 KB |
Output is correct |
10 |
Correct |
373 ms |
5752 KB |
Output is correct |
11 |
Correct |
1214 ms |
5724 KB |
Output is correct |
12 |
Correct |
2738 ms |
5880 KB |
Output is correct |
13 |
Execution timed out |
4078 ms |
5112 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |