#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
int N, K, M, Q;
int Sq;
int B[400][1000], sz[400];
int D[400][1000], C[400][1000];
int A[150505], T[150505];
void calc(int t){
if (!sz[t]) return;
int k=sz[t]-1;
for (int i=sz[t]-1; i>=0; i--){
while (B[t][i] + K < B[t][k]) k--;
if (k == sz[t]-1) D[t][i] = 1, C[t][i] = B[t][i] + K;
else D[t][i] = D[t][k+1] + 1, C[t][i] = C[t][k+1];
}
}
void init(int n, int l, int X[])
{
N=n, K=l;
Sq = 400;
M = (N+Sq-1)/Sq;
for (int i=0; i<N; i++){
B[i/Sq][sz[i/Sq]++] = X[i];
A[i] = X[i];
}
for (int i=0; i<M; i++) calc(i);
}
void del(int x){
int t, k;
for (t=0; t<M; t++){
if (!sz[t]) continue;
k = lower_bound(B[t], B[t]+sz[t], x) - B[t];
if (k < sz[t] && B[t][k] == x) break;
}
sz[t]--;
for (; k<sz[t]; k++) B[t][k] = B[t][k+1];
calc(t);
}
void add(int x){
int t;
for (t=0; t<M-1; t++) if (sz[t] && x <= B[t][sz[t]-1]) break;
B[t][sz[t]++] = x;
for (int i=sz[t]-1; i>0 && B[t][i-1] > B[t][i]; i--) swap(B[t][i-1], B[t][i]);
calc(t);
}
int update(int x, int y)
{
del(A[x]);
A[x] = y;
add(y);
Q++;
if (Q % 400 == 0){
int cnt=0;
for (int i=0; i<M; i++){
for (int j=0; j<sz[i]; j++) T[cnt++] = B[i][j];
sz[i] = 0;
}
init(N, K, T);
}
int p=-1, ans=0;
for (int i=0; i<M; i++){
if (!sz[i]) continue;
int k = upper_bound(B[i], B[i]+sz[i], p) - B[i];
if (k == sz[i]) continue;
ans += D[i][k], p = C[i][k];
}
return ans;
}
Compilation message
elephants.cpp: In function 'void del(int)':
elephants.cpp:34:12: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
int t, k;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
223 ms |
1528 KB |
Output is correct |
8 |
Correct |
238 ms |
1760 KB |
Output is correct |
9 |
Correct |
328 ms |
2936 KB |
Output is correct |
10 |
Incorrect |
159 ms |
3064 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
223 ms |
1528 KB |
Output is correct |
8 |
Correct |
238 ms |
1760 KB |
Output is correct |
9 |
Correct |
328 ms |
2936 KB |
Output is correct |
10 |
Incorrect |
159 ms |
3064 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
223 ms |
1528 KB |
Output is correct |
8 |
Correct |
238 ms |
1760 KB |
Output is correct |
9 |
Correct |
328 ms |
2936 KB |
Output is correct |
10 |
Incorrect |
159 ms |
3064 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |