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>
using namespace std;
const int N = 150005;
const int B = 500;
int n, L;
int a[N], in[N];
int jump[N], f[N];
int szVec;
vector<int> arr;
vector< pair<int, int> > vec[N];
void calc(vector< pair<int, int> > &vec) {
int ptr = 0, sz = vec.size();
for (int i = 0; i < sz; ++i) {
while (ptr < sz && vec[ptr].first <= vec[i].first + L) ptr++;
if (ptr == sz) jump[vec[i].second] = vec[i].second;
else jump[vec[i].second] = vec[ptr].second;
}
for (int i = sz - 1; i >= 0; --i) {
int j = vec[i].second;
int k = jump[j];
if (k == j) f[j] = 0;
else {
f[j] = f[k] + 1;
if (jump[k] != -1) jump[j] = jump[k];
}
}
}
void init(int _n, int _L, int X[]) {
n = _n, L = _L;
for (int i = 0; i < n; ++i) a[i] = X[i];
for (int i = 0; i < n; i += B) {
int r = min(n, i + B) - 1;
for (int j = i; j <= r; ++j) {
vec[szVec].push_back({a[j], j});
in[j] = szVec;
}
calc(vec[szVec]), arr.push_back(szVec++);
}
}
void push(int p, int x) {
vector< pair<int, int> > &cur = vec[arr[p]];
vector< pair<int, int> > buf = cur; cur.clear();
bool flag = 0;
for (auto i : buf) {
if (!flag && i.first > a[x]) {
flag = 1, cur.push_back({a[x], x});
}
cur.push_back(i);
}
if (!flag) cur.push_back({a[x], x});
buf.clear();
if (cur.size() == B * 2) {
for (int i = B; i < B * 2; ++i) {
buf.push_back(cur[i]);
in[cur[i].second] = szVec;
}
cur.resize(B);
arr.insert(arr.begin() + p + 1, szVec);
vec[szVec++] = buf;
calc(cur), calc(buf);
}
else {
calc(cur);
}
}
int get() {
// for (int i = 0; i < n; ++i) cout << a[i] << ' ' << jump[i] << ' ' << f[i] << '\n';
// for (auto i : arr) {
// cout << "#\n";
// for (auto j : vec[i]) cout << j.first << ' ' << j.second << '\n';
// }
int res = 0, cur = -1e9;
for (auto i : arr) {
if (vec[i].back().first <= cur) continue;
// cout << cur << '\n';
++res;
auto j = lower_bound(vec[i].begin(), vec[i].end(), make_pair(cur + 1, 0));
res += f[j -> second];
cur = a[jump[j -> second]] + L;
}
return res;
}
int update(int x, int y) {
if (n == 1) return 1;
for (int i = 0; i < arr.size(); ++i) {
int id = arr[i];
if (id != in[x]) continue;
vector< pair<int, int> > tmp = vec[id]; vec[id].clear();
for (auto j : tmp) if (j.second != x) vec[id].push_back(j);
if (!vec[id].size()) {
arr.erase(arr.begin() + i); break;
}
calc(vec[id]); break;
}
a[x] = y;
bool flag = 0;
for (int i = 0; i < arr.size(); ++i) {
int id = arr[i];
if (vec[id].back().first < a[x]) continue;
flag = 1, in[x] = arr[i], push(i, x); break;
}
if (!flag) {
in[x] = arr.back();
push(arr.size() - 1, x);
}
return get();
}
Compilation message (stderr)
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); ++i) {
~~^~~~~~~~~~~~
elephants.cpp:107:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); ++i) {
~~^~~~~~~~~~~~
# | 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... |