#include "elephants.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
const int maxn = 150005;
const int B = 500;
int N,L,S,V[maxn];
vector<vector<int>> X;
vector<vector<pii>> P;
void build(vector<int> &x,vector<pii> &p){
int sz=(int)x.size();
p.assign(sz,{0,0});
int rt=sz;
for(int i=sz-1;i>=0;i--){
while(rt>0 && x[rt-1]>x[i]+L) rt--;
if(rt==sz) p[i].fi=x[i];
else p[i]=p[rt];
p[i].se++;
}
}
void init(int _N, int _L, int _X[])
{
N=_N,L=_L;S=(N-1)/B+1;
X.assign(S,{});P.assign(S,{});
for(int i=0;i<N;i++) X[i/B].push_back(V[i]=_X[i]);
for(int i=0;i<S;i++) build(X[i],P[i]);
}
void del(int val){
int p=0;
while(p<S && val>X[p].back()) p++;
for(int i=0;i<(int)X[p].size();i++){
if(X[p][i]==val){
X[p].erase(X[p].begin()+i);
break;
}
}
//cout << "del " << p << endl;
if(X[p].empty()){
//cout << "empty" << '\n';
X.erase(X.begin()+p);
P.erase(P.begin()+p);
S--;
}
else build(X[p],P[p]);
}
void add(int val){
int p=0;
while(p<S && val>X[p].back()) p++;
if(p==S) p--;
for(int i=0;i<=(int)X[p].size();i++){
if(i==(int)X[p].size() || val<=X[p][i]){
X[p].insert(X[p].begin()+i,val);
break;
}
}
if((int)X[p].size()==2*B){
vector<int> nw(X[p].begin()+B,X[p].end());
X[p].resize(B);
X.insert(X.begin()+p+1,nw);
P.insert(P.begin()+p+1,vector<pii>());
build(X[p],P[p]);
build(X[p+1],P[p+1]);
S++;
}
else build(X[p],P[p]);
}
int update(int i, int y)
{
//cout << i << endl;
del(V[i]);
V[i]=y;
add(V[i]);
int res=1,cur=X[0][0];
for(int i=0;i<S;i++){
int nxt=upper_bound(X[i].begin(),X[i].end(),cur+L)-X[i].begin();
if(nxt==(int)X[i].size()) continue;
res+=P[i][nxt].se;
cur=P[i][nxt].fi;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8696 KB |
Output is correct |
4 |
Correct |
1 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8696 KB |
Output is correct |
4 |
Correct |
1 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
185 ms |
8796 KB |
Output is correct |
8 |
Correct |
193 ms |
9820 KB |
Output is correct |
9 |
Correct |
228 ms |
12888 KB |
Output is correct |
10 |
Correct |
219 ms |
13392 KB |
Output is correct |
11 |
Correct |
224 ms |
13420 KB |
Output is correct |
12 |
Correct |
368 ms |
13088 KB |
Output is correct |
13 |
Correct |
235 ms |
13136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8696 KB |
Output is correct |
4 |
Correct |
1 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
185 ms |
8796 KB |
Output is correct |
8 |
Correct |
193 ms |
9820 KB |
Output is correct |
9 |
Correct |
228 ms |
12888 KB |
Output is correct |
10 |
Correct |
219 ms |
13392 KB |
Output is correct |
11 |
Correct |
224 ms |
13420 KB |
Output is correct |
12 |
Correct |
368 ms |
13088 KB |
Output is correct |
13 |
Correct |
235 ms |
13136 KB |
Output is correct |
14 |
Correct |
280 ms |
10544 KB |
Output is correct |
15 |
Correct |
297 ms |
10328 KB |
Output is correct |
16 |
Correct |
644 ms |
13420 KB |
Output is correct |
17 |
Correct |
613 ms |
14160 KB |
Output is correct |
18 |
Correct |
644 ms |
13996 KB |
Output is correct |
19 |
Correct |
331 ms |
13876 KB |
Output is correct |
20 |
Correct |
617 ms |
14420 KB |
Output is correct |
21 |
Correct |
571 ms |
14164 KB |
Output is correct |
22 |
Correct |
375 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8696 KB |
Output is correct |
4 |
Correct |
1 ms |
8536 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
185 ms |
8796 KB |
Output is correct |
8 |
Correct |
193 ms |
9820 KB |
Output is correct |
9 |
Correct |
228 ms |
12888 KB |
Output is correct |
10 |
Correct |
219 ms |
13392 KB |
Output is correct |
11 |
Correct |
224 ms |
13420 KB |
Output is correct |
12 |
Correct |
368 ms |
13088 KB |
Output is correct |
13 |
Correct |
235 ms |
13136 KB |
Output is correct |
14 |
Correct |
280 ms |
10544 KB |
Output is correct |
15 |
Correct |
297 ms |
10328 KB |
Output is correct |
16 |
Correct |
644 ms |
13420 KB |
Output is correct |
17 |
Correct |
613 ms |
14160 KB |
Output is correct |
18 |
Correct |
644 ms |
13996 KB |
Output is correct |
19 |
Correct |
331 ms |
13876 KB |
Output is correct |
20 |
Correct |
617 ms |
14420 KB |
Output is correct |
21 |
Correct |
571 ms |
14164 KB |
Output is correct |
22 |
Correct |
375 ms |
14160 KB |
Output is correct |
23 |
Correct |
1247 ms |
17748 KB |
Output is correct |
24 |
Correct |
1266 ms |
17696 KB |
Output is correct |
25 |
Correct |
945 ms |
17496 KB |
Output is correct |
26 |
Correct |
973 ms |
19536 KB |
Output is correct |
27 |
Correct |
891 ms |
17544 KB |
Output is correct |
28 |
Correct |
886 ms |
11604 KB |
Output is correct |
29 |
Correct |
698 ms |
11608 KB |
Output is correct |
30 |
Correct |
908 ms |
11604 KB |
Output is correct |
31 |
Correct |
694 ms |
11604 KB |
Output is correct |
32 |
Correct |
886 ms |
18776 KB |
Output is correct |
33 |
Correct |
870 ms |
18272 KB |
Output is correct |
34 |
Correct |
901 ms |
19280 KB |
Output is correct |
35 |
Correct |
809 ms |
17492 KB |
Output is correct |
36 |
Correct |
775 ms |
16976 KB |
Output is correct |
37 |
Correct |
1160 ms |
18512 KB |
Output is correct |
38 |
Correct |
1008 ms |
18264 KB |
Output is correct |
39 |
Correct |
887 ms |
17232 KB |
Output is correct |
40 |
Correct |
1047 ms |
18004 KB |
Output is correct |
41 |
Correct |
1645 ms |
18264 KB |
Output is correct |
42 |
Correct |
1657 ms |
18268 KB |
Output is correct |