#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];
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 = 1, r = 0;
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 |
4 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 |
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 |
4 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 |
22 ms |
512 KB |
Output is correct |
6 |
Correct |
34 ms |
760 KB |
Output is correct |
7 |
Correct |
37 ms |
640 KB |
Output is correct |
8 |
Correct |
34 ms |
640 KB |
Output is correct |
9 |
Correct |
28 ms |
640 KB |
Output is correct |
10 |
Correct |
47 ms |
768 KB |
Output is correct |
11 |
Correct |
46 ms |
760 KB |
Output is correct |
12 |
Correct |
46 ms |
760 KB |
Output is correct |
13 |
Correct |
45 ms |
760 KB |
Output is correct |
14 |
Correct |
42 ms |
640 KB |
Output is correct |
15 |
Correct |
44 ms |
760 KB |
Output is correct |
16 |
Correct |
30 ms |
632 KB |
Output is correct |
17 |
Correct |
14 ms |
640 KB |
Output is correct |
18 |
Correct |
44 ms |
760 KB |
Output is correct |
19 |
Correct |
50 ms |
888 KB |
Output is correct |
20 |
Correct |
49 ms |
888 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 |
7 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
512 KB |
Output is correct |
8 |
Correct |
14 ms |
768 KB |
Output is correct |
9 |
Correct |
34 ms |
1280 KB |
Output is correct |
10 |
Correct |
15 ms |
1024 KB |
Output is correct |
11 |
Correct |
124 ms |
4728 KB |
Output is correct |
12 |
Correct |
70 ms |
1536 KB |
Output is correct |
13 |
Correct |
196 ms |
2936 KB |
Output is correct |
14 |
Correct |
293 ms |
7160 KB |
Output is correct |
15 |
Correct |
174 ms |
10232 KB |
Output is correct |
16 |
Correct |
262 ms |
11896 KB |
Output is correct |
17 |
Correct |
70 ms |
2680 KB |
Output is correct |
18 |
Correct |
135 ms |
4576 KB |
Output is correct |
19 |
Correct |
224 ms |
13304 KB |
Output is correct |
20 |
Correct |
108 ms |
10616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
888 KB |
Output is correct |
2 |
Correct |
292 ms |
1492 KB |
Output is correct |
3 |
Correct |
671 ms |
2424 KB |
Output is correct |
4 |
Correct |
1200 ms |
3576 KB |
Output is correct |
5 |
Correct |
1652 ms |
4216 KB |
Output is correct |
6 |
Correct |
1925 ms |
4088 KB |
Output is correct |
7 |
Correct |
1749 ms |
4280 KB |
Output is correct |
8 |
Correct |
1346 ms |
4984 KB |
Output is correct |
9 |
Correct |
900 ms |
5496 KB |
Output is correct |
10 |
Correct |
369 ms |
5880 KB |
Output is correct |
11 |
Correct |
1225 ms |
5752 KB |
Output is correct |
12 |
Correct |
2791 ms |
5880 KB |
Output is correct |
13 |
Execution timed out |
4075 ms |
5368 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |