#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 = jump[vec[i].second];
if (j == -1) f[vec[i].second] = 0;
else {
f[vec[i].second] = f[j] + 1;
if (jump[j] != -1) jump[i] = jump[j];
}
}
}
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, 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:88:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); ++i) {
~~^~~~~~~~~~~~
elephants.cpp:100:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); ++i) {
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Incorrect |
4 ms |
992 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Incorrect |
4 ms |
992 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Incorrect |
4 ms |
992 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Incorrect |
4 ms |
992 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |