#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
const int N = 150001;
const int sz = 1000;
const int K = N / sz + 2;
int n, L, f[K][sz], g[K][sz], B, X[N], st[K], query;
vector<int> block[K];
void build(int k) {
int m = block[k].size() - 1;
f[k][m] = 0;
g[k][m] = block[k].back();
for(int i = m - 1, t = m; i >= 0; --i) {
while(block[k][i] + L < block[k][t]) --t; ++t;
f[k][i] = f[k][t] + 1;
g[k][i] = t == m ? block[k][i] + L + 1 : g[k][t];
}
}
void rebuild() {
vector<int> v;
for(int i = 0; i < B; ++i) {
for(int j = 0; j < block[i].size() - 1; ++j)
v.push_back(block[i][j]);
}
for(int i = 0; i < n; ++i)
block[i / sz].push_back(v[i]);
for(int k = 0; k < B; ++k) if(block[k].size()) {
block[k].push_back(block[k].back() + L + 1);
build(k);
st[k] = block[k][0];
}
}
void init(int _n, int _L, int _X[]) {
n = _n; L = _L; B = 0;
for(int i = 0; i < n; ++i) {
block[i / sz].push_back(X[i] = _X[i]);
if(i % sz == 0) ++B;
}
for(int k = 0; k < B; ++k) if(block[k].size()) {
block[k].push_back(block[k].back() + L + 1);
build(k);
st[k] = block[k][0];
}
}
int answer() {
int cur = -1e9, ans = 0;
for(int i = 0; i < B; ++i) {
int j = lower_bound(block[i].begin(), block[i].end(), cur) - block[i].begin();
if(j >= block[i].size()) continue;
ans += f[i][j];
cur = g[i][j];
} return ans;
}
int find(int x) {
int i = lower_bound(st, st + B, x) - st;
if(i >= B) --i;
return i;
}
int update(int k, int y) {
if(++query % sz == 0) rebuild();
int lb = find(X[k]);
int rb = find(y);
int pos = lower_bound(block[lb].begin(), block[lb].end(), X[k]) - block[lb].begin();
X[k] = y;
for(int i = pos; i < block[lb].size() - 1; ++i)
swap(block[lb][i], block[lb][i + 1]);
block[lb].pop_back(); block[lb].pop_back();
block[lb].push_back(block[lb].back() + L + 1);
block[rb].pop_back(); block[rb].push_back(y);
for(int i = block[rb].size() - 1; i >= 1; --i) {
if(block[rb][i - 1] > block[rb][i]) {
swap(block[rb][i], block[rb][i - 1]);
} else break;
}
block[rb].push_back(block[rb].back() + L + 1);
build(lb);
if(lb != rb) build(rb);
return answer();
}
Compilation message
elephants.cpp: In function 'void build(int)':
elephants.cpp:17:3: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
while(block[k][i] + L < block[k][t]) --t; ++t;
^~~~~
elephants.cpp:17:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
while(block[k][i] + L < block[k][t]) --t; ++t;
^~
elephants.cpp: In function 'void rebuild()':
elephants.cpp:26:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < block[i].size() - 1; ++j)
~~^~~~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'int answer()':
elephants.cpp:55:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(j >= block[i].size()) continue;
~~^~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:76:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = pos; i < block[lb].size() - 1; ++i)
~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Incorrect |
20 ms |
1320 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Incorrect |
20 ms |
1320 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Incorrect |
20 ms |
1320 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |