#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 150005
#define B 350
int n,k,bs[MAX_N/B+2],p[MAX_N],pl[MAX_N],l,cnt,ds[MAX_N/B+2][B*3],sz[MAX_N/B+2][B*3],el[MAX_N/B+2][B*3];
void Bini(int x,int *xel,int xbs,int *xds,int *xsz){
sort(xel,xel+xbs);
int r=xbs;
for(int i=xbs-1;i>=0;i--){
while(r-1>=0&&xel[i]+l<xel[r-1])r--;
if(r==xbs){
xds[i]=xel[i]+l;
xsz[i]=1;
}else{
xds[i]=xds[r];
xsz[i]=xsz[r]+1;
}
}
}
void ALLini(void){
for(int i=0;i<n;i++)pl[i]=p[i];
sort(pl,pl+n);
bs[0]=0;
k=0;
for(int i=0;i<n;i++){
if(bs[k]>=B){
k++;
bs[k]=0;
}
el[k][bs[k]]=pl[i];
bs[k]++;
}
for(int i=0;i<=k;i++)Bini(i,el[i],bs[i],ds[i],sz[i]);
}
void init(int N, int L, int X[]){
n=N,l=L;
for(int i=0;i<n;i++)p[i]=X[i];
ALLini();
}
int update(int t, int y){
int a=-1,b=p[t];
while(a+1<=k&&bs[a+1]==0||(bs[a+1]>=1&&el[a+1][0]<=b))a++;
if(a==-1)a=0;
for(int i=0;i<bs[a]-1;i++){
if(el[a][i]==b){
el[a][i]=el[a][bs[a]-1];
break;
}
}
bs[a]--;
Bini(a,el[a],bs[a],ds[a],sz[a]);
p[t]=y;
a=-1;
while(a+1<=k&&bs[a+1]==0||(bs[a+1]>=1&&el[a+1][0]<=y))a++;
if(a==-1)a=0;
el[a][bs[a]]=y;
bs[a]++;
Bini(a,el[a],bs[a],ds[a],sz[a]);
int ans=0;
a=0,b=-1;
for(;a<=k;a++){
if(bs[a]==0)continue;
int c=upper_bound(el[a],el[a]+bs[a],b)-el[a];
if(c==bs[a])continue;
ans+=sz[a][c];
b=ds[a][c];
}
cnt++;
if(cnt>=B){
cnt=0;
ALLini();
}
return ans;
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:43:14: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
while(a+1<=k&&bs[a+1]==0||(bs[a+1]>=1&&el[a+1][0]<=b))a++;
~~~~~~^~~~~~~~~~~~
elephants.cpp:55:14: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
while(a+1<=k&&bs[a+1]==0||(bs[a+1]>=1&&el[a+1][0]<=y))a++;
~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
779 ms |
1588 KB |
Output is correct |
8 |
Correct |
819 ms |
1784 KB |
Output is correct |
9 |
Correct |
1006 ms |
3368 KB |
Output is correct |
10 |
Correct |
984 ms |
3320 KB |
Output is correct |
11 |
Correct |
945 ms |
3448 KB |
Output is correct |
12 |
Correct |
1643 ms |
3340 KB |
Output is correct |
13 |
Correct |
900 ms |
3448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
779 ms |
1588 KB |
Output is correct |
8 |
Correct |
819 ms |
1784 KB |
Output is correct |
9 |
Correct |
1006 ms |
3368 KB |
Output is correct |
10 |
Correct |
984 ms |
3320 KB |
Output is correct |
11 |
Correct |
945 ms |
3448 KB |
Output is correct |
12 |
Correct |
1643 ms |
3340 KB |
Output is correct |
13 |
Correct |
900 ms |
3448 KB |
Output is correct |
14 |
Correct |
1171 ms |
2060 KB |
Output is correct |
15 |
Correct |
1238 ms |
2424 KB |
Output is correct |
16 |
Correct |
2483 ms |
3592 KB |
Output is correct |
17 |
Correct |
2802 ms |
4600 KB |
Output is correct |
18 |
Correct |
2892 ms |
4592 KB |
Output is correct |
19 |
Correct |
3151 ms |
4600 KB |
Output is correct |
20 |
Correct |
2762 ms |
4600 KB |
Output is correct |
21 |
Correct |
2733 ms |
4600 KB |
Output is correct |
22 |
Correct |
1622 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
779 ms |
1588 KB |
Output is correct |
8 |
Correct |
819 ms |
1784 KB |
Output is correct |
9 |
Correct |
1006 ms |
3368 KB |
Output is correct |
10 |
Correct |
984 ms |
3320 KB |
Output is correct |
11 |
Correct |
945 ms |
3448 KB |
Output is correct |
12 |
Correct |
1643 ms |
3340 KB |
Output is correct |
13 |
Correct |
900 ms |
3448 KB |
Output is correct |
14 |
Correct |
1171 ms |
2060 KB |
Output is correct |
15 |
Correct |
1238 ms |
2424 KB |
Output is correct |
16 |
Correct |
2483 ms |
3592 KB |
Output is correct |
17 |
Correct |
2802 ms |
4600 KB |
Output is correct |
18 |
Correct |
2892 ms |
4592 KB |
Output is correct |
19 |
Correct |
3151 ms |
4600 KB |
Output is correct |
20 |
Correct |
2762 ms |
4600 KB |
Output is correct |
21 |
Correct |
2733 ms |
4600 KB |
Output is correct |
22 |
Correct |
1622 ms |
4600 KB |
Output is correct |
23 |
Correct |
6141 ms |
9336 KB |
Output is correct |
24 |
Correct |
6738 ms |
11876 KB |
Output is correct |
25 |
Correct |
5301 ms |
11768 KB |
Output is correct |
26 |
Correct |
6101 ms |
11896 KB |
Output is correct |
27 |
Execution timed out |
9086 ms |
11768 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |