#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
int sqrtN;
struct Query{
int l, r, idx;
Query(){}
Query(int l, int r, int idx): l(l), r(r), idx(idx){}
bool operator<(const Query &tmp)const{
if(l/sqrtN != tmp.l/sqrtN) return l < tmp.l;
return r < tmp.r;
}
};
int n, q, k;
ll arr[100002];
vector<Query> vec;
ll cnt[100002];
ll ans[100002];
vector<ll> v;
ll tree[400002];
inline void update(int i, int l, int r, int idx, ll val){
if(l==r){
tree[i] += val;
return;
}
int m = (l+r)>>1;
if(idx<=m) update(i*2, l, m, idx, val);
else update(i*2+1, m+1, r, idx, val);
tree[i] = max(tree[i*2], tree[i*2+1]);
}
inline void removeNum(ll x){
update(1, 0, k-1, x, -v[x]);
}
inline void addNum(ll x){
update(1, 0, k-1, x, v[x]);
}
void renumber(){
for(int i=0; i<n; i++) v.push_back(arr[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i=0; i<n; i++) arr[i] = lower_bound(v.begin(), v.end(), arr[i]) - v.begin();
k = (int)v.size();
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
sqrtN = 800;
for(int i=0; i<n; i++) cin >> arr[i];
renumber();
for(int i=1; i<=q; i++){
int l, r;
cin >> l >> r;
l--, r--;
vec.push_back(Query(l, r, i));
}
sort(vec.begin(), vec.end());
int s = 0, e = -1;
for(int i=0; i<q; i++){
Query tmp = vec[i];
int l = tmp.l, r = tmp.r, idx = tmp.idx;
while(e < r) addNum(arr[++e]);
while(s > l) addNum(arr[--s]);
while(e > r) removeNum(arr[e--]);
while(s < l) removeNum(arr[s++]);
ans[idx] = tree[1];
}
for(int i=1; i<=q; i++){
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
12 ms |
512 KB |
Output is correct |
5 |
Correct |
29 ms |
512 KB |
Output is correct |
6 |
Correct |
41 ms |
640 KB |
Output is correct |
7 |
Correct |
46 ms |
640 KB |
Output is correct |
8 |
Correct |
29 ms |
640 KB |
Output is correct |
9 |
Correct |
29 ms |
640 KB |
Output is correct |
10 |
Correct |
66 ms |
760 KB |
Output is correct |
11 |
Correct |
64 ms |
760 KB |
Output is correct |
12 |
Correct |
61 ms |
760 KB |
Output is correct |
13 |
Correct |
61 ms |
760 KB |
Output is correct |
14 |
Correct |
61 ms |
760 KB |
Output is correct |
15 |
Correct |
58 ms |
760 KB |
Output is correct |
16 |
Correct |
34 ms |
768 KB |
Output is correct |
17 |
Correct |
12 ms |
656 KB |
Output is correct |
18 |
Correct |
58 ms |
712 KB |
Output is correct |
19 |
Correct |
68 ms |
760 KB |
Output is correct |
20 |
Correct |
74 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
14 ms |
1024 KB |
Output is correct |
10 |
Correct |
9 ms |
1408 KB |
Output is correct |
11 |
Correct |
73 ms |
4948 KB |
Output is correct |
12 |
Correct |
34 ms |
2300 KB |
Output is correct |
13 |
Correct |
61 ms |
3056 KB |
Output is correct |
14 |
Correct |
88 ms |
5228 KB |
Output is correct |
15 |
Correct |
130 ms |
6636 KB |
Output is correct |
16 |
Correct |
88 ms |
6256 KB |
Output is correct |
17 |
Correct |
38 ms |
3064 KB |
Output is correct |
18 |
Correct |
68 ms |
4844 KB |
Output is correct |
19 |
Correct |
82 ms |
7140 KB |
Output is correct |
20 |
Correct |
39 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
1000 KB |
Output is correct |
2 |
Correct |
281 ms |
1396 KB |
Output is correct |
3 |
Correct |
590 ms |
1984 KB |
Output is correct |
4 |
Correct |
963 ms |
2924 KB |
Output is correct |
5 |
Correct |
1386 ms |
3200 KB |
Output is correct |
6 |
Correct |
1393 ms |
3696 KB |
Output is correct |
7 |
Correct |
1179 ms |
4588 KB |
Output is correct |
8 |
Correct |
780 ms |
5100 KB |
Output is correct |
9 |
Correct |
467 ms |
5600 KB |
Output is correct |
10 |
Correct |
220 ms |
5740 KB |
Output is correct |
11 |
Correct |
746 ms |
5740 KB |
Output is correct |
12 |
Correct |
1823 ms |
5848 KB |
Output is correct |
13 |
Correct |
2891 ms |
5856 KB |
Output is correct |
14 |
Correct |
3664 ms |
6240 KB |
Output is correct |
15 |
Correct |
3949 ms |
6756 KB |
Output is correct |
16 |
Correct |
3811 ms |
6752 KB |
Output is correct |
17 |
Correct |
3801 ms |
6764 KB |
Output is correct |
18 |
Correct |
3711 ms |
6764 KB |
Output is correct |
19 |
Correct |
3735 ms |
6892 KB |
Output is correct |
20 |
Correct |
3644 ms |
6376 KB |
Output is correct |
21 |
Correct |
3605 ms |
6628 KB |
Output is correct |
22 |
Correct |
3538 ms |
6764 KB |
Output is correct |
23 |
Correct |
3542 ms |
6636 KB |
Output is correct |
24 |
Correct |
3608 ms |
6764 KB |
Output is correct |
25 |
Correct |
186 ms |
6492 KB |
Output is correct |