#include "elephants.h"
#include "bits/stdc++.h"
using namespace std;
int n;
int a[150010];
int len;
const int magic = 50005;
vector <int> v[1000];
vector <int> dp[1000];
vector <int> nxt[1000];
int bucket[150010];
int tot;
int counter;
bool cmp(int x, int y) {
return a[x] < a[y];
}
int search(int b, int e, int block, int value) {
if(b == e) {
return a[v[block][b]] <= value ? b : -1;
}
int m = (b + e + 1) >> 1;
if(a[v[block][m]] <= value) return search(m, e, block, value);
else return search(b, m-1, block, value);
}
void get_done(int b) {
if(v[b].empty()) return ;
int sz = v[b].size();
nxt[b].resize(sz);
dp[b].resize(sz);
for(int i = sz-1; i >= 0; i--) {
int id = search(0, sz-1, b, a[v[b][i]] + len);
if(id == sz - 1) {
dp[b][i] = 1;
nxt[b][i] = a[v[b][i]] + len;
} else {
dp[b][i] = dp[b][id + 1] + 1;
nxt[b][i] = nxt[b][id + 1];
}
}
}
void init(int N, int L, int X[])
{
tot = (N-1) / magic;
len = L;
n = N;
for(int i = 0; i < n; i++) {
a[i] = X[i];
}
for(int i = 0; i < n; i++) {
v[i / magic].push_back(i);
bucket[i] = i / magic;
}
for(int i = 0; i <= tot; i++) {
get_done(i);
}
counter = 0;
}
int query() {
int ans = 0;
int cover = -1;
for(int i = 0; i <= tot; i++) {
if(v[i].empty()) continue;
int sz = v[i].size();
int u = search(0, sz-1, i, cover);
if(u < sz-1) {
ans += dp[i][u + 1];
cover = nxt[i][u + 1];
}
}
return ans;
}
void rebuild() {
for(int i = 0; i <= tot; i++) {
v[i].clear();
}
vector <int> pos;
for(int i = 0; i < n; i++) {
pos.push_back(i);
}
sort(pos.begin(), pos.end(), cmp);
for(int i = 0; i < n; i++) {
bucket[pos[i]] = i / magic;
v[i / magic].push_back(pos[i]);
}
}
int update(int i, int y)
{
++counter;
if(counter == magic) {
counter = 0;
rebuild();
}
int b1 = bucket[i];
int b2 = tot;
v[b1].erase(find(v[b1].begin(), v[b1].end(), i));
a[i] = y;
for(int x = 0; x <= tot; x++) {
if(v[x].empty()) continue;
if(a[i] <= a[v[x].back()]) {
b2 = x;
break;
}
}
bucket[i] = b2;
v[b2].push_back(i);
sort(v[b2].begin(), v[b2].end(), cmp);
get_done(b1);
get_done(b2);
return query();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18892 KB |
Output is correct |
2 |
Correct |
0 ms |
18892 KB |
Output is correct |
3 |
Correct |
0 ms |
18892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18892 KB |
Output is correct |
2 |
Correct |
0 ms |
18892 KB |
Output is correct |
3 |
Correct |
0 ms |
18892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
9000 ms |
19032 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
9000 ms |
19236 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
9000 ms |
20924 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |