#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN=1.5e5, B=300, mxBC=(mxN-1)/B+1;
int n, l, *x, uc, bc, r[mxBC];
map<int, int> mp;
vector<int> v[mxBC];
vector<array<int, 2>> w[mxBC];
void init(int N, int L, int X[]) {
n=N;
l=L;
x=X;
for(int i=0; i<n; ++i)
++mp[x[i]];
}
void bb(int i) {
w[i]=vector<array<int, 2>>(v[i].size());
for(int j1=(int)v[i].size()-1, j2=v[i].size(); ~j1; --j1) {
while(v[i][j2-1]>v[i][j1]+l)
--j2;
w[i][j1]=j2<v[i].size()?array<int, 2>{w[i][j2][0]+1, w[i][j2][1]}:array<int, 2>{1, v[i][j1]+l};
}
}
void bld() {
bc=0;
for(pair<int, int> p : mp) {
if(!bc||v[bc-1].size()>=B)
v[bc++].clear();
v[bc-1].push_back(p.first);
}
for(int i=0; i<bc; ++i) {
r[i]=i+1<bc?v[i+1][0]-1:1e9;
bb(i);
}
}
void upd(int x, bool a) {
int c=-1;
while(x>r[++c]);
int p=lower_bound(v[c].begin(), v[c].end(), x)-v[c].begin();
if(a)
v[c].insert(v[c].begin()+p, x);
else
v[c].erase(v[c].begin()+p);
bb(c);
}
int update(int i, int y) {
if(uc++%B==0)
bld();
--mp[x[i]];
if(!mp[x[i]]) {
upd(x[i], 0);
mp.erase(x[i]);
}
if(!mp[y])
upd(y, 1);
++mp[y];
x[i]=y;
int a=0;
for(int i=0, c=-1; i<bc; ++i) {
int p=upper_bound(v[i].begin(), v[i].end(), c)-v[i].begin();
if(p<v[i].size()) {
a+=w[i][p][0];
c=w[i][p][1];
}
}
return a;
}
Compilation message
elephants.cpp: In function 'void bb(int)':
elephants.cpp:24:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
w[i][j1]=j2<v[i].size()?array<int, 2>{w[i][j2][0]+1, w[i][j2][1]}:array<int, 2>{1, v[i][j1]+l};
~~^~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:66:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p<v[i].size()) {
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
304 ms |
1964 KB |
Output is correct |
8 |
Correct |
339 ms |
3156 KB |
Output is correct |
9 |
Correct |
490 ms |
5880 KB |
Output is correct |
10 |
Correct |
438 ms |
5624 KB |
Output is correct |
11 |
Correct |
488 ms |
5564 KB |
Output is correct |
12 |
Correct |
840 ms |
5804 KB |
Output is correct |
13 |
Correct |
443 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
304 ms |
1964 KB |
Output is correct |
8 |
Correct |
339 ms |
3156 KB |
Output is correct |
9 |
Correct |
490 ms |
5880 KB |
Output is correct |
10 |
Correct |
438 ms |
5624 KB |
Output is correct |
11 |
Correct |
488 ms |
5564 KB |
Output is correct |
12 |
Correct |
840 ms |
5804 KB |
Output is correct |
13 |
Correct |
443 ms |
5496 KB |
Output is correct |
14 |
Correct |
246 ms |
3964 KB |
Output is correct |
15 |
Correct |
548 ms |
4200 KB |
Output is correct |
16 |
Correct |
1262 ms |
6264 KB |
Output is correct |
17 |
Correct |
1559 ms |
8100 KB |
Output is correct |
18 |
Correct |
1630 ms |
7904 KB |
Output is correct |
19 |
Correct |
890 ms |
8004 KB |
Output is correct |
20 |
Correct |
1693 ms |
8076 KB |
Output is correct |
21 |
Correct |
1570 ms |
7932 KB |
Output is correct |
22 |
Correct |
853 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
304 ms |
1964 KB |
Output is correct |
8 |
Correct |
339 ms |
3156 KB |
Output is correct |
9 |
Correct |
490 ms |
5880 KB |
Output is correct |
10 |
Correct |
438 ms |
5624 KB |
Output is correct |
11 |
Correct |
488 ms |
5564 KB |
Output is correct |
12 |
Correct |
840 ms |
5804 KB |
Output is correct |
13 |
Correct |
443 ms |
5496 KB |
Output is correct |
14 |
Correct |
246 ms |
3964 KB |
Output is correct |
15 |
Correct |
548 ms |
4200 KB |
Output is correct |
16 |
Correct |
1262 ms |
6264 KB |
Output is correct |
17 |
Correct |
1559 ms |
8100 KB |
Output is correct |
18 |
Correct |
1630 ms |
7904 KB |
Output is correct |
19 |
Correct |
890 ms |
8004 KB |
Output is correct |
20 |
Correct |
1693 ms |
8076 KB |
Output is correct |
21 |
Correct |
1570 ms |
7932 KB |
Output is correct |
22 |
Correct |
853 ms |
7424 KB |
Output is correct |
23 |
Correct |
5522 ms |
17028 KB |
Output is correct |
24 |
Correct |
5238 ms |
17064 KB |
Output is correct |
25 |
Correct |
4661 ms |
17024 KB |
Output is correct |
26 |
Correct |
4869 ms |
17048 KB |
Output is correct |
27 |
Correct |
4969 ms |
16900 KB |
Output is correct |
28 |
Correct |
1352 ms |
5780 KB |
Output is correct |
29 |
Correct |
1289 ms |
5644 KB |
Output is correct |
30 |
Correct |
1349 ms |
5680 KB |
Output is correct |
31 |
Correct |
1282 ms |
5528 KB |
Output is correct |
32 |
Correct |
5350 ms |
16484 KB |
Output is correct |
33 |
Correct |
1806 ms |
16128 KB |
Output is correct |
34 |
Correct |
4308 ms |
16692 KB |
Output is correct |
35 |
Correct |
1798 ms |
17096 KB |
Output is correct |
36 |
Correct |
79 ms |
7368 KB |
Output is correct |
37 |
Correct |
3325 ms |
16948 KB |
Output is correct |
38 |
Correct |
4296 ms |
15736 KB |
Output is correct |
39 |
Correct |
4053 ms |
16632 KB |
Output is correct |
40 |
Correct |
4437 ms |
15712 KB |
Output is correct |
41 |
Correct |
7599 ms |
16668 KB |
Output is correct |
42 |
Correct |
7899 ms |
16960 KB |
Output is correct |