#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 = 1500;
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 |
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 |
0 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 |
1 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 |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 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 |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
20 ms |
512 KB |
Output is correct |
5 |
Correct |
51 ms |
512 KB |
Output is correct |
6 |
Correct |
80 ms |
760 KB |
Output is correct |
7 |
Correct |
89 ms |
760 KB |
Output is correct |
8 |
Correct |
53 ms |
640 KB |
Output is correct |
9 |
Correct |
53 ms |
640 KB |
Output is correct |
10 |
Correct |
133 ms |
648 KB |
Output is correct |
11 |
Correct |
127 ms |
724 KB |
Output is correct |
12 |
Correct |
125 ms |
768 KB |
Output is correct |
13 |
Correct |
127 ms |
760 KB |
Output is correct |
14 |
Correct |
114 ms |
760 KB |
Output is correct |
15 |
Correct |
125 ms |
712 KB |
Output is correct |
16 |
Correct |
58 ms |
696 KB |
Output is correct |
17 |
Correct |
25 ms |
768 KB |
Output is correct |
18 |
Correct |
115 ms |
760 KB |
Output is correct |
19 |
Correct |
129 ms |
764 KB |
Output is correct |
20 |
Correct |
140 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
14 ms |
1024 KB |
Output is correct |
10 |
Correct |
10 ms |
1536 KB |
Output is correct |
11 |
Correct |
68 ms |
5084 KB |
Output is correct |
12 |
Correct |
36 ms |
2300 KB |
Output is correct |
13 |
Correct |
61 ms |
3012 KB |
Output is correct |
14 |
Correct |
91 ms |
5224 KB |
Output is correct |
15 |
Correct |
121 ms |
6624 KB |
Output is correct |
16 |
Correct |
88 ms |
6380 KB |
Output is correct |
17 |
Correct |
38 ms |
3180 KB |
Output is correct |
18 |
Correct |
75 ms |
4844 KB |
Output is correct |
19 |
Correct |
83 ms |
7148 KB |
Output is correct |
20 |
Correct |
41 ms |
4976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
1016 KB |
Output is correct |
2 |
Correct |
483 ms |
1392 KB |
Output is correct |
3 |
Correct |
1087 ms |
1908 KB |
Output is correct |
4 |
Correct |
1692 ms |
2860 KB |
Output is correct |
5 |
Correct |
2337 ms |
3212 KB |
Output is correct |
6 |
Correct |
2295 ms |
3684 KB |
Output is correct |
7 |
Correct |
1803 ms |
4440 KB |
Output is correct |
8 |
Correct |
1179 ms |
5092 KB |
Output is correct |
9 |
Correct |
660 ms |
5612 KB |
Output is correct |
10 |
Correct |
251 ms |
5740 KB |
Output is correct |
11 |
Correct |
1000 ms |
5788 KB |
Output is correct |
12 |
Correct |
2503 ms |
5736 KB |
Output is correct |
13 |
Execution timed out |
4088 ms |
4716 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |