#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int imp[100010];
typedef long long ll;
ll ans[100010];
int in;
int data[100010];
int bck;
struct qr{int ind,l,r; bool operator<(const qr& other) const { return l/bck==other.l/bck?r<other.r:l<other.l; }; } query[100010];
int n,q;
ll tree[262144];
int main()
{
scanf("%d%d",&n,&q); int i; for(i=1;i<=n;++i) scanf("%d",data+i),imp[i]=data[i];
bck=n/sqrt(q);
sort(imp,imp+n);in=unique(imp,imp+n)-imp;
int a,b;
for(i=0;i<q;++i) scanf("%d%d",&a,&b),query[i].ind=i,query[i].l=a,query[i].r=b;
sort(query,query+q);
int l=query[0].l, r=query[0].r;
int tmp,ind;
for(i=l;i<=r;++i) {
tmp=data[i];
ind=lower_bound(imp,imp+in,tmp)-imp;
ind+=131072;
tree[ind]+=tmp;
ind/=2;
while(ind) tree[ind]=max(tree[ind*2],tree[ind*2+1]), ind/=2;
}
ans[query[0].ind]=tree[1];
int ql,qr;
for(i=1;i<q;++i){
ql=query[i].l; qr=query[i].r;
while(ql<=l-1) {
tmp=data[--l];
ind=lower_bound(imp,imp+in,tmp)-imp;
ind+=131072;
tree[ind]+=tmp;
ind/=2;
while(ind) tree[ind]=max(tree[ind*2],tree[ind*2+1]), ind/=2;
}
while(r+1<=qr) {
tmp=data[++r];
ind=lower_bound(imp,imp+in,tmp)-imp;
ind+=131072;
tree[ind]+=tmp;
ind/=2;
while(ind) tree[ind]=max(tree[ind*2],tree[ind*2+1]), ind/=2;
}
while(l<ql) {
tmp=data[l++];
ind=lower_bound(imp,imp+in,tmp)-imp;
ind+=131072;
tree[ind]-=tmp;
ind/=2;
while(ind) tree[ind]=max(tree[ind*2],tree[ind*2+1]), ind/=2;
}
while(qr<r) {
tmp=data[r--];
ind=lower_bound(imp,imp+in,tmp)-imp;
ind+=131072;
tree[ind]-=tmp;
ind/=2;
while(ind) tree[ind]=max(tree[ind*2],tree[ind*2+1]), ind/=2;
}
ans[query[i].ind]=tree[1];
}
for(i=0;i<q;++i) printf("%lld\n",ans[i]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5892 KB |
Output is correct |
2 |
Correct |
0 ms |
5892 KB |
Output is correct |
3 |
Correct |
0 ms |
5892 KB |
Output is correct |
4 |
Correct |
0 ms |
5892 KB |
Output is correct |
5 |
Correct |
0 ms |
5892 KB |
Output is correct |
6 |
Correct |
0 ms |
5892 KB |
Output is correct |
7 |
Correct |
0 ms |
5892 KB |
Output is correct |
8 |
Correct |
0 ms |
5892 KB |
Output is correct |
9 |
Correct |
0 ms |
5892 KB |
Output is correct |
10 |
Correct |
0 ms |
5892 KB |
Output is correct |
11 |
Correct |
0 ms |
5892 KB |
Output is correct |
12 |
Correct |
0 ms |
5892 KB |
Output is correct |
13 |
Correct |
0 ms |
5892 KB |
Output is correct |
14 |
Correct |
0 ms |
5892 KB |
Output is correct |
15 |
Correct |
0 ms |
5892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5892 KB |
Output is correct |
2 |
Correct |
2 ms |
5892 KB |
Output is correct |
3 |
Correct |
4 ms |
5892 KB |
Output is correct |
4 |
Correct |
11 ms |
5892 KB |
Output is correct |
5 |
Correct |
29 ms |
5892 KB |
Output is correct |
6 |
Correct |
53 ms |
5892 KB |
Output is correct |
7 |
Correct |
57 ms |
5892 KB |
Output is correct |
8 |
Correct |
48 ms |
5892 KB |
Output is correct |
9 |
Correct |
49 ms |
5892 KB |
Output is correct |
10 |
Correct |
71 ms |
5892 KB |
Output is correct |
11 |
Correct |
69 ms |
5892 KB |
Output is correct |
12 |
Correct |
68 ms |
5892 KB |
Output is correct |
13 |
Correct |
68 ms |
5892 KB |
Output is correct |
14 |
Correct |
65 ms |
5892 KB |
Output is correct |
15 |
Correct |
67 ms |
5892 KB |
Output is correct |
16 |
Correct |
50 ms |
5892 KB |
Output is correct |
17 |
Correct |
41 ms |
5892 KB |
Output is correct |
18 |
Correct |
67 ms |
5892 KB |
Output is correct |
19 |
Correct |
71 ms |
5892 KB |
Output is correct |
20 |
Correct |
72 ms |
5892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5892 KB |
Output is correct |
2 |
Correct |
0 ms |
5892 KB |
Output is correct |
3 |
Correct |
0 ms |
5892 KB |
Output is correct |
4 |
Correct |
0 ms |
5892 KB |
Output is correct |
5 |
Correct |
2 ms |
5892 KB |
Output is correct |
6 |
Correct |
0 ms |
5892 KB |
Output is correct |
7 |
Correct |
0 ms |
5892 KB |
Output is correct |
8 |
Correct |
7 ms |
5892 KB |
Output is correct |
9 |
Correct |
16 ms |
5892 KB |
Output is correct |
10 |
Correct |
18 ms |
5892 KB |
Output is correct |
11 |
Correct |
93 ms |
5892 KB |
Output is correct |
12 |
Correct |
45 ms |
5892 KB |
Output is correct |
13 |
Correct |
78 ms |
5892 KB |
Output is correct |
14 |
Correct |
102 ms |
5892 KB |
Output is correct |
15 |
Correct |
142 ms |
5892 KB |
Output is correct |
16 |
Incorrect |
93 ms |
5892 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
5892 KB |
Output is correct |
2 |
Correct |
498 ms |
5892 KB |
Output is correct |
3 |
Correct |
1118 ms |
5892 KB |
Output is correct |
4 |
Correct |
1911 ms |
5892 KB |
Output is correct |
5 |
Correct |
2779 ms |
5892 KB |
Output is correct |
6 |
Correct |
3185 ms |
5892 KB |
Output is correct |
7 |
Correct |
3119 ms |
5892 KB |
Output is correct |
8 |
Correct |
3008 ms |
5892 KB |
Output is correct |
9 |
Correct |
3304 ms |
5892 KB |
Output is correct |
10 |
Correct |
3313 ms |
5892 KB |
Output is correct |
11 |
Correct |
3827 ms |
5892 KB |
Output is correct |
12 |
Execution timed out |
4000 ms |
5888 KB |
Program timed out |
13 |
Halted |
0 ms |
0 KB |
- |