#include "elephants.h"
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pii pair<int, int>
using namespace std;
#define SQ 400
int n, l;
int loc[150005];
int bid[150005];
vector<vector<pair<int , pii> > > buck;
vector<pair<int, pii> > emp;
//{LOC {count, last} }
int find(){
int res = 0;
int lst = -1;
for(auto& v : buck){
if(v.empty() || v.back().ff <= lst) continue;
int id = upper_bound(v.begin(), v.end(), lst, [](const int& lhs, const pair<int, pii>& rhs){
return lhs < rhs.ff;
}) - v.begin();
res += v[id].ss.ff;
lst = v[id].ss.ss;
}
return res;
}
void recalc(vector<pair<int, pii> >& vec){
int sz = vec.size();
int r = sz;
for(int l = sz - 1; l >= 0; l--){
while(vec[r - 1].ff > vec[l].ff + ::l) r--;
vec[l].ss.ff = (r == sz ? 0 : vec[r].ss.ff) + 1;
vec[l].ss.ss = (r == sz ? vec[l].ff + ::l : vec[r].ss.ss);
}
}
vector<pii> sorted;
void renew(){
for(int i = 0; i < n; i++){
sorted[i].ff = loc[i];
sorted[i].ss = i;
}
sort(sorted.begin(), sorted.end());
buck.clear();
for(int i = 0; i < n; i++){
if(i % SQ == 0){
buck.push_back(emp);
}
buck.back().push_back({sorted[i].ff, {0, 0}});
bid[sorted[i].ss] = i / SQ;
}
for(auto& x : buck){
recalc(x);
}
}
void init(int N, int L, int X[]){
n = N; l = L;
for(int i = 0; i < n; i++){
loc[i] = X[i];
}
sorted.resize(n);
renew();
}
int cnt = 0;
void erase(vector<pair<int, pii> >& buck, int x){
for(int i = 1; i < buck.size(); i++){
if(buck[i - 1].ff == x) swap(buck[i - 1], buck[i]);
}
buck.pop_back();
recalc(buck);
}
void insert(vector<pair<int, pii> >& buck, int x){
buck.push_back({x, {0, 0}});
int i = buck.size() - 1;
while(i != 0 && buck[i - 1].ff > buck[i].ff){
swap(buck[i - 1], buck[i]);
i--;
}
recalc(buck);
}
int update(int j, int y){
if(loc[j] == y) return find();
if(++cnt % SQ == 0){
loc[j] = y;
renew();
} else {
erase(buck[bid[j]], loc[j]);
int t = upper_bound(buck.begin(), buck.end(), y, [](const int& lhs, const vector<pair<int, pii> >& rhs){
return rhs.back().ff >= lhs;
}) - buck.begin();
if(t == buck.size()) t--;
insert(buck[t], y);
loc[j] = y;
bid[j] = t;
}
return find();
}
Compilation message
elephants.cpp: In function 'void erase(std::vector<std::pair<int, std::pair<int, int> > >&, int)':
elephants.cpp:69:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < buck.size(); i++){
~~^~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:94:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(t == buck.size()) t--;
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
336 ms |
1596 KB |
Output is correct |
8 |
Correct |
374 ms |
1760 KB |
Output is correct |
9 |
Correct |
580 ms |
2824 KB |
Output is correct |
10 |
Correct |
727 ms |
2924 KB |
Output is correct |
11 |
Correct |
738 ms |
2984 KB |
Output is correct |
12 |
Correct |
1164 ms |
2944 KB |
Output is correct |
13 |
Correct |
825 ms |
2860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
336 ms |
1596 KB |
Output is correct |
8 |
Correct |
374 ms |
1760 KB |
Output is correct |
9 |
Correct |
580 ms |
2824 KB |
Output is correct |
10 |
Correct |
727 ms |
2924 KB |
Output is correct |
11 |
Correct |
738 ms |
2984 KB |
Output is correct |
12 |
Correct |
1164 ms |
2944 KB |
Output is correct |
13 |
Correct |
825 ms |
2860 KB |
Output is correct |
14 |
Correct |
522 ms |
1988 KB |
Output is correct |
15 |
Correct |
577 ms |
2188 KB |
Output is correct |
16 |
Correct |
1795 ms |
3184 KB |
Output is correct |
17 |
Correct |
2093 ms |
4052 KB |
Output is correct |
18 |
Correct |
2209 ms |
3864 KB |
Output is correct |
19 |
Correct |
1370 ms |
4056 KB |
Output is correct |
20 |
Correct |
2098 ms |
4096 KB |
Output is correct |
21 |
Correct |
2037 ms |
4004 KB |
Output is correct |
22 |
Correct |
1504 ms |
4148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
336 ms |
1596 KB |
Output is correct |
8 |
Correct |
374 ms |
1760 KB |
Output is correct |
9 |
Correct |
580 ms |
2824 KB |
Output is correct |
10 |
Correct |
727 ms |
2924 KB |
Output is correct |
11 |
Correct |
738 ms |
2984 KB |
Output is correct |
12 |
Correct |
1164 ms |
2944 KB |
Output is correct |
13 |
Correct |
825 ms |
2860 KB |
Output is correct |
14 |
Correct |
522 ms |
1988 KB |
Output is correct |
15 |
Correct |
577 ms |
2188 KB |
Output is correct |
16 |
Correct |
1795 ms |
3184 KB |
Output is correct |
17 |
Correct |
2093 ms |
4052 KB |
Output is correct |
18 |
Correct |
2209 ms |
3864 KB |
Output is correct |
19 |
Correct |
1370 ms |
4056 KB |
Output is correct |
20 |
Correct |
2098 ms |
4096 KB |
Output is correct |
21 |
Correct |
2037 ms |
4004 KB |
Output is correct |
22 |
Correct |
1504 ms |
4148 KB |
Output is correct |
23 |
Correct |
4741 ms |
7776 KB |
Output is correct |
24 |
Correct |
5098 ms |
7688 KB |
Output is correct |
25 |
Correct |
4041 ms |
7780 KB |
Output is correct |
26 |
Correct |
5964 ms |
7812 KB |
Output is correct |
27 |
Correct |
6612 ms |
7772 KB |
Output is correct |
28 |
Correct |
1599 ms |
2296 KB |
Output is correct |
29 |
Correct |
1553 ms |
2324 KB |
Output is correct |
30 |
Correct |
1597 ms |
2428 KB |
Output is correct |
31 |
Correct |
1565 ms |
2324 KB |
Output is correct |
32 |
Correct |
5581 ms |
7744 KB |
Output is correct |
33 |
Correct |
5307 ms |
7756 KB |
Output is correct |
34 |
Correct |
5890 ms |
7808 KB |
Output is correct |
35 |
Correct |
5332 ms |
7780 KB |
Output is correct |
36 |
Correct |
168 ms |
7288 KB |
Output is correct |
37 |
Execution timed out |
9023 ms |
7780 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |