#include<stdio.h>
#include<algorithm>
#define BSIZ 300
int n,q;
int x[100100];
struct sn{
int loc,val;
bool operator<(const sn&r)const{
return val<r.val;
}
};
sn st[100100];
int a[100100];
int cpr[100100];
int qry[100100][2];
long long int ans[100100];
long long int sv[100100];
int cprn;
long long int it[1<<18];
void push(int loc,long long int val){
loc+=1<<17;
it[loc]=val;
loc/=2;
while(loc!=0){
if(it[loc*2]>it[loc*2+1]){
it[loc]=it[loc*2];
}
else{
it[loc]=it[loc*2+1];
}
loc/=2;
}
}
long long int calc(){
return it[1];
}
int main(){
int i,j,k,p,bf,df;
scanf("%d%d",&n,&q);
for(i=0;i<n;i++){
scanf("%d",&x[i]);
st[i].val=x[i];
st[i].loc=i;
}
std::sort(st,st+n);
for(i=0;i<n;i++){
a[st[i].loc]=cprn;
if(i==n-1||st[i].val!=st[i+1].val){
cpr[cprn]=st[i].val;
cprn++;
}
}
for(i=0;i<q;i++){
scanf("%d%d",&qry[i][0],&qry[i][1]);
qry[i][0]--;
qry[i][1]--;
st[i].loc=i;
st[i].val=qry[i][1];
}
std::sort(st,st+q);
for(i=0;i<=(n-1)/BSIZ;i++){
bf=i*BSIZ;
df=i*BSIZ;
for(j=0;j<q;j++){
p=st[j].loc;
if(qry[p][0]/BSIZ==i){
for(k=bf;k<=qry[p][1];k++){
sv[a[k]]+=cpr[a[k]];
push(a[k],sv[a[k]]);
}
bf=qry[p][1]+1;
if(df<qry[p][0]){
for(k=df;k<qry[p][0];k++){
sv[a[k]]-=cpr[a[k]];
push(a[k],sv[a[k]]);
}
}
else{
for(k=qry[p][0];k<df;k++){
sv[a[k]]+=cpr[a[k]];
push(a[k],sv[a[k]]);
}
}
df=qry[p][0];
ans[p]=it[1];
}
}
for(k=0;k<cprn;k++){
if(sv[k]==0)continue;
sv[k]=0;
push(k,0);
}
}
for(i=0;i<q;i++){
printf("%lld\n",ans[i]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7436 KB |
Output is correct |
2 |
Correct |
0 ms |
7436 KB |
Output is correct |
3 |
Correct |
0 ms |
7436 KB |
Output is correct |
4 |
Correct |
0 ms |
7436 KB |
Output is correct |
5 |
Correct |
0 ms |
7436 KB |
Output is correct |
6 |
Correct |
0 ms |
7436 KB |
Output is correct |
7 |
Correct |
0 ms |
7436 KB |
Output is correct |
8 |
Correct |
0 ms |
7436 KB |
Output is correct |
9 |
Correct |
0 ms |
7436 KB |
Output is correct |
10 |
Correct |
0 ms |
7436 KB |
Output is correct |
11 |
Correct |
0 ms |
7436 KB |
Output is correct |
12 |
Correct |
0 ms |
7436 KB |
Output is correct |
13 |
Correct |
0 ms |
7436 KB |
Output is correct |
14 |
Correct |
0 ms |
7436 KB |
Output is correct |
15 |
Correct |
0 ms |
7436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7436 KB |
Output is correct |
2 |
Correct |
0 ms |
7436 KB |
Output is correct |
3 |
Correct |
4 ms |
7436 KB |
Output is correct |
4 |
Correct |
12 ms |
7436 KB |
Output is correct |
5 |
Correct |
28 ms |
7436 KB |
Output is correct |
6 |
Correct |
40 ms |
7436 KB |
Output is correct |
7 |
Correct |
44 ms |
7436 KB |
Output is correct |
8 |
Correct |
40 ms |
7436 KB |
Output is correct |
9 |
Correct |
36 ms |
7436 KB |
Output is correct |
10 |
Correct |
56 ms |
7436 KB |
Output is correct |
11 |
Correct |
56 ms |
7436 KB |
Output is correct |
12 |
Correct |
56 ms |
7436 KB |
Output is correct |
13 |
Correct |
56 ms |
7436 KB |
Output is correct |
14 |
Correct |
56 ms |
7436 KB |
Output is correct |
15 |
Correct |
52 ms |
7436 KB |
Output is correct |
16 |
Correct |
36 ms |
7436 KB |
Output is correct |
17 |
Correct |
36 ms |
7436 KB |
Output is correct |
18 |
Correct |
52 ms |
7436 KB |
Output is correct |
19 |
Correct |
52 ms |
7436 KB |
Output is correct |
20 |
Correct |
56 ms |
7436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7436 KB |
Output is correct |
2 |
Correct |
0 ms |
7436 KB |
Output is correct |
3 |
Correct |
0 ms |
7436 KB |
Output is correct |
4 |
Correct |
0 ms |
7436 KB |
Output is correct |
5 |
Correct |
0 ms |
7436 KB |
Output is correct |
6 |
Correct |
0 ms |
7436 KB |
Output is correct |
7 |
Correct |
4 ms |
7436 KB |
Output is correct |
8 |
Correct |
4 ms |
7436 KB |
Output is correct |
9 |
Correct |
24 ms |
7436 KB |
Output is correct |
10 |
Correct |
20 ms |
7436 KB |
Output is correct |
11 |
Correct |
220 ms |
7436 KB |
Output is correct |
12 |
Correct |
64 ms |
7436 KB |
Output is correct |
13 |
Correct |
112 ms |
7436 KB |
Output is correct |
14 |
Correct |
196 ms |
7436 KB |
Output is correct |
15 |
Correct |
312 ms |
7436 KB |
Output is correct |
16 |
Correct |
1600 ms |
7436 KB |
Output is correct |
17 |
Correct |
536 ms |
7436 KB |
Output is correct |
18 |
Correct |
620 ms |
7436 KB |
Output is correct |
19 |
Correct |
1132 ms |
7436 KB |
Output is correct |
20 |
Correct |
1028 ms |
7436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
7436 KB |
Output is correct |
2 |
Correct |
268 ms |
7436 KB |
Output is correct |
3 |
Correct |
572 ms |
7436 KB |
Output is correct |
4 |
Correct |
964 ms |
7436 KB |
Output is correct |
5 |
Correct |
1360 ms |
7436 KB |
Output is correct |
6 |
Correct |
1568 ms |
7436 KB |
Output is correct |
7 |
Correct |
1476 ms |
7436 KB |
Output is correct |
8 |
Correct |
1508 ms |
7436 KB |
Output is correct |
9 |
Correct |
1620 ms |
7436 KB |
Output is correct |
10 |
Correct |
1776 ms |
7436 KB |
Output is correct |
11 |
Correct |
1988 ms |
7436 KB |
Output is correct |
12 |
Correct |
2624 ms |
7436 KB |
Output is correct |
13 |
Correct |
3652 ms |
7436 KB |
Output is correct |
14 |
Execution timed out |
4000 ms |
7436 KB |
Program timed out |
15 |
Halted |
0 ms |
0 KB |
- |