#include "elephants.h"
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int NN=15e4;
const int S=400; // how many in one block
//const int S=4;
int l;
struct Block
{
int d;
int t[2*S+10];
pair<int,int> dp[2*S+10];
Block()
{
d=0;
}
void clear()
{
d=0;
return;
}
void recount()
{
dp[d]={0,0};
for(int i=d-1,j=d;i>=0;i--)
{
while(t[i]+l<t[j-1])
j--;
dp[i].fi=dp[j].fi+1;
if(j==d)
dp[i].se=t[i]+l;
else
dp[i].se=dp[j].se;
}
return;
}
void add(int x)
{
int p;
for(p=0;p<d && t[p]<x;p++);
for(int i=d-1;i>=p;i--)
t[i+1]=t[i];
t[p]=x;
d++;
recount();
return;
}
void remove(int x)
{
int p;
for(p=0;p<d && t[p]<x;p++);
for(int i=p;i<d-1;i++)
t[i]=t[i+1];
d--;
recount();
return;
}
pair<int,int> ans_pos(int x)
{
if(d==0)
return {0,x};
int bg=0,en=d-1;
while(bg<en)
{
int mid=(bg+en)/2;
if(t[mid]>x)
en=mid;
else
bg=mid+1;
}
if(t[bg]<=x)
return {0,x};
return dp[bg];
}
};
int n,qq=0;
Block b[NN/S+10];
int curr_pos[NN+10];
void recount_all()
{
for(int i=0;i<=n/S;i++)
b[i].clear();
vector<int> tmp(n);
for(int i=0;i<n;i++)
tmp[i]=curr_pos[i];
sort(tmp.begin(),tmp.end());
for(int i=0;i<n;i++)
{
b[i/S].t[i%S]=tmp[i];
b[i/S].d++;
}
for(int i=0;i<=n/S;i++)
b[i].recount();
return;
}
Block& find_block(int x)
{
int i;
for(i=0;i<=n/S && (b[i].d==0 || b[i].t[0]<x);i++);
if(i==0 || (b[i].d>0 && b[i].t[0]==x))
return b[i];
while(i>0 && b[i-1].d==0)
i--;
return b[i-1];
}
int get_ans()
{
int ans=0,x=-1;
for(int i=0;i<=n/S;i++)
{
auto tmp=b[i].ans_pos(x);
ans+=tmp.fi;
x=tmp.se;
//cerr<<"xd "<<i<<" "<<ans<<" "<<x<<"\n";
//for(int j=0;j<b[i].d;j++)
// cerr<<b[i].t[j]<<" ";
//cerr<<"\n";
}
return ans;
}
void init(int N,int L,int X[])
{
n=N;
l=L;
for(int i=0;i<n;i++)
curr_pos[i]=X[i];
recount_all();
return;
}
int update(int i,int y)
{
if(qq>=S-10)
{
recount_all();
qq=0;
}
qq++;
find_block(curr_pos[i]).remove(curr_pos[i]);
find_block(y).add(y);
curr_pos[i]=y;
return get_ans();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
3 ms |
3916 KB |
Output is correct |
5 |
Correct |
2 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
3 ms |
3916 KB |
Output is correct |
5 |
Correct |
2 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3916 KB |
Output is correct |
7 |
Correct |
215 ms |
4780 KB |
Output is correct |
8 |
Correct |
258 ms |
4760 KB |
Output is correct |
9 |
Correct |
384 ms |
5148 KB |
Output is correct |
10 |
Correct |
551 ms |
6476 KB |
Output is correct |
11 |
Correct |
598 ms |
6516 KB |
Output is correct |
12 |
Correct |
924 ms |
6604 KB |
Output is correct |
13 |
Correct |
576 ms |
6272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
3 ms |
3916 KB |
Output is correct |
5 |
Correct |
2 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3916 KB |
Output is correct |
7 |
Correct |
215 ms |
4780 KB |
Output is correct |
8 |
Correct |
258 ms |
4760 KB |
Output is correct |
9 |
Correct |
384 ms |
5148 KB |
Output is correct |
10 |
Correct |
551 ms |
6476 KB |
Output is correct |
11 |
Correct |
598 ms |
6516 KB |
Output is correct |
12 |
Correct |
924 ms |
6604 KB |
Output is correct |
13 |
Correct |
576 ms |
6272 KB |
Output is correct |
14 |
Correct |
356 ms |
6580 KB |
Output is correct |
15 |
Correct |
423 ms |
6612 KB |
Output is correct |
16 |
Correct |
1429 ms |
7216 KB |
Output is correct |
17 |
Correct |
1656 ms |
7648 KB |
Output is correct |
18 |
Correct |
1786 ms |
7632 KB |
Output is correct |
19 |
Correct |
970 ms |
7796 KB |
Output is correct |
20 |
Correct |
1643 ms |
7876 KB |
Output is correct |
21 |
Correct |
1658 ms |
7624 KB |
Output is correct |
22 |
Correct |
1068 ms |
7228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
3 ms |
3916 KB |
Output is correct |
5 |
Correct |
2 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3916 KB |
Output is correct |
7 |
Correct |
215 ms |
4780 KB |
Output is correct |
8 |
Correct |
258 ms |
4760 KB |
Output is correct |
9 |
Correct |
384 ms |
5148 KB |
Output is correct |
10 |
Correct |
551 ms |
6476 KB |
Output is correct |
11 |
Correct |
598 ms |
6516 KB |
Output is correct |
12 |
Correct |
924 ms |
6604 KB |
Output is correct |
13 |
Correct |
576 ms |
6272 KB |
Output is correct |
14 |
Correct |
356 ms |
6580 KB |
Output is correct |
15 |
Correct |
423 ms |
6612 KB |
Output is correct |
16 |
Correct |
1429 ms |
7216 KB |
Output is correct |
17 |
Correct |
1656 ms |
7648 KB |
Output is correct |
18 |
Correct |
1786 ms |
7632 KB |
Output is correct |
19 |
Correct |
970 ms |
7796 KB |
Output is correct |
20 |
Correct |
1643 ms |
7876 KB |
Output is correct |
21 |
Correct |
1658 ms |
7624 KB |
Output is correct |
22 |
Correct |
1068 ms |
7228 KB |
Output is correct |
23 |
Correct |
3333 ms |
12636 KB |
Output is correct |
24 |
Correct |
3898 ms |
12524 KB |
Output is correct |
25 |
Correct |
2763 ms |
12536 KB |
Output is correct |
26 |
Correct |
4459 ms |
12528 KB |
Output is correct |
27 |
Correct |
4914 ms |
12372 KB |
Output is correct |
28 |
Correct |
1144 ms |
8772 KB |
Output is correct |
29 |
Correct |
1102 ms |
8728 KB |
Output is correct |
30 |
Correct |
1140 ms |
8852 KB |
Output is correct |
31 |
Correct |
1099 ms |
8732 KB |
Output is correct |
32 |
Correct |
4792 ms |
11956 KB |
Output is correct |
33 |
Correct |
4674 ms |
11284 KB |
Output is correct |
34 |
Correct |
4412 ms |
12168 KB |
Output is correct |
35 |
Correct |
3799 ms |
12460 KB |
Output is correct |
36 |
Correct |
2947 ms |
11944 KB |
Output is correct |
37 |
Correct |
6206 ms |
12352 KB |
Output is correct |
38 |
Correct |
4549 ms |
11172 KB |
Output is correct |
39 |
Correct |
4170 ms |
12200 KB |
Output is correct |
40 |
Correct |
4973 ms |
11204 KB |
Output is correct |
41 |
Correct |
6618 ms |
11948 KB |
Output is correct |
42 |
Correct |
6745 ms |
12188 KB |
Output is correct |