Submission #920325

# Submission time Handle Problem Language Result Execution time Memory
920325 2024-02-02T13:00:58 Z Lalic Dancing Elephants (IOI11_elephants) C++17
26 / 100
12 ms 12888 KB
#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[405][2*405+10], qtd[405][2*405+10], fim[405][2*405+10];
int arr[MAXN], aux[MAXN], bucketSize[405];
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, ini;
	if(bucketSize[0]) ans=qtd[0][0], ini=fim[0][0];
	else ans=qtd[1][0], ini=fim[1][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]);
	addToBucket(y);
	arr[i]=y;
	
	//cout << "after update:\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";
	
	return getAns();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12632 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12888 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12632 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Incorrect 12 ms 12632 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 12888 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12632 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Incorrect 12 ms 12632 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 12888 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12632 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Incorrect 12 ms 12632 KB Output isn't correct
8 Halted 0 ms 0 KB -