This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "elephants.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1.5e5 + 5;
const int B = 390;
int n, k;
int R[N + 5], pos[N];
map<int, int> mp;
vector<int> jump[N + 5], cnt[N + 5], bucket[N + 5];
void init(int _n, int _k, int X[]) {
n = _n, k = _k;
for(int i = 0; i < n; i++) ++mp[pos[i] = X[i]];
}
void solve_bucket(int idx) {
vector<int> &vec = bucket[idx];
jump[idx] = vector<int>(vec.size()), cnt[idx] = vector<int>(vec.size());
for(int i = vec.size() - 1; ~i; i--) {
int nex = upper_bound(vec.begin(), vec.end(), vec[i] + k) - vec.begin();
if(nex >= vec.size()) jump[idx][i] = vec[i] + k, cnt[idx][i] = 1;
else jump[idx][i] = jump[idx][nex], cnt[idx][i] = cnt[idx][nex] + 1;
}
}
void revalidate() {
int ptr = 0;
for(pii p : mp) {
if(!ptr || bucket[ptr - 1].size() >= B)
bucket[ptr++].clear();
bucket[ptr - 1].emplace_back(p.x);
}
for(int i = 0; i < B; i++) if(bucket[i].size()) {
if(bucket[i + 1].size()) R[i] = bucket[i + 1][0];
else R[i] = 2e9;
solve_bucket(i);
}
}
void add_elephant(int x) {
int ptr = 0;
while(R[ptr] < x) ++ptr;
int idx = lower_bound(bucket[ptr].begin(), bucket[ptr].end(), x) - bucket[ptr].begin();
bucket[ptr].insert(bucket[ptr].begin() + idx, x);
solve_bucket(ptr);
}
void del_elephant(int x) {
int ptr = 0;
while(R[ptr] < x) ++ptr;
bucket[ptr].erase(lower_bound(bucket[ptr].begin(), bucket[ptr].end(), x));
solve_bucket(ptr);
}
int counter;
int update(int i, int y) {
if(counter++ % B == 0) revalidate();
if(!--mp[pos[i]]) del_elephant(pos[i]);
if(!mp[pos[i] = y]++) add_elephant(y);
int answer = 0, cur = -1;
for(int i = 0; i < B; i++) {
int idx = upper_bound(bucket[i].begin(), bucket[i].end(), cur) - bucket[i].begin();
if(idx < bucket[i].size())
answer += cnt[i][idx], cur = jump[i][idx];
}
return answer;
}
Compilation message (stderr)
elephants.cpp: In function 'void solve_bucket(int)':
elephants.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(nex >= vec.size()) jump[idx][i] = vec[i] + k, cnt[idx][i] = 1;
| ~~~~^~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:77:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if(idx < bucket[i].size())
| ~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |