#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) a.begin(),a.end()
#define sz(a) (int)(a.size())
#define ub upper_bound
typedef long long int ll;
typedef vector<vector<int>> vii;
typedef pair<int,int> pii;
int n,l;
vii bucket,reach,cnt;
vector<int>x,comp;
int blk = 500,num;
int find(int cur,int val){
int l = 0,r = sz(bucket[cur])-1;
while(r>=l){
int mid = (l+r)/2;
if(x[bucket[cur][mid]] <= val)l = mid+1;
else r = mid-1;
}
return l;
}
void build1(vector<int>a,int cur){
if(cur == sz(bucket)){
bucket.pb(a);
reach.pb(a);
cnt.pb(a);
}else bucket[cur] = reach[cur] = cnt[cur] = a;
int last = bucket[cur].back();
for(int i=sz(a)-1;i>=0;i--){
int v = bucket[cur][i];
comp[v] = cur;
if(x[v]+l >= x[last]){
reach[cur][i] = x[v]+l;
cnt[cur][i] = 1;
}else{
int p = find(cur,x[v]+l);
reach[cur][i] = reach[cur][p];
cnt[cur][i] = cnt[cur][p]+1;
}
}
}
void build2(){
bucket.clear();
cnt.clear();
reach.clear();
vector<pii>a;
for(int i=0;i<n;i++)a.pb({x[i],i});
sort(all(a));
for(int i=0,j=0;i<n;i+=blk,j++){
vector<int>b;
for(int k=i;k<min(n,i+blk);k++)b.pb(a[k].second);
build1(b,j);
}
}
void init(int N, int L, int X[])
{
n = N;
l = L;
x.resize(n);
comp.resize(n);
for(int i=0;i<n;i++)x[i] = X[i];
build2();
}
int update(int i, int y)
{
if(num == blk-1)build2();
num%=blk;
int c = comp[i];
vector<int>a;
for(int x:bucket[c]){
if(x != i)a.pb(x);
}
build1(a,c);
x[i] = y;
for(int i=0;i<sz(bucket);i++){
if(y <= x[bucket[i].back()]){
c = i;
break;
}
}
if(y > x[bucket.back().back()])c = sz(bucket)-1;
a = bucket[c];
a.pb(i);
sort(all(a),[&](const int &i,const int &j){return x[i] < x[j];});
build1(a,c);
int ans = cnt[0][0];
int cur = reach[0][0];
for(int i=1;i<sz(bucket);i++){
if(cur >= x[bucket[i].back()])continue;
else if(x[bucket[i][0]] > cur){
cur = reach[i][0];
ans+=cnt[i][0];
}else{
int idx = find(i,cur);
cur = reach[i][idx];
ans+=cnt[i][idx];
}
}
//for(int i=0;i<n;i++)cout<<x[bucket[0][i]]<<" "<<reach[0][i]<<" "<<cnt[0][i]<<'\n';
num++;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
304 KB |
Output is correct |
7 |
Correct |
5650 ms |
2364 KB |
Output is correct |
8 |
Correct |
5774 ms |
2636 KB |
Output is correct |
9 |
Correct |
2809 ms |
4108 KB |
Output is correct |
10 |
Incorrect |
90 ms |
4016 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
304 KB |
Output is correct |
7 |
Correct |
5650 ms |
2364 KB |
Output is correct |
8 |
Correct |
5774 ms |
2636 KB |
Output is correct |
9 |
Correct |
2809 ms |
4108 KB |
Output is correct |
10 |
Incorrect |
90 ms |
4016 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
304 KB |
Output is correct |
7 |
Correct |
5650 ms |
2364 KB |
Output is correct |
8 |
Correct |
5774 ms |
2636 KB |
Output is correct |
9 |
Correct |
2809 ms |
4108 KB |
Output is correct |
10 |
Incorrect |
90 ms |
4016 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |