#include "elephants.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0; i<n; ++i)
#define pb push_back
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define f first
#define s second
mt19937 rng(998244353);
const int N=2e5+5;
int a[N];
int b[N];
const int K=400;
int block[K][2*K];
int nxt[K][2*K];
int val[K][2*K];
int sz[K];
pi go[K][2*K];
int n,len;
void make(int i) {
int R=0;
for (int L=0; L<sz[i]; ++L) {
while (R<sz[i] && block[i][R]-block[i][L]<=len) ++R;
nxt[i][L]=R;
val[i][L]=block[i][L]+len;
}
for (int j=sz[i]-1; j>=0; --j) {
if (nxt[i][j]==sz[i]) {
go[i][j]={1,val[i][j]};
} else {
go[i][j]={go[i][nxt[i][j]].f+1,go[i][nxt[i][j]].s};
}
}
}
void build() {
n=0;
forn(i,K) {
forn(j,sz[i]) b[n++]=block[i][j];
}
for(int l=0; l<n; l+=K) {
int r=min(l+K,n);
sz[l/K]=r-l;
for(int i=l; i<r; ++i) {
block[l/K][i-l]=b[i];
}
int i=l/K;
make(i);
}
}
void init(int _n, int _l, int x[]) {
n=_n; len=_l;
forn(i,n) a[i]=x[i];
forn(i,n) b[i]=a[i];
sort(b,b+n);
for(int l=0; l<n; l+=K) {
int r=min(l+K,n);
sz[l/K]=r-l;
for(int i=l; i<r; ++i) {
block[l/K][i-l]=b[i];
}
}
build();
}
void rem(int i, int x) {
forn(j,sz[i]) {
if (block[i][j]<x) continue;
block[i][j]=block[i][j+1];
}
--sz[i];
make(i);
}
void del(int x) {
for (int i=0; i<K; ++i) {
if (x<=block[i][sz[i]-1]) {
rem(i,x);
break;
}
}
}
void insert(int i, int x) {
int z=1;
int t=x;
forn(j,sz[i]) {
if (block[i][j]<x) continue;
swap(t,block[i][j]);
}
block[i][sz[i]]=t;
++sz[i];
make(i);
}
void add(int x) {
for (int i=0; i<K; ++i) {
if (x<=block[i][sz[i]-1]) {
insert(i,x);
break;
}
if (i==(n-1)/K) {
insert(i,x);
break;
}
}
}
int Q=0;
int update(int ind, int x) {
del(a[ind]);
a[ind]=x;
add(a[ind]);
++Q;
if (Q==K) {
Q=0;
build();
}
//forn(i,n) {
// if (!sz[i]) break;
// forn(j,sz[i]) cout<<block[i][j]<<' '; cout<<'\n';
// forn(j,sz[i]) cout<<nxt[i][j]<<' '; cout<<'\n';
// forn(j,sz[i]) cout<<val[i][j]<<' '; cout<<'\n';
// forn(j,sz[i]) cout<<go[i][j].f<<','<<go[i][j].s<<" "; cout<<'\n';
//}
int i=0, j=0, pos=block[0][0];
int ans=0;
for (;sz[i];) {
auto it = go[i][j];
ans += it.f;
pos = it.s;
while (sz[i] && block[i][sz[i]-1]<=pos) ++i;
if (!sz[i]) break;
int l=0, r=sz[i]-1;
while (l<r) {
int m=(l+r)>>1;
if (block[i][m]<=pos) l=m+1;
else r=m;
}
j=r;
}
return ans;
}
Compilation message
elephants.cpp: In function 'void insert(int, int)':
elephants.cpp:98:7: warning: unused variable 'z' [-Wunused-variable]
98 | int z=1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
331 ms |
1836 KB |
Output is correct |
8 |
Correct |
337 ms |
2128 KB |
Output is correct |
9 |
Correct |
328 ms |
3724 KB |
Output is correct |
10 |
Correct |
312 ms |
3736 KB |
Output is correct |
11 |
Correct |
273 ms |
3788 KB |
Output is correct |
12 |
Correct |
543 ms |
3636 KB |
Output is correct |
13 |
Correct |
292 ms |
3740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
331 ms |
1836 KB |
Output is correct |
8 |
Correct |
337 ms |
2128 KB |
Output is correct |
9 |
Correct |
328 ms |
3724 KB |
Output is correct |
10 |
Correct |
312 ms |
3736 KB |
Output is correct |
11 |
Correct |
273 ms |
3788 KB |
Output is correct |
12 |
Correct |
543 ms |
3636 KB |
Output is correct |
13 |
Correct |
292 ms |
3740 KB |
Output is correct |
14 |
Correct |
316 ms |
2324 KB |
Output is correct |
15 |
Correct |
516 ms |
2620 KB |
Output is correct |
16 |
Correct |
858 ms |
4052 KB |
Output is correct |
17 |
Correct |
904 ms |
4980 KB |
Output is correct |
18 |
Correct |
942 ms |
4980 KB |
Output is correct |
19 |
Correct |
443 ms |
4984 KB |
Output is correct |
20 |
Correct |
928 ms |
4988 KB |
Output is correct |
21 |
Correct |
866 ms |
4984 KB |
Output is correct |
22 |
Correct |
459 ms |
4980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
331 ms |
1836 KB |
Output is correct |
8 |
Correct |
337 ms |
2128 KB |
Output is correct |
9 |
Correct |
328 ms |
3724 KB |
Output is correct |
10 |
Correct |
312 ms |
3736 KB |
Output is correct |
11 |
Correct |
273 ms |
3788 KB |
Output is correct |
12 |
Correct |
543 ms |
3636 KB |
Output is correct |
13 |
Correct |
292 ms |
3740 KB |
Output is correct |
14 |
Correct |
316 ms |
2324 KB |
Output is correct |
15 |
Correct |
516 ms |
2620 KB |
Output is correct |
16 |
Correct |
858 ms |
4052 KB |
Output is correct |
17 |
Correct |
904 ms |
4980 KB |
Output is correct |
18 |
Correct |
942 ms |
4980 KB |
Output is correct |
19 |
Correct |
443 ms |
4984 KB |
Output is correct |
20 |
Correct |
928 ms |
4988 KB |
Output is correct |
21 |
Correct |
866 ms |
4984 KB |
Output is correct |
22 |
Correct |
459 ms |
4980 KB |
Output is correct |
23 |
Correct |
2306 ms |
10064 KB |
Output is correct |
24 |
Correct |
2514 ms |
9988 KB |
Output is correct |
25 |
Correct |
1653 ms |
9980 KB |
Output is correct |
26 |
Correct |
2027 ms |
9992 KB |
Output is correct |
27 |
Correct |
1864 ms |
10060 KB |
Output is correct |
28 |
Correct |
1152 ms |
2576 KB |
Output is correct |
29 |
Correct |
1124 ms |
2576 KB |
Output is correct |
30 |
Correct |
1156 ms |
2580 KB |
Output is correct |
31 |
Correct |
1125 ms |
2580 KB |
Output is correct |
32 |
Correct |
1330 ms |
9988 KB |
Output is correct |
33 |
Correct |
1353 ms |
9988 KB |
Output is correct |
34 |
Correct |
1627 ms |
9988 KB |
Output is correct |
35 |
Correct |
994 ms |
9996 KB |
Output is correct |
36 |
Correct |
409 ms |
9984 KB |
Output is correct |
37 |
Correct |
2253 ms |
9984 KB |
Output is correct |
38 |
Correct |
1534 ms |
9988 KB |
Output is correct |
39 |
Correct |
1461 ms |
9988 KB |
Output is correct |
40 |
Correct |
1473 ms |
10152 KB |
Output is correct |
41 |
Correct |
2785 ms |
9988 KB |
Output is correct |
42 |
Correct |
2826 ms |
9984 KB |
Output is correct |