#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
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, ll val){
tree[i+=131072] += val;
for(i/=2; i; i/=2) {
tree[i] = max(tree[i*2], tree[i*2+1]);
}
}
inline void removeNum(ll x){
update(x, -v[x]);
}
inline void addNum(ll x){
update(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 = ceil(sqrt(n));
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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
14 ms |
640 KB |
Output is correct |
6 |
Correct |
26 ms |
760 KB |
Output is correct |
7 |
Correct |
30 ms |
760 KB |
Output is correct |
8 |
Correct |
27 ms |
760 KB |
Output is correct |
9 |
Correct |
30 ms |
768 KB |
Output is correct |
10 |
Correct |
27 ms |
768 KB |
Output is correct |
11 |
Correct |
27 ms |
768 KB |
Output is correct |
12 |
Correct |
27 ms |
768 KB |
Output is correct |
13 |
Correct |
27 ms |
768 KB |
Output is correct |
14 |
Correct |
26 ms |
760 KB |
Output is correct |
15 |
Correct |
35 ms |
768 KB |
Output is correct |
16 |
Correct |
28 ms |
760 KB |
Output is correct |
17 |
Correct |
41 ms |
760 KB |
Output is correct |
18 |
Correct |
28 ms |
760 KB |
Output is correct |
19 |
Correct |
25 ms |
768 KB |
Output is correct |
20 |
Correct |
31 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
11 ms |
1024 KB |
Output is correct |
10 |
Correct |
16 ms |
1408 KB |
Output is correct |
11 |
Correct |
78 ms |
4972 KB |
Output is correct |
12 |
Correct |
32 ms |
2300 KB |
Output is correct |
13 |
Correct |
52 ms |
3184 KB |
Output is correct |
14 |
Correct |
77 ms |
4844 KB |
Output is correct |
15 |
Correct |
109 ms |
6620 KB |
Output is correct |
16 |
Correct |
76 ms |
5844 KB |
Output is correct |
17 |
Correct |
39 ms |
3056 KB |
Output is correct |
18 |
Correct |
70 ms |
4844 KB |
Output is correct |
19 |
Correct |
78 ms |
6636 KB |
Output is correct |
20 |
Correct |
37 ms |
4464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
1024 KB |
Output is correct |
2 |
Correct |
192 ms |
1396 KB |
Output is correct |
3 |
Correct |
360 ms |
2036 KB |
Output is correct |
4 |
Correct |
563 ms |
2924 KB |
Output is correct |
5 |
Correct |
797 ms |
3312 KB |
Output is correct |
6 |
Correct |
1000 ms |
3696 KB |
Output is correct |
7 |
Correct |
1249 ms |
4580 KB |
Output is correct |
8 |
Correct |
1679 ms |
5100 KB |
Output is correct |
9 |
Correct |
2810 ms |
5740 KB |
Output is correct |
10 |
Correct |
3532 ms |
5852 KB |
Output is correct |
11 |
Correct |
2552 ms |
5968 KB |
Output is correct |
12 |
Correct |
2137 ms |
5868 KB |
Output is correct |
13 |
Correct |
2190 ms |
5992 KB |
Output is correct |
14 |
Correct |
2333 ms |
6372 KB |
Output is correct |
15 |
Correct |
2407 ms |
6756 KB |
Output is correct |
16 |
Correct |
2314 ms |
6500 KB |
Output is correct |
17 |
Correct |
2324 ms |
6392 KB |
Output is correct |
18 |
Correct |
2354 ms |
6372 KB |
Output is correct |
19 |
Correct |
2334 ms |
6484 KB |
Output is correct |
20 |
Correct |
2297 ms |
6376 KB |
Output is correct |
21 |
Correct |
2301 ms |
7012 KB |
Output is correct |
22 |
Correct |
2313 ms |
7660 KB |
Output is correct |
23 |
Correct |
2290 ms |
7564 KB |
Output is correct |
24 |
Correct |
2283 ms |
7648 KB |
Output is correct |
25 |
Correct |
3541 ms |
7412 KB |
Output is correct |