#pragma GCC optimize("O3")
#include "elephants.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int oo = 1e9 + 9;
const int MAX = 150005;
const int BLOCK = 380;
int n, l;
vector<int> st;
int arr[MAX];
struct DATA{
vector<int> v;
vector<pii> dp;
void update(int a, bool b){
auto itr = lower_bound(v.begin(), v.end(), a);
if(b){
v.insert(itr, a);
}
else{
v.erase(itr);
}
}
void calc(){
dp.resize(v.size());
for(int i = v.size() - 1; i >= 0; --i){
int j = upper_bound(v.begin(), v.end(), v[i] + l) - v.begin();
if(j == v.size()){
dp[i] = {1, v[i] + l};
}
else{
dp[i] = dp[j];
dp[i].first += 1;
}
}
}
};
int blockCnt = 0;
DATA blocks[MAX / BLOCK + 1];
void build(){
vector<int> v(n);
for (int i = 0; i < n; i++)
{
v[i] = arr[i];
}
sort(v.begin(), v.end());
for(int i = 0; i < blockCnt; i++){
blocks[i].v.clear();
}
for(int i = 0; i < n; ++i){
blocks[i / BLOCK].v.push_back(v[i]);
}
for(int i = 0; i < blockCnt; i++){
sort(blocks[i].v.begin(), blocks[i].v.end());
blocks[i].calc();
}
}
int findBlock(int a){
int l = 0, r = blockCnt - 1;
int ans = 0;
while(l <= r){
int mid = (l + r) / 2;
int mn = oo;
if(!blocks[mid].v.empty()){
mn = blocks[mid].v[0];
}
if(a < mn){
r = mid - 1;
}
else{
ans = mid;
l = mid + 1;
}
}
return ans;
}
void init(int N, int L, int X[])
{
n = N;
l = L;
blockCnt = (N + BLOCK - 1) / BLOCK;
for(int i = 0; i < N; ++i){
arr[i] = X[i];
}
build();
}
int q = 0;
int update(int i, int y)
{
q += 1;
if(q >= BLOCK - 1){
arr[i] = y;
build();
q = 0;
}
else{
int a = findBlock(arr[i]);
int b = findBlock(y);
blocks[a].update(arr[i], 0);
blocks[b].update(y, 1);
arr[i] = y;
blocks[a].calc();
blocks[b].calc();
}
int p = -1;
int ans = 0;
for(int i = 0; i < blockCnt; ++i){
if(blocks[i].v.back() <= p) continue;
int idx = upper_bound(blocks[i].v.begin(), blocks[i].v.end(), p) - blocks[i].v.begin();
ans += blocks[i].dp[idx].first;
p = blocks[i].dp[idx].second;
}
return ans;
}
Compilation message
elephants.cpp: In member function 'void DATA::calc()':
elephants.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | if(j == v.size()){
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
707 ms |
1620 KB |
Output is correct |
8 |
Correct |
752 ms |
1868 KB |
Output is correct |
9 |
Correct |
805 ms |
2740 KB |
Output is correct |
10 |
Correct |
870 ms |
2576 KB |
Output is correct |
11 |
Correct |
836 ms |
2564 KB |
Output is correct |
12 |
Correct |
1970 ms |
3016 KB |
Output is correct |
13 |
Correct |
820 ms |
2568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
707 ms |
1620 KB |
Output is correct |
8 |
Correct |
752 ms |
1868 KB |
Output is correct |
9 |
Correct |
805 ms |
2740 KB |
Output is correct |
10 |
Correct |
870 ms |
2576 KB |
Output is correct |
11 |
Correct |
836 ms |
2564 KB |
Output is correct |
12 |
Correct |
1970 ms |
3016 KB |
Output is correct |
13 |
Correct |
820 ms |
2568 KB |
Output is correct |
14 |
Correct |
928 ms |
2272 KB |
Output is correct |
15 |
Correct |
1248 ms |
2308 KB |
Output is correct |
16 |
Correct |
3030 ms |
3252 KB |
Output is correct |
17 |
Correct |
3407 ms |
5560 KB |
Output is correct |
18 |
Correct |
3317 ms |
5500 KB |
Output is correct |
19 |
Correct |
2161 ms |
5088 KB |
Output is correct |
20 |
Correct |
3368 ms |
5804 KB |
Output is correct |
21 |
Correct |
3154 ms |
5532 KB |
Output is correct |
22 |
Correct |
1198 ms |
4516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
707 ms |
1620 KB |
Output is correct |
8 |
Correct |
752 ms |
1868 KB |
Output is correct |
9 |
Correct |
805 ms |
2740 KB |
Output is correct |
10 |
Correct |
870 ms |
2576 KB |
Output is correct |
11 |
Correct |
836 ms |
2564 KB |
Output is correct |
12 |
Correct |
1970 ms |
3016 KB |
Output is correct |
13 |
Correct |
820 ms |
2568 KB |
Output is correct |
14 |
Correct |
928 ms |
2272 KB |
Output is correct |
15 |
Correct |
1248 ms |
2308 KB |
Output is correct |
16 |
Correct |
3030 ms |
3252 KB |
Output is correct |
17 |
Correct |
3407 ms |
5560 KB |
Output is correct |
18 |
Correct |
3317 ms |
5500 KB |
Output is correct |
19 |
Correct |
2161 ms |
5088 KB |
Output is correct |
20 |
Correct |
3368 ms |
5804 KB |
Output is correct |
21 |
Correct |
3154 ms |
5532 KB |
Output is correct |
22 |
Correct |
1198 ms |
4516 KB |
Output is correct |
23 |
Correct |
7659 ms |
11328 KB |
Output is correct |
24 |
Correct |
8296 ms |
11632 KB |
Output is correct |
25 |
Correct |
4114 ms |
11572 KB |
Output is correct |
26 |
Correct |
7065 ms |
10880 KB |
Output is correct |
27 |
Correct |
8342 ms |
10724 KB |
Output is correct |
28 |
Correct |
3976 ms |
5200 KB |
Output is correct |
29 |
Correct |
3779 ms |
5188 KB |
Output is correct |
30 |
Correct |
3966 ms |
5196 KB |
Output is correct |
31 |
Correct |
3932 ms |
5192 KB |
Output is correct |
32 |
Correct |
5361 ms |
10312 KB |
Output is correct |
33 |
Correct |
5030 ms |
9636 KB |
Output is correct |
34 |
Correct |
5443 ms |
10520 KB |
Output is correct |
35 |
Correct |
3636 ms |
10812 KB |
Output is correct |
36 |
Correct |
1904 ms |
10280 KB |
Output is correct |
37 |
Execution timed out |
9068 ms |
12492 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |