#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int sz = 300;
struct q{int s,e,n;}qr[100005];
bool cmp(q a, q b){return a.s / sz == b.s / sz ? (a.e < b.e) : (a.s < b.s);}
int a[100005],n,q;
vector<int> p;
long long res[100005];
int lim;
inline void add(int pos, int val, long long* tree){
pos += lim;
tree[pos] += 1ll * val * p[pos-lim];
while(pos > 1){
pos >>= 1;
tree[pos] = max(tree[2*pos],tree[2*pos+1]);
}
}
inline void solve(){
long long tree[270000] = {};
for(lim = 1; lim <= p.size(); lim <<= 1);
int s = 1, e = 0;
for (int i=0; i<q; i++) {
while (s < qr[i].s) {
add(a[s],-1,tree);
s++;
}
while (qr[i].s < s) {
s--;
add(a[s],1,tree);
}
while (e < qr[i].e) {
e++;
add(a[e],1,tree);
}
while (qr[i].e < e) {
add(a[e],-1,tree);
e--;
}
res[qr[i].n] = tree[1];
}
}
int main(){
scanf("%d %d",&n,&q);
for (int i=1; i<=n; i++) {
scanf("%d",&a[i]);
p.push_back(a[i]);
}
for (int i=0; i<q; i++) {
scanf("%d %d",&qr[i].s,&qr[i].e);
qr[i].n = i;
}
sort(qr,qr+q,cmp);
sort(p.begin(),p.end());
p.resize(unique(p.begin(),p.end()) - p.begin());
for (int i=1; i<=n; i++) {
a[i] = (int)(lower_bound(p.begin(),p.end(),a[i]) - p.begin());
}
solve();
for (int i=0; i<q; i++) {
printf("%lld\n",res[i]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5568 KB |
Output is correct |
2 |
Correct |
0 ms |
5576 KB |
Output is correct |
3 |
Correct |
0 ms |
5572 KB |
Output is correct |
4 |
Correct |
0 ms |
5572 KB |
Output is correct |
5 |
Correct |
0 ms |
5572 KB |
Output is correct |
6 |
Correct |
0 ms |
5568 KB |
Output is correct |
7 |
Correct |
0 ms |
5572 KB |
Output is correct |
8 |
Correct |
0 ms |
5572 KB |
Output is correct |
9 |
Correct |
0 ms |
5572 KB |
Output is correct |
10 |
Correct |
0 ms |
5572 KB |
Output is correct |
11 |
Correct |
0 ms |
5572 KB |
Output is correct |
12 |
Correct |
0 ms |
5568 KB |
Output is correct |
13 |
Correct |
0 ms |
5568 KB |
Output is correct |
14 |
Correct |
0 ms |
5568 KB |
Output is correct |
15 |
Correct |
0 ms |
5568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5572 KB |
Output is correct |
2 |
Correct |
0 ms |
5568 KB |
Output is correct |
3 |
Correct |
4 ms |
5576 KB |
Output is correct |
4 |
Correct |
4 ms |
5572 KB |
Output is correct |
5 |
Correct |
16 ms |
5572 KB |
Output is correct |
6 |
Correct |
20 ms |
5572 KB |
Output is correct |
7 |
Correct |
24 ms |
5576 KB |
Output is correct |
8 |
Correct |
12 ms |
5572 KB |
Output is correct |
9 |
Correct |
16 ms |
5572 KB |
Output is correct |
10 |
Correct |
32 ms |
5568 KB |
Output is correct |
11 |
Correct |
28 ms |
5572 KB |
Output is correct |
12 |
Correct |
32 ms |
5568 KB |
Output is correct |
13 |
Correct |
24 ms |
5576 KB |
Output is correct |
14 |
Correct |
32 ms |
5572 KB |
Output is correct |
15 |
Correct |
32 ms |
5568 KB |
Output is correct |
16 |
Correct |
20 ms |
5572 KB |
Output is correct |
17 |
Correct |
8 ms |
5572 KB |
Output is correct |
18 |
Correct |
28 ms |
5572 KB |
Output is correct |
19 |
Correct |
32 ms |
5568 KB |
Output is correct |
20 |
Correct |
32 ms |
5568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5572 KB |
Output is correct |
2 |
Correct |
0 ms |
5576 KB |
Output is correct |
3 |
Correct |
0 ms |
5572 KB |
Output is correct |
4 |
Correct |
0 ms |
5568 KB |
Output is correct |
5 |
Correct |
0 ms |
5576 KB |
Output is correct |
6 |
Correct |
0 ms |
5572 KB |
Output is correct |
7 |
Correct |
4 ms |
5572 KB |
Output is correct |
8 |
Correct |
0 ms |
5572 KB |
Output is correct |
9 |
Correct |
8 ms |
5704 KB |
Output is correct |
10 |
Correct |
8 ms |
5828 KB |
Output is correct |
11 |
Correct |
72 ms |
6092 KB |
Output is correct |
12 |
Correct |
36 ms |
6092 KB |
Output is correct |
13 |
Correct |
56 ms |
6084 KB |
Output is correct |
14 |
Correct |
92 ms |
6092 KB |
Output is correct |
15 |
Correct |
104 ms |
6088 KB |
Output is correct |
16 |
Correct |
68 ms |
6088 KB |
Output is correct |
17 |
Correct |
36 ms |
6084 KB |
Output is correct |
18 |
Correct |
72 ms |
6092 KB |
Output is correct |
19 |
Correct |
76 ms |
6092 KB |
Output is correct |
20 |
Correct |
40 ms |
6088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
5572 KB |
Output is correct |
2 |
Correct |
156 ms |
5704 KB |
Output is correct |
3 |
Correct |
380 ms |
5704 KB |
Output is correct |
4 |
Correct |
652 ms |
5832 KB |
Output is correct |
5 |
Correct |
956 ms |
5836 KB |
Output is correct |
6 |
Correct |
1104 ms |
5828 KB |
Output is correct |
7 |
Correct |
1084 ms |
6092 KB |
Output is correct |
8 |
Correct |
892 ms |
6092 KB |
Output is correct |
9 |
Correct |
616 ms |
6088 KB |
Output is correct |
10 |
Correct |
448 ms |
6088 KB |
Output is correct |
11 |
Correct |
900 ms |
6092 KB |
Output is correct |
12 |
Correct |
1756 ms |
6092 KB |
Output is correct |
13 |
Correct |
2620 ms |
6084 KB |
Output is correct |
14 |
Correct |
3492 ms |
6088 KB |
Output is correct |
15 |
Correct |
3880 ms |
6088 KB |
Output is correct |
16 |
Correct |
3808 ms |
6084 KB |
Output is correct |
17 |
Correct |
3788 ms |
6088 KB |
Output is correct |
18 |
Correct |
3748 ms |
6088 KB |
Output is correct |
19 |
Correct |
3744 ms |
6084 KB |
Output is correct |
20 |
Correct |
3484 ms |
6088 KB |
Output is correct |
21 |
Correct |
3448 ms |
6088 KB |
Output is correct |
22 |
Correct |
3464 ms |
6088 KB |
Output is correct |
23 |
Correct |
3436 ms |
6088 KB |
Output is correct |
24 |
Correct |
3452 ms |
6088 KB |
Output is correct |
25 |
Correct |
308 ms |
6084 KB |
Output is correct |