#include "elephants.h"
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
const int MAXN = 1e6 + 16;
const int MAX = 3e3 + 15;
int n, L, x[MAXN], block[MAXN], T, dp[MAX][MAX], d[MAX][MAX], q;
vector<int> blocks[MAX];
void init(int n_, int l_, int x_[]){
n = n_, L = l_;
T = sqrt(n) + 1;
for(int i = 0; i < n; i++)
x[i] = x_[i], blocks[i / T].push_back(x[i]);
}
void buildBlock(int b){
if(blocks[b].empty()) return;
int s = sz(blocks[b]);
for(int l = s - 1, r = s; l >= 0; l--){
int curX = blocks[b][l];
while(blocks[b][r - 1] - curX > L) r--;
if(r == s){
dp[b][l] = 1;
d[b][l] = curX + L;
}else{
dp[b][l] = dp[b][r] + 1;
d[b][l] = d[b][r];
}
}
}
void build(){
vector<int> aux;
for(int i = 0; i <= (n - 1) / T; i++){
for(auto& x : blocks[i]) aux.push_back(x);
blocks[i].clear();
}
for(int i = 0; i < n; i++)
blocks[i / T].push_back(aux[i]);
for(int i = 0; i <= (n - 1) / T; i++)
buildBlock(i);
}
int getBlock(int x){
for(int i = (n - 1) / T; i >= 0; i--)
if(blocks[i].size() && blocks[i][0] <= x) return i;
return 0;
}
int query(void){
int ans = 0;
for(int i = 0, r = -1; i <= (n - 1) / T; i++){
int idx = upper_bound(all(blocks[i]), r) - blocks[i].begin();
if(idx >= blocks[i].size()) continue;
ans += dp[i][idx];
r = d[i][idx];
}
return ans;
}
int update(int i, int y){
if(q++ % T == 0) build();
int b = getBlock(x[i]);
auto it = lower_bound(all(blocks[b]), x[i]);
blocks[b].erase(it);
buildBlock(b);
x[i] = y;
int nb = getBlock(y);
it = lower_bound(all(blocks[nb]), x[i]);
if(it == blocks[nb].end()) blocks[nb].push_back(x[i]);
else blocks[nb].insert(it, x[i]);
buildBlock(nb);
return query();
}
Compilation message
elephants.cpp: In function 'int query()':
elephants.cpp:69:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if(idx >= blocks[i].size()) continue;
| ~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 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 |
384 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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
224 ms |
2784 KB |
Output is correct |
8 |
Correct |
275 ms |
3184 KB |
Output is correct |
9 |
Correct |
353 ms |
4820 KB |
Output is correct |
10 |
Correct |
354 ms |
5740 KB |
Output is correct |
11 |
Correct |
349 ms |
5572 KB |
Output is correct |
12 |
Correct |
557 ms |
5908 KB |
Output is correct |
13 |
Correct |
372 ms |
5528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
224 ms |
2784 KB |
Output is correct |
8 |
Correct |
275 ms |
3184 KB |
Output is correct |
9 |
Correct |
353 ms |
4820 KB |
Output is correct |
10 |
Correct |
354 ms |
5740 KB |
Output is correct |
11 |
Correct |
349 ms |
5572 KB |
Output is correct |
12 |
Correct |
557 ms |
5908 KB |
Output is correct |
13 |
Correct |
372 ms |
5528 KB |
Output is correct |
14 |
Correct |
293 ms |
4844 KB |
Output is correct |
15 |
Correct |
457 ms |
4656 KB |
Output is correct |
16 |
Correct |
863 ms |
6372 KB |
Output is correct |
17 |
Correct |
951 ms |
7904 KB |
Output is correct |
18 |
Correct |
1078 ms |
7888 KB |
Output is correct |
19 |
Correct |
584 ms |
8048 KB |
Output is correct |
20 |
Correct |
944 ms |
7924 KB |
Output is correct |
21 |
Correct |
956 ms |
7868 KB |
Output is correct |
22 |
Correct |
613 ms |
7460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 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 |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
224 ms |
2784 KB |
Output is correct |
8 |
Correct |
275 ms |
3184 KB |
Output is correct |
9 |
Correct |
353 ms |
4820 KB |
Output is correct |
10 |
Correct |
354 ms |
5740 KB |
Output is correct |
11 |
Correct |
349 ms |
5572 KB |
Output is correct |
12 |
Correct |
557 ms |
5908 KB |
Output is correct |
13 |
Correct |
372 ms |
5528 KB |
Output is correct |
14 |
Correct |
293 ms |
4844 KB |
Output is correct |
15 |
Correct |
457 ms |
4656 KB |
Output is correct |
16 |
Correct |
863 ms |
6372 KB |
Output is correct |
17 |
Correct |
951 ms |
7904 KB |
Output is correct |
18 |
Correct |
1078 ms |
7888 KB |
Output is correct |
19 |
Correct |
584 ms |
8048 KB |
Output is correct |
20 |
Correct |
944 ms |
7924 KB |
Output is correct |
21 |
Correct |
956 ms |
7868 KB |
Output is correct |
22 |
Correct |
613 ms |
7460 KB |
Output is correct |
23 |
Correct |
2576 ms |
15176 KB |
Output is correct |
24 |
Correct |
2859 ms |
15208 KB |
Output is correct |
25 |
Correct |
2276 ms |
15088 KB |
Output is correct |
26 |
Correct |
2519 ms |
15132 KB |
Output is correct |
27 |
Correct |
2636 ms |
14940 KB |
Output is correct |
28 |
Correct |
487 ms |
5788 KB |
Output is correct |
29 |
Correct |
439 ms |
5708 KB |
Output is correct |
30 |
Correct |
476 ms |
5812 KB |
Output is correct |
31 |
Correct |
463 ms |
5792 KB |
Output is correct |
32 |
Correct |
2254 ms |
14636 KB |
Output is correct |
33 |
Correct |
1823 ms |
13900 KB |
Output is correct |
34 |
Correct |
2173 ms |
14864 KB |
Output is correct |
35 |
Correct |
1928 ms |
15240 KB |
Output is correct |
36 |
Correct |
1572 ms |
14624 KB |
Output is correct |
37 |
Correct |
2567 ms |
16176 KB |
Output is correct |
38 |
Correct |
2212 ms |
13820 KB |
Output is correct |
39 |
Correct |
1901 ms |
14852 KB |
Output is correct |
40 |
Correct |
2288 ms |
13924 KB |
Output is correct |
41 |
Correct |
3195 ms |
14820 KB |
Output is correct |
42 |
Correct |
3225 ms |
14888 KB |
Output is correct |