#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 15005;
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] = -1;
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 == -1) 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() {
int res = 0, cur = -1e9;
for (auto i : arr) {
if (vec[i].back().first <= cur) continue;
++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][0].first > a[x] || 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
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:89:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); ++i) {
~~^~~~~~~~~~~~
elephants.cpp:101: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 |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
796 KB |
Output is correct |
4 |
Correct |
3 ms |
1000 KB |
Output is correct |
5 |
Correct |
4 ms |
1028 KB |
Output is correct |
6 |
Correct |
3 ms |
1028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
796 KB |
Output is correct |
4 |
Correct |
3 ms |
1000 KB |
Output is correct |
5 |
Correct |
4 ms |
1028 KB |
Output is correct |
6 |
Correct |
3 ms |
1028 KB |
Output is correct |
7 |
Incorrect |
33 ms |
1848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
796 KB |
Output is correct |
4 |
Correct |
3 ms |
1000 KB |
Output is correct |
5 |
Correct |
4 ms |
1028 KB |
Output is correct |
6 |
Correct |
3 ms |
1028 KB |
Output is correct |
7 |
Incorrect |
33 ms |
1848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
760 KB |
Output is correct |
3 |
Correct |
3 ms |
796 KB |
Output is correct |
4 |
Correct |
3 ms |
1000 KB |
Output is correct |
5 |
Correct |
4 ms |
1028 KB |
Output is correct |
6 |
Correct |
3 ms |
1028 KB |
Output is correct |
7 |
Incorrect |
33 ms |
1848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |