#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
int N, K, M, Q;
int Sq=400;
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;
M = (N+Sq-1)/Sq;
for (int i=0; i<N; i++){
B[i/Sq][sz[i/Sq]++] = X[i];
if (!Q) 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 % Sq == 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:33:12: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
int t, k;
^
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
232 ms |
1508 KB |
Output is correct |
8 |
Correct |
243 ms |
1664 KB |
Output is correct |
9 |
Correct |
356 ms |
2944 KB |
Output is correct |
10 |
Correct |
288 ms |
3052 KB |
Output is correct |
11 |
Correct |
285 ms |
4344 KB |
Output is correct |
12 |
Correct |
518 ms |
4472 KB |
Output is correct |
13 |
Correct |
293 ms |
4088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
232 ms |
1508 KB |
Output is correct |
8 |
Correct |
243 ms |
1664 KB |
Output is correct |
9 |
Correct |
356 ms |
2944 KB |
Output is correct |
10 |
Correct |
288 ms |
3052 KB |
Output is correct |
11 |
Correct |
285 ms |
4344 KB |
Output is correct |
12 |
Correct |
518 ms |
4472 KB |
Output is correct |
13 |
Correct |
293 ms |
4088 KB |
Output is correct |
14 |
Correct |
257 ms |
3512 KB |
Output is correct |
15 |
Correct |
363 ms |
3704 KB |
Output is correct |
16 |
Correct |
821 ms |
5112 KB |
Output is correct |
17 |
Correct |
884 ms |
6264 KB |
Output is correct |
18 |
Correct |
1004 ms |
6008 KB |
Output is correct |
19 |
Correct |
739 ms |
6392 KB |
Output is correct |
20 |
Correct |
952 ms |
6188 KB |
Output is correct |
21 |
Correct |
883 ms |
6268 KB |
Output is correct |
22 |
Correct |
526 ms |
5752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
232 ms |
1508 KB |
Output is correct |
8 |
Correct |
243 ms |
1664 KB |
Output is correct |
9 |
Correct |
356 ms |
2944 KB |
Output is correct |
10 |
Correct |
288 ms |
3052 KB |
Output is correct |
11 |
Correct |
285 ms |
4344 KB |
Output is correct |
12 |
Correct |
518 ms |
4472 KB |
Output is correct |
13 |
Correct |
293 ms |
4088 KB |
Output is correct |
14 |
Correct |
257 ms |
3512 KB |
Output is correct |
15 |
Correct |
363 ms |
3704 KB |
Output is correct |
16 |
Correct |
821 ms |
5112 KB |
Output is correct |
17 |
Correct |
884 ms |
6264 KB |
Output is correct |
18 |
Correct |
1004 ms |
6008 KB |
Output is correct |
19 |
Correct |
739 ms |
6392 KB |
Output is correct |
20 |
Correct |
952 ms |
6188 KB |
Output is correct |
21 |
Correct |
883 ms |
6268 KB |
Output is correct |
22 |
Correct |
526 ms |
5752 KB |
Output is correct |
23 |
Correct |
3127 ms |
13384 KB |
Output is correct |
24 |
Correct |
3147 ms |
13304 KB |
Output is correct |
25 |
Correct |
2443 ms |
13304 KB |
Output is correct |
26 |
Correct |
2282 ms |
13304 KB |
Output is correct |
27 |
Correct |
3429 ms |
13208 KB |
Output is correct |
28 |
Correct |
1040 ms |
5240 KB |
Output is correct |
29 |
Correct |
984 ms |
5368 KB |
Output is correct |
30 |
Correct |
1039 ms |
5244 KB |
Output is correct |
31 |
Correct |
1006 ms |
5296 KB |
Output is correct |
32 |
Correct |
2122 ms |
12920 KB |
Output is correct |
33 |
Correct |
1893 ms |
12152 KB |
Output is correct |
34 |
Correct |
1924 ms |
13048 KB |
Output is correct |
35 |
Correct |
1706 ms |
13304 KB |
Output is correct |
36 |
Correct |
1612 ms |
12892 KB |
Output is correct |
37 |
Correct |
2802 ms |
13180 KB |
Output is correct |
38 |
Correct |
2248 ms |
12000 KB |
Output is correct |
39 |
Correct |
3372 ms |
13148 KB |
Output is correct |
40 |
Correct |
2137 ms |
12064 KB |
Output is correct |
41 |
Correct |
3405 ms |
12812 KB |
Output is correct |
42 |
Correct |
3493 ms |
13048 KB |
Output is correct |