This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "elephants.h"
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,sse4.1,popcnt,tune=native")
#include <bits/stdc++.h>
using namespace std;
const int maxn = 150170, C = 404;
int n, l, x[maxn], bl[maxn], to[maxn], sto[maxn], w[maxn];
#define INLINE inline __attribute__(( always_inline ))
vector<int> b[maxn/C];
INLINE int lower_bound(int X, int LBB = 0) {
int B = LBB, I = 0;
if(B > maxn/C || b[B].empty()) return n;
for(int z = 1<<10; z>>=1;) if(B+z < maxn/C && !b[B+z].empty() && x[b[B+z][0]] < X) B += z;
for(int z = 1<<10; z>>=1;) if(I+z < b[B].size() && x[b[B][I+z]] < X) I += z;
//cout << X << " // " << B << " " << I << '\n';
if(x[b[B][I]] < X && ++I == b[B].size()) I = 0, ++B;
//cout << X << " // " << B << " " << I << '\n';
//cout << "Final" << (b[B].empty() ? n : b[B][I]) << '\n';
return b[B].empty() ? n : b[B][I];
}
INLINE void update(int i) {
//cout << "Updated block " << i << "\n";
int f = 0;
for(auto v : b[i]) {
to[v] = lower_bound(x[v]+l+1, f);//, cout << v << " " << x[v]+l+1 << " " << to[v] << '\n';
f = bl[to[v]];
}
for(int v, _v = b[i].size(); _v--;) {
v = b[i][_v];
if(bl[to[v]] == bl[v])
sto[v] = sto[to[v]], w[v] = 1+w[to[v]];
else sto[v] = v, w[v] = 1;
}
}
INLINE void pop(int v) {
for(int i = 0; i+1 < b[bl[v]].size(); i++)
if(b[bl[v]][i] == v) swap(b[bl[v]][i], b[bl[v]][i+1]);
b[bl[v]].pop_back();
update(bl[v]);
}
INLINE void push(int i) {
bl[i] = min((n-1)/C, bl[lower_bound(x[i])]);
int bid = bl[i];
//for(auto &b : b[bid]) cout << x[b] << " "; cout << "F\n";
for(auto &b : b[bid])
if(x[b] > x[i]) swap(b, i);
b[bid].push_back(i);
//for(auto &b : b[bid]) cout << x[b] << " "; cout << "G\n";
update(bid);
}
INLINE void build() {
vector<int> ord;
ord.reserve(n);
for(int i = 0; i*C < n; i++)
for(auto j : b[i])
ord.push_back(j);
if(ord.empty())
for(int i = 0; i < n; i++) ord.push_back(i);
for(int i = 0; i*C < n; i++) {
b[i].clear();
b[i].reserve(2*C);
}
for(int i, pos = 0; pos < n; pos++) {
i = ord[pos];
bl[i] = pos/C;
b[bl[i]].push_back(i);
}
for(int i = 0; i*C < n; i++) update(i);
}
void init(int N, int L, int X[]) {
n = N;
l = L;
bl[n] = maxn;
for(int i = 0; i < n; i++) x[i] = X[i];
build();
}
INLINE int calc() {
int p = b[0][0], ans = 0, uwu = 0;
while(p != n) {
ans += w[p];
p = sto[p];
p = lower_bound(x[p]+l+1, bl[p]+1);
}
return ans;
}
int timer = 0;
int update(int i, int y) {
pop(i);
//for(int uwu = 0; uwu < 2; uwu++, cout << " | ")
// for(auto i : b[uwu]) cout << x[i] << " ";cout << ":\n";
//cout << "x_" << i << " := " << y << endl;
x[i] = y;
push(i);
//for(int uwu = 0; uwu < 2; uwu++, cout << " | ")
// for(auto i : b[uwu]) cout << x[i] << " ";cout << ":\n";
if(++timer == C-1) timer = 0, build();
return calc();
}
Compilation message (stderr)
elephants.cpp: In function 'int lower_bound(int, int)':
elephants.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int z = 1<<10; z>>=1;) if(I+z < b[B].size() && x[b[B][I+z]] < X) I += z;
~~~~^~~~~~~~~~~~~
elephants.cpp:16:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(x[b[B][I]] < X && ++I == b[B].size()) I = 0, ++B;
~~~~^~~~~~~~~~~~~~
elephants.cpp: In function 'void pop(int)':
elephants.cpp:36:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i+1 < b[bl[v]].size(); i++)
~~~~^~~~~~~~~~~~~~~~~
elephants.cpp: In function 'int calc()':
elephants.cpp:78:28: warning: unused variable 'uwu' [-Wunused-variable]
int p = b[0][0], ans = 0, uwu = 0;
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |