#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
const int MAXN = 150004;
const int B = 512;
struct Data{
int pos;
int jumpl;
int nexp;
bool operator<(const Data &D) const {
return pos < D.pos;
}
};
vector<Data> Q[MAXN];
int n;
int bl;
int len;
void compute(int id){
if(Q[id].empty())
return;
int sz = Q[id].size();
int lp = sz - 1;
for(int i = sz - 1; i >= 0 ; i -- ){
while(Q[id][lp].pos - Q[id][i].pos > len){
-- lp;
}
if(lp == sz - 1){
Q[id][i].jumpl = 1;
Q[id][i].nexp = Q[id][i].pos + len;
}
else{
Q[id][i].jumpl = Q[id][lp + 1].jumpl + 1;
Q[id][i].nexp = Q[id][lp + 1].nexp;
}
}
}
int pz[MAXN];
int oper = 1;
void init(int N, int L, int X[]){
n = N;
len = L;
bl = (n + B - 1) / B;
for(int i = 0 ; i < n ; i ++ )
pz[i] = X[i];
for(int i = 0 ; i < n ; i ++ ){
Q[i/B].push_back({X[i], -1, -1});
}
for(int i = 0 ; i < bl ; i ++ )
compute(i);
}
bool check(int id ,int el){
if(Q[id].empty())
return false;
Data W = {el, -1, -1};
int sd = lower_bound(Q[id].begin() ,Q[id].end(), W) - Q[id].begin();
if(sd < Q[id].size()){
return Q[id][sd].pos == el;
}
return false;
}
void Erase(int el){
int id = -1;
for(int i = bl - 1; i >= 0 ; i -- ){
if(Q[i].empty())continue;
if(check(i, el)){
id = i;
break;
}
}
vector<int> idx;
bool er = false;
for(auto p : Q[id]){
if(p.pos == el){
if(er){
idx.push_back(p.pos);
}
else{
er=true;
}
}
else{
idx.push_back(p.pos);
}
}
Q[id].clear();
for(auto v : idx){
Q[id].push_back({v, -1, -1});
}
compute(id);
}
void Insert(int el){
int id = 0;
for(int i = bl - 1; i >= 0 ; i -- ){
if(Q[i].empty()) continue;
if(el >= Q[i][0].pos){
id = i;
break;
}
}
vector<int> ver;
for(auto p : Q[id]){
if(el <= p.pos && el != -1){
ver.push_back(el);
el = -1;
}
ver.push_back(p.pos);
}
if(el!=-1)
ver.push_back(el);
Q[id].clear();
for(auto p : ver)
Q[id].push_back({p,-1,-1});
compute(id);
}
int update(int i, int y){
oper ++ ;
oper %= B;
if(oper == 0){
vector<int> tot;
for(int j = 0 ; j < bl ; j ++ ){
for(auto p : Q[j])
tot.push_back(p.pos);
Q[j].clear();
}
for(int j = 0 ; j < n; j ++ ){
Q[j / B].push_back({tot[j], -1, -1});
}
for(int j = 0 ; j < bl; j ++ ) compute(j);
}
Erase(pz[i]);
pz[i]=y;
Insert(pz[i]);
int endp = -1;
int res = 0;
int rd;
Data C;
for(int j = 0 ; j < bl ; j ++ ){
if(Q[j].empty())
continue;
C = {endp, -1, -1};
rd = upper_bound(Q[j].begin(), Q[j].end(), C) - Q[j].begin();
if(rd < Q[j].size()){
res += Q[j][rd].jumpl;
endp = Q[j][rd].nexp;
}
}
return res;
}
Compilation message
elephants.cpp: In function 'bool check(int, int)':
elephants.cpp:72:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(sd < Q[id].size()){
~~~^~~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:161:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(rd < Q[j].size()){
~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3840 KB |
Output is correct |
2 |
Correct |
5 ms |
3840 KB |
Output is correct |
3 |
Correct |
6 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3840 KB |
Output is correct |
2 |
Correct |
5 ms |
3840 KB |
Output is correct |
3 |
Correct |
6 ms |
3840 KB |
Output is correct |
4 |
Correct |
7 ms |
3840 KB |
Output is correct |
5 |
Correct |
6 ms |
3840 KB |
Output is correct |
6 |
Correct |
7 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3840 KB |
Output is correct |
2 |
Correct |
5 ms |
3840 KB |
Output is correct |
3 |
Correct |
6 ms |
3840 KB |
Output is correct |
4 |
Correct |
7 ms |
3840 KB |
Output is correct |
5 |
Correct |
6 ms |
3840 KB |
Output is correct |
6 |
Correct |
7 ms |
3840 KB |
Output is correct |
7 |
Correct |
779 ms |
4996 KB |
Output is correct |
8 |
Correct |
912 ms |
5468 KB |
Output is correct |
9 |
Correct |
1067 ms |
6448 KB |
Output is correct |
10 |
Correct |
970 ms |
6172 KB |
Output is correct |
11 |
Correct |
1111 ms |
7472 KB |
Output is correct |
12 |
Correct |
1270 ms |
8172 KB |
Output is correct |
13 |
Correct |
943 ms |
7344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3840 KB |
Output is correct |
2 |
Correct |
5 ms |
3840 KB |
Output is correct |
3 |
Correct |
6 ms |
3840 KB |
Output is correct |
4 |
Correct |
7 ms |
3840 KB |
Output is correct |
5 |
Correct |
6 ms |
3840 KB |
Output is correct |
6 |
Correct |
7 ms |
3840 KB |
Output is correct |
7 |
Correct |
779 ms |
4996 KB |
Output is correct |
8 |
Correct |
912 ms |
5468 KB |
Output is correct |
9 |
Correct |
1067 ms |
6448 KB |
Output is correct |
10 |
Correct |
970 ms |
6172 KB |
Output is correct |
11 |
Correct |
1111 ms |
7472 KB |
Output is correct |
12 |
Correct |
1270 ms |
8172 KB |
Output is correct |
13 |
Correct |
943 ms |
7344 KB |
Output is correct |
14 |
Correct |
1170 ms |
7088 KB |
Output is correct |
15 |
Correct |
1270 ms |
7328 KB |
Output is correct |
16 |
Correct |
1824 ms |
8756 KB |
Output is correct |
17 |
Correct |
1871 ms |
9764 KB |
Output is correct |
18 |
Correct |
1938 ms |
9864 KB |
Output is correct |
19 |
Correct |
1379 ms |
9240 KB |
Output is correct |
20 |
Correct |
2031 ms |
9764 KB |
Output is correct |
21 |
Correct |
1804 ms |
9984 KB |
Output is correct |
22 |
Correct |
1464 ms |
8740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3840 KB |
Output is correct |
2 |
Correct |
5 ms |
3840 KB |
Output is correct |
3 |
Correct |
6 ms |
3840 KB |
Output is correct |
4 |
Correct |
7 ms |
3840 KB |
Output is correct |
5 |
Correct |
6 ms |
3840 KB |
Output is correct |
6 |
Correct |
7 ms |
3840 KB |
Output is correct |
7 |
Correct |
779 ms |
4996 KB |
Output is correct |
8 |
Correct |
912 ms |
5468 KB |
Output is correct |
9 |
Correct |
1067 ms |
6448 KB |
Output is correct |
10 |
Correct |
970 ms |
6172 KB |
Output is correct |
11 |
Correct |
1111 ms |
7472 KB |
Output is correct |
12 |
Correct |
1270 ms |
8172 KB |
Output is correct |
13 |
Correct |
943 ms |
7344 KB |
Output is correct |
14 |
Correct |
1170 ms |
7088 KB |
Output is correct |
15 |
Correct |
1270 ms |
7328 KB |
Output is correct |
16 |
Correct |
1824 ms |
8756 KB |
Output is correct |
17 |
Correct |
1871 ms |
9764 KB |
Output is correct |
18 |
Correct |
1938 ms |
9864 KB |
Output is correct |
19 |
Correct |
1379 ms |
9240 KB |
Output is correct |
20 |
Correct |
2031 ms |
9764 KB |
Output is correct |
21 |
Correct |
1804 ms |
9984 KB |
Output is correct |
22 |
Correct |
1464 ms |
8740 KB |
Output is correct |
23 |
Correct |
5233 ms |
16824 KB |
Output is correct |
24 |
Correct |
5502 ms |
16980 KB |
Output is correct |
25 |
Correct |
4738 ms |
16428 KB |
Output is correct |
26 |
Correct |
5017 ms |
15632 KB |
Output is correct |
27 |
Correct |
4514 ms |
15392 KB |
Output is correct |
28 |
Correct |
3241 ms |
8824 KB |
Output is correct |
29 |
Correct |
3100 ms |
8832 KB |
Output is correct |
30 |
Correct |
2902 ms |
8936 KB |
Output is correct |
31 |
Correct |
2894 ms |
8824 KB |
Output is correct |
32 |
Correct |
6312 ms |
14944 KB |
Output is correct |
33 |
Correct |
4994 ms |
14208 KB |
Output is correct |
34 |
Correct |
5319 ms |
15312 KB |
Output is correct |
35 |
Correct |
5735 ms |
15592 KB |
Output is correct |
36 |
Correct |
4528 ms |
15048 KB |
Output is correct |
37 |
Correct |
5632 ms |
17044 KB |
Output is correct |
38 |
Correct |
5113 ms |
14228 KB |
Output is correct |
39 |
Correct |
4473 ms |
15256 KB |
Output is correct |
40 |
Correct |
6152 ms |
14116 KB |
Output is correct |
41 |
Correct |
6681 ms |
16548 KB |
Output is correct |
42 |
Correct |
6398 ms |
16644 KB |
Output is correct |