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"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx,avx2,fma")
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1);
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< x << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
return out << '(' << a.first << ", " << a.second << ')'; }
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 1.5e5 + 7, SQ = 400;
ll a [N], pos [N], n, L, dp [SQ][3 * SQ + 3], dp2 [SQ][3 * SQ + 3], mxsz, idx [N];
vector <ll> block [SQ];
multiset <ll> st;
ll Find (ll x) {
if (st.empty() || x > *st.rbegin()) return ((n - 1) / SQ);
ll t;
for (t = 0; t < SQ && (block[t].empty() || block[t].back() < x); t++);
return t;
}
void Ins (ll id, ll x) {
block[id].push_back(x);
for (ll i = SZ(block[id]) - 2; i >= 0; i--) {
if (block[id][i] > block[id][i+1]) swap(block[id][i], block[id][i+1]);
else break;
}
}
void build (ll id) {
mxsz = max(mxsz, SZ(block[id]));
dp[id][SZ(block[id])] = 0;
for (ll l = SZ(block[id]), r = SZ(block[id]) - 1; l--;) {
while (block[id][r] - block[id][l] > L) r--;
dp[id][l] = dp[id][r + 1] + 1;
if (r + 1 == SZ(block[id])) dp2[id][l] = block[id][l] + L;
else dp2[id][l] = dp2[id][r + 1];
}
}
ll solve () {
ll ans = 0, cur = -1;
for (ll i = 0; i < SQ; i++) {
if (block[i].empty() || block[i].back() <= cur) continue;
ll ptr = upper_bound(All(block[i]), cur) - block[i].begin();
ans += dp[i][ptr];
cur = dp2[i][ptr];
}
return ans;
}
void rebuild () {
mxsz = 0;
iota(idx, idx + n, 0);
sort(idx, idx + n, [&](ll x, ll y) { return a[x] < a[y]; });
for (ll i = 0; i < SQ; i++) block[i].clear();
for (ll i = 0; i < n; i++) block[i / SQ].push_back(a[idx[i]]), pos[idx[i]] = i / SQ;
for (ll i = 0; i < SQ; i++) build(i);
}
void init (ll _n, ll _L, ll arr []) {
n = _n, L = _L;
for (ll i = 0; i < n; i++) a[i] = arr[i], pos[i] = i / SQ, block[i / SQ].push_back(a[i]), st.insert(a[i]);
for (ll i = 0; i < SQ; i++) build(i);
}
ll update (ll i, ll y) {
if (mxsz > 3 * SQ) rebuild();
block[pos[i]].erase(lower_bound(All(block[pos[i]]), a[i]));
build(pos[i]);
st.erase(st.find(a[i]));
a[i] = y;
pos[i] = Find(a[i]);
st.insert(a[i]);
Ins(pos[i], a[i]);
build(pos[i]);
return solve();
}
Compilation message (stderr)
elephants.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)':
elephants.cpp:44:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
44 | if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']'; }
| ^~
elephants.cpp:44:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
44 | if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']'; }
| ^~~~
# | 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... |