This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int bsize=300;
int N,A[150000],B[150000],C[150000],P[150000],L,D[1000];
vector<int>vb[1000];
void update_bucket(int t){
for(int i:vb[t]){
C[i]=t;
}
for(int i=vb[t].size()-1;i>=0;i--){
int l,r;
l=i;
r=vb[t].size();
while(l+1!=r){
int m=(l+r)/2;
if(P[vb[t][i]]+L<P[vb[t][m]]){
r=m;
}
else{
l=m;
}
}
if(r==vb[t].size()){
A[vb[t][i]]=1;
B[vb[t][i]]=P[vb[t][i]]+L;
}
else{
A[vb[t][i]]=A[vb[t][r]]+1;
B[vb[t][i]]=B[vb[t][r]];
}
}
}
void erase(int t){
int id=C[t];
for(int i=0;i<vb[id].size()-1;i++){
if(vb[id][i]==t){
swap(vb[id][i],vb[id][i+1]);
}
}
vb[id].pop_back();
update_bucket(id);
}
void add(int t,int x){
P[t]=x;
D[0]=0;
for(int i=0;i<(N-1)/bsize+1;i++){
if(vb[i].empty()){
D[i+1]=D[i];
}
else{
D[i]=P[vb[i].front()];
D[i+1]=P[vb[i].back()];
}
}
D[(N-1)/bsize+1]=2000000000;
int id;
for(int i=0;i<(N-1)/bsize+1;i++){
if(x<=D[i+1]){
id=i;
break;
}
}
vb[id].push_back(t);
for(int i=vb[id].size()-2;i>=0;i--){
if(P[vb[id][i]]>P[vb[id][i+1]]){
swap(vb[id][i],vb[id][i+1]);
}
}
update_bucket(id);
}
int calc(){
int c=0;
int p=-1;
for(int i=0;i<(N-1)/bsize+1;i++){
if(vb[i].empty()||P[vb[i].back()]<=p)continue;
int l,r;
l=-1;
r=vb[i].size()-1;
while(l+1!=r){
int m=(l+r)/2;
if(p<P[vb[i][m]]){
r=m;
}
else{
l=m;
}
}
c+=A[vb[i][r]];
p=B[vb[i][r]];
}
return c;
}
void init(int n, int l, int X[]){
N=n;
L=l;
for(int i=0;i<N;i++){
P[i]=X[i];
}
for(int i=0;i<(N-1)/bsize+1;i++){
for(int j=i*bsize;j<(i+1)*bsize&&j<N;j++){
vb[i].push_back(j);
}
update_bucket(i);
}
}
int update(int i, int y){
erase(i);
add(i,y);
return calc();
}
Compilation message (stderr)
elephants.cpp: In function 'void update_bucket(int)':
elephants.cpp:27:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if(r==vb[t].size()){
| ~^~~~~~~~~~~~~~
elephants.cpp: In function 'void erase(int)':
elephants.cpp:40:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0;i<vb[id].size()-1;i++){
| ~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |