#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 15e4+10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
const int blockSize = 400;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int bucket[blockSize][2*blockSize], qtd[blockSize][2*blockSize], fim[blockSize][2*blockSize];
int arr[MAXN], aux[MAXN], bucketSize[blockSize];
int n, lim, cnt;
int getBucketId(int val){
int l=1, r=(n-1)/blockSize, best=0;
while(l<=r){
int mid=(l+r)>>1;
if(bucket[mid][0]<=val){
best=mid;
l=mid+1;
}
else r=mid-1;
}
return best;
}
void calcRes(int b){
int id=bucketSize[b];
for(int i=bucketSize[b]-1;i>=0;i--){
while(bucket[b][id-1]>bucket[b][i]+lim) id--;
if(id==bucketSize[b]){
qtd[b][i]=1;
fim[b][i]=bucket[b][i]+lim;
}
else{
qtd[b][i]=qtd[b][id]+1;
fim[b][i]=fim[b][id];
}
}
}
void addToBucket(int val){
int b=getBucketId(val);
int id=0;
while(id<bucketSize[b] && bucket[b][id]<val) id++;
for(int i=bucketSize[b];i>=id+1;i--) bucket[b][i]=bucket[b][i-1];
bucket[b][id]=val;
bucketSize[b]++;
calcRes(b);
}
void removeFromBucket(int val){
int b=getBucketId(val);
int id=0;
while(id<bucketSize[b] && bucket[b][id]<val) id++;
for(int i=id;i<bucketSize[b]-1;i++) bucket[b][i]=bucket[b][i+1];
bucketSize[b]--;
calcRes(b);
}
void recalculate(){
int id=0;
for(int i=0;i<=(n-1)/blockSize;i++){
for(int j=0;j<bucketSize[i];j++)
aux[id++]=bucket[i][j];
bucketSize[i]=0;
}
for(int i=0;i<n;i++) bucket[i/blockSize][i%blockSize]=aux[i], bucketSize[i/blockSize]++;
for(int i=0;i<=(n-1)/blockSize;i++) calcRes(i);
//cout << "after recalculation:\n";
//for(int a=0;a<=(n-1)/blockSize;a++){
//cout << "bucket " << a << ":\n";
//for(int j=0;j<bucketSize[a];j++) cout << bucket[a][j] << " \n"[j==bucketSize[a]-1];
//}
//cout << "\n";
}
int getAns(){
int ans=qtd[0][0], ini=fim[0][0];
for(int i=1;i<=(n-1)/blockSize;i++){
if(!bucketSize[i] || bucket[i][bucketSize[i]-1]<=ini) continue;
int l=0, r=bucketSize[i]-1, best=-1;
while(l<=r){
int mid=(l+r)>>1;
if(bucket[i][mid]>ini){
best=mid;
r=mid-1;
}
else l=mid+1;
}
ans+=qtd[i][best];
ini=fim[i][best];
}
return ans;
}
void init(int N, int L, int X[])
{
n=N; cnt=0; lim=L;
for(int i=0;i<N;i++) arr[i]=X[i];
for(int i=0;i<N;i++) bucket[i/blockSize][i%blockSize]=arr[i], bucketSize[i/blockSize]++;
}
int update(int i, int y)
{
cnt++;
if(cnt==blockSize){
cnt=0;
recalculate();
}
removeFromBucket(arr[i]);
//cout << "after removal:\n";
//for(int j=0;j<n-1;j++) cout << bucket[0][j] << " \n"[j==n-2];
addToBucket(y);
//cout << "after adding:\n";
//for(int j=0;j<n;j++) cout << bucket[0][j] << " \n"[j==n-1];
arr[i]=y;
//for(int a=0;a<=(n-1)/blockSize;a++){
//cout << "bucket " << a << ":\n";
//for(int j=0;j<bucketSize[a];j++) cout << bucket[a][j] << " \n"[j==bucketSize[a]-1];
//}
return getAns();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12644 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12644 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
12 ms |
13656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12644 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
12 ms |
13656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12632 KB |
Output is correct |
5 |
Correct |
2 ms |
12644 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Incorrect |
12 ms |
13656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |