#include <bits/stdc++.h>
using namespace std;
#define ar2 array<int, 2>
#define lb(v, x) lower_bound(v.begin(), v.end(), x)-v.begin()
const int mx = 1.5e5+5, sq = 555, mSQ = mx/sq+5;
int N, L, X[mx], mxB, reset; vector<int> blk[mSQ]; vector<ar2> dp[mSQ];
map<int, int> mp;
void calc(int b){
int sz = blk[b].size(); dp[b].clear(); dp[b].resize(sz);
for (int i = sz-1, p = sz-1; i >= 0; i--){
while (blk[b][p]-blk[b][i] > L) p--;
dp[b][i] = (p == sz-1) ? ar2{1, blk[b][p]+L} : ar2{dp[b][p+1][0]+1, dp[b][p+1][1]};
}
}
void upd(int x, bool ins){
int b = 0; while (b < mxB and (blk[b].empty() or x > blk[b].back())) b++;
int p = lb(blk[b], x);
ins ? blk[b].insert(blk[b].begin()+p, x) : blk[b].erase(blk[b].begin()+p);
calc(b);
}
void build(){
multiset<int> pos; int b = 0; blk[0].clear();
for (int i = 0; i < N; i++) pos.insert(X[i]);
for (auto i : mp){
if (blk[b].size() >= sq) blk[++b].clear();
blk[b].push_back(i.first);
}for (int i = 0; i <= mxB; i++) calc(i);
}
int query(){
int ans = 0;
for (int b = 0, x = -1; b <= mxB; b++){
int p = lb(blk[b], x+1);
if (p != blk[b].size()) x = dp[b][p][1], ans += dp[b][p][0];
}return ans;
}
void init(int n, int l, int x[]){
N = n; L = l; mxB = (N-1)/sq;
for (int i = 0; i < n; i++) X[i] = x[i], mp[X[i]]++;
build();
}
int update(int i, int y){
if (++reset == sq) build(), reset = 0;
mp[X[i]]--;
if (!mp[X[i]]) upd(X[i], 0), mp.erase(X[i]);
if (!mp[y]) upd(y, 1);
mp[y]++;
X[i] = y; return query();
}
Compilation message
elephants.cpp: In function 'int query()':
elephants.cpp:37:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if (p != blk[b].size()) x = dp[b][p][1], ans += dp[b][p][0];
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
25 ms |
2364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
25 ms |
2364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
25 ms |
2364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |