#include "elephants.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int n,l;
vi s;
int K;
const int oo = 2e9 + 5;
const int NN = 1e6 + 5;
int Step = 0;
multiset < int > ss;
vi v[NN];
vi dp[NN];
int SZ;
void build_block(int w) {
int sz = v[w].size();
int index = sz;
dp[w].resize(sz);
for (auto & it : dp[w])
it = 0;
for (int j = sz - 1;j >= 0;--j) {
while (v[w][j] + l < v[w][index - 1])
--index;
dp[w][j] = 1;
if (index < sz)
dp[w][j] += dp[w][index];
}
}
void del(int val) {
for (int i = 0;i < SZ;++i)
if (!v[i].empty() && v[i][0] <= val && val <= v[i].back()) {
vi nw;
for (auto it : v[i])
if (it != val)
nw.pb(it);
else
val = oo;
assert(nw.size() + 1 == v[i].size());
v[i] = nw;
build_block(i);
break;
}
}
void add(int val) {
int pr = SZ - 1;
for (int i = 0;i < SZ;++i)
if (!v[i].empty() && val <= v[i].back()) {
pr = i;
break;
}
vi nw;
for (auto it : v[pr]) {
if (it > val)
nw.pb(val),val = oo;
nw.pb(it);
}
if (val != oo)
nw.pb(val);
assert(v[pr].size() + 1 == nw.size());
v[pr] = nw;
build_block(pr);
}
void build(void) {
int index = -1;
SZ = (ss.size() + K - 1) / K;
for (int i = 0;i < SZ;++i)
v[i].clear(),dp[i].clear();
for (auto it : ss) {
++index;
v[index / K].pb(it);
}
for (int i = 0;i < SZ;++i) {
build_block(i);
}
}
void init(int N, int L, int X[])
{
n = N;
K = sqrt(n);
l = L;
for (int i = 0;i < n;++i)
s.pb(X[i]),ss.insert(X[i]);
}
int update(int i, int y)
{
if (!(Step % K)) {
build();
}
++Step;
ss.erase(ss.find(s[i]));
del(s[i]);
ss.insert(y);
add(s[i] = y);
int prev = -oo;
int ans = 0;
for (int i = 0;i + 1 < SZ;++i)
if (!v[i].empty() && !v[i + 1].empty()) {
assert(v[i].back() <= v[i + 1][0]);
}
for (int i = 0;i < SZ;++i) {
for (int j = 0;j + 1 < v[i].size();++j)
assert(v[i][j] <= v[i][j + 1]);
}
int sum = 0;
for (int i = 0;i < SZ;++i)
sum += v[i].size();
assert(sum == n);
for (int i = 0;i < SZ;++i) {
if (v[i].empty())
continue;
int low = lower_bound(all(v[i]),prev) - v[i].begin();
if (low != v[i].size()) {
ans += dp[i][low];
prev = v[i][low] + l + 1;
}
}
return ans;
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:129:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0;j + 1 < v[i].size();++j)
~~~~~~^~~~~~~~~~~~~
elephants.cpp:140:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (low != v[i].size()) {
~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47352 KB |
Output is correct |
2 |
Correct |
53 ms |
47352 KB |
Output is correct |
3 |
Correct |
52 ms |
47388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47352 KB |
Output is correct |
2 |
Correct |
53 ms |
47352 KB |
Output is correct |
3 |
Correct |
52 ms |
47388 KB |
Output is correct |
4 |
Incorrect |
56 ms |
47436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47352 KB |
Output is correct |
2 |
Correct |
53 ms |
47352 KB |
Output is correct |
3 |
Correct |
52 ms |
47388 KB |
Output is correct |
4 |
Incorrect |
56 ms |
47436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47352 KB |
Output is correct |
2 |
Correct |
53 ms |
47352 KB |
Output is correct |
3 |
Correct |
52 ms |
47388 KB |
Output is correct |
4 |
Incorrect |
56 ms |
47436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
47352 KB |
Output is correct |
2 |
Correct |
53 ms |
47352 KB |
Output is correct |
3 |
Correct |
52 ms |
47388 KB |
Output is correct |
4 |
Incorrect |
56 ms |
47436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |