#include "towers.h"
#include <vector>
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 2e5 + 5;
int v[nmax];
namespace DSU {
int dsu[nmax], nxt[nmax];
int mxR[nmax];
void init(int l, int r) {
for(int i = l; i <= r; i++) dsu[i] = i, nxt[i] = i + 1;
return;
}
int f(int x) { return x == dsu[x]? x : dsu[x] = f(dsu[x]); }
void unite(int x, int y) {
x = f(x);
y = f(y);
if(x == y) return;
if(x > y) swap(x, y);
dsu[y] = x;
mxR[x] = max(mxR[x], mxR[y]);
nxt[x] = nxt[y];
return;
}
int coef(int i, const vector<int>& p) {
i = f(i);
return mxR[i] - max(v[p[i]], v[p[nxt[i]]]);
}
}
int timeout[nmax];
int dir[nmax];
void initglobals(signed L, signed R) {
++L;
++R;
vector<int> p;
for(int i = L; i <= R; i++) {
if((i == L || v[i] < v[i - 1]) && (i == R || v[i] < v[i + 1])) p.emplace_back(i), timeout[i] = 1e9 + 5;
else timeout[i] = 0;
}
DSU::init(0, sz(p));
set<pii> heap;
set<int> indemnizatii;
for(int i = 0; i < sz(p) - 1; i++) {
indemnizatii.emplace(i);
indemnizatii.emplace(i + 1);
int mx = v[p[i] + 1];
for(int j = p[i] + 2; j < p[i + 1]; j++)
mx = max(mx, v[j]);
DSU::mxR[i] = mx;
heap.emplace(mx - max(v[p[i]], v[p[i + 1]]), i);
}
auto get = [&](int p_) { return pii{DSU::coef(p_, p), p_}; };
while(!heap.empty()) {
auto [C, i] = *heap.begin();
heap.erase(heap.begin());
i = DSU::f(i);
int urm = DSU::nxt[i];
if(i != *indemnizatii.begin()) {
int ant = DSU::f(i - 1);
heap.erase(heap.find(get(ant)));
}
if(urm != *indemnizatii.rbegin())
heap.erase(heap.find(get(urm)));
if(v[p[i]] < v[p[urm]]) {
timeout[p[urm]] = C;
dir[p[urm]] = p[i];
DSU::unite(i, urm);
indemnizatii.erase(urm);
if(i != *indemnizatii.begin()) {
int ant = DSU::f(i - 1);
heap.insert(get(ant));
}
if(i != *indemnizatii.rbegin())
heap.insert(get(i));
}
else {
timeout[p[i]] = C;
dir[p[i]] = p[urm];
if(i != *indemnizatii.begin()) {
int ant = DSU::f(i - 1);
DSU::unite(ant, i);
heap.insert(get(ant));
}
if(urm != *indemnizatii.rbegin())
heap.insert(get(urm));
indemnizatii.erase(i);
}
}
}
template<typename T>
struct AINT {
struct Node {
vector<Node>::iterator l, r;
T inner;
};
using ns = vector<Node>::iterator;
vector<Node> aint;
int n;
ns nil = begin(aint), root = nil;
ns newnode(ns L, ns R, T val) { aint.emplace_back(L, R, val); return prev(end(aint)); }
ns newnode() { return newnode(nil, nil, T()); }
void init(int n_) {
n = n_;
aint.reserve(nmax * 32);
aint.resize(max(sz(aint), 2ll));
nil = begin(aint); nil -> l = nil -> r = nil; nil -> inner = T();
root = nil;
walk([&](T& a, int cl, int cr) {
if(cl != cr) return 1;
a = T();
return 0;
});
}
template<int cst = 0, class CB> ns walk(CB&& cb) { return root = (cst == 0? walk<cst>(cb, root, 1, n) : (walk<cst>(cb, root, 1, n), root)); }
template<int cst = 0, class CB> ns walk(CB&& cb, int l, int r) { return root = (cst == 0? walk<cst>(cb, root, l, r) : (walk<cst>(cb, root, l, r), root)); }
template<class CB> void const_walk(CB&& cb, ns start) { const_walk(cb, start, 1, n); }
template<class CB> void const_walk(CB&& cb, ns start, int l, int r) { walk<1>(cb, l, r, start, 1, n); }
template<int cst = 0, class CB> ns walk(CB&& cb, ns start, int l, int r) { return walk<cst>(cb, l, r, start, 1, n); }
template<int cst, class CB> ns walk(CB&& cb, int l, int r, ns node, int cl, int cr) {
if(cr < l || r < cl) return node;
auto a = node -> inner;
if(l <= cl && cr <= r && !cb(a, cl, cr)) {
if(cst) return node;
return newnode(node -> l, node -> r, a);
}
int mid = (cl + cr) >> 1;
auto L = walk<cst>(cb, l, r, node -> l, cl, mid);
auto R = walk<cst>(cb, l, r, node -> r, mid + 1, cr);
if(!cst) node = newnode();
node -> l = L, node -> r = R, node -> inner.pull(L -> inner, R -> inner);
return node;
}
};
struct Sum {
int val;
Sum(int a = 0): val(a) {;}
void pull(const Sum& L, const Sum& R) { val = L.val + R.val; }
};
struct mnidx {
int val;
mnidx(int a = 0): val(a) {;}
void pull(const mnidx& L, const mnidx& R) { val = v[L.val] < v[R.val]? L.val : R.val; }
};
struct mxval {
int val;
mxval(int a = 0): val(a) {;}
void pull(const mxval& L, const mxval& R) { val = max(L.val, R.val); }
};
map<int, AINT<Sum>::ns> sumroot;
map<int, AINT<mnidx>::ns> raiseroot, fallroot;
AINT<Sum> sum_aint;
AINT<mnidx> dir_aint;
AINT<mxval> max_aint;
void init(signed N, std::vector<signed> H) {
//cerr << N << ' ' << sz(H) << '\n';
v[0] = 1e9 + 2;
for(int i = 0; i < N; i++) v[i + 1] = H[i];
initglobals(0, N - 1);
max_aint.init(N);
max_aint.walk([&](auto &a, int cl, int cr) { if(cl != cr) return 1; a.val = v[cl]; return 0; });
vector<int> idx(N); iota(all(idx), 1);
sum_aint.init(N);
sort(all(idx), [&](int a, int b) { return timeout[a] > timeout[b]; });
for(auto x : idx)
sumroot[timeout[x]] = sum_aint.walk([&](auto& a, int cl, int cr) { a.val = 1; return 0;}, x, x);
//cerr << timeout[x] << '\t' << sum_aint.root -> inner.val << '\n';
dir_aint.init(N);
sort(all(idx), [&](int a, int b) { return dir[a] < dir[b]; });
raiseroot[0] = dir_aint.root;
for(auto x : idx)
raiseroot[dir[x]] = dir_aint.walk([&](auto& a, int cl, int cr) { a.val = cl * (timeout[cl] != 0); return 0; }, x, x);
for(int i = 1; i <= N; i++) if(raiseroot.count(i) == 0) raiseroot[i] = raiseroot[i - 1];
dir_aint.init(N);
sort(all(idx), [&](int a, int b) { return dir[a] > dir[b]; });
fallroot[N + 1] = dir_aint.root;
for(auto x : idx)
fallroot[dir[x]] = dir_aint.walk([&](auto& a, int cl, int cr) { a.val = cl * (timeout[cl] != 0); return 0; }, x, x);
for(int i = N; i > 0; i--) if(fallroot.count(i) == 0) fallroot[i] = fallroot[i + 1];
return;
}
int query_max(int l, int r) {
int mx = 0;
max_aint.const_walk([&](auto& a, int cl, int cr) { mx = max(mx, a.val); return 0;}, max_aint.root, l, r);
return mx;
}
signed max_towers(signed L, signed R, signed D) {
++L, ++R;
//cerr << L << ' ' << R << '\n';
if(sumroot.lower_bound(D) == sumroot.end()) {
int mx = query_max(L, R);
return mx - max(v[L], v[R]) >= D? 2 : 1;
}
auto forsum = sumroot.lower_bound(D) -> second;
int alltogether = 0;
sum_aint.const_walk([&](auto a, int cl, int cr) { alltogether += a.val; return 0; }, forsum, L, R);
if(alltogether == 0) {
int mnL = L;
dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mnL] > v[a.val]) mnL = a.val; return 0;}, raiseroot[L - 1], L , R);
int mnR = R;
dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mnR] > v[a.val]) mnR = a.val; return 0;}, fallroot[R + 1], L , R);
if(mnL > mnR) return 1;
return query_max(mnL, mnR) - max(v[mnL], v[mnR]) >= D? 2 : 1;
}
int l = L, r;
sum_aint.const_walk([&](auto &a, int cl, int cr) {
if(l < cl) return 0;
if(a.val == 0) { l = cr + 1; return 0; }
if(cl == cr) { l = cl; return 0; }
return 1; }, forsum, L, R);
int rez = alltogether;
if(L < l) {
int mn = L;
dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mn] > v[a.val]) mn = a.val; return 0;}, raiseroot[L - 1], L + 1, l - 1);
int mx = query_max(mn + 1, l);
if(mx - max(v[l], v[mn]) >= D) rez++;
}
int limit = alltogether - 1;
r = L;
sum_aint.const_walk([&](auto &a, int cl, int cr) {
if(r < cl) return 0;
if(a.val <= limit) { limit -= a.val; r = cr + 1; return 0; }
if(cl == cr) { r = cl; return 0; }
return 1; }, forsum, L, R);
if(r < R) {
int mn = R;
dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mn] > v[a.val]) mn = a.val; return 0;}, fallroot[R + 1], r + 1, R - 1);
int mx = query_max(r + 1, mn - 1);
if(mx - max(v[r], v[mn]) >= D) rez++;
}
//cerr << L << ' ' << l << ' ' << r << ' ' << R << '\n';
return rez;
}
#undef int
Compilation message
towers.cpp:122:7: error: need 'typename' before 'std::vector<AINT<T>::Node>::iterator' because 'std::vector<AINT<T>::Node>' is a dependent scope
122 | vector<Node>::iterator l, r;
| ^~~~~~~~~~~~
| typename
towers.cpp:125:15: error: need 'typename' before 'std::vector<AINT<T>::Node>::iterator' because 'std::vector<AINT<T>::Node>' is a dependent scope
125 | using ns = vector<Node>::iterator;
| ^~~~~~~~~~~~
| typename
towers.cpp:129:4: error: 'ns' does not name a type
129 | ns nil = begin(aint), root = nil;
| ^~
towers.cpp:131:4: error: 'ns' does not name a type
131 | ns newnode(ns L, ns R, T val) { aint.emplace_back(L, R, val); return prev(end(aint)); }
| ^~
towers.cpp:132:4: error: 'ns' does not name a type
132 | ns newnode() { return newnode(nil, nil, T()); }
| ^~
towers.cpp:147:36: error: 'ns' does not name a type
147 | template<int cst = 0, class CB> ns walk(CB&& cb) { return root = (cst == 0? walk<cst>(cb, root, 1, n) : (walk<cst>(cb, root, 1, n), root)); }
| ^~
towers.cpp:148:36: error: 'ns' does not name a type
148 | template<int cst = 0, class CB> ns walk(CB&& cb, int l, int r) { return root = (cst == 0? walk<cst>(cb, root, l, r) : (walk<cst>(cb, root, l, r), root)); }
| ^~
towers.cpp:149:48: error: 'ns' has not been declared
149 | template<class CB> void const_walk(CB&& cb, ns start) { const_walk(cb, start, 1, n); }
| ^~
towers.cpp:150:48: error: 'ns' has not been declared
150 | template<class CB> void const_walk(CB&& cb, ns start, int l, int r) { walk<1>(cb, l, r, start, 1, n); }
| ^~
towers.cpp:151:36: error: 'ns' does not name a type
151 | template<int cst = 0, class CB> ns walk(CB&& cb, ns start, int l, int r) { return walk<cst>(cb, l, r, start, 1, n); }
| ^~
towers.cpp:153:32: error: 'ns' does not name a type
153 | template<int cst, class CB> ns walk(CB&& cb, int l, int r, ns node, int cl, int cr) {
| ^~
towers.cpp: In member function 'void AINT<T>::init(ll)':
towers.cpp:138:7: error: 'nil' was not declared in this scope
138 | nil = begin(aint); nil -> l = nil -> r = nil; nil -> inner = T();
| ^~~
towers.cpp:139:7: error: 'root' was not declared in this scope
139 | root = nil;
| ^~~~
towers.cpp: In member function 'void AINT<T>::const_walk(CB&&, int, ll, ll)':
towers.cpp:150:74: error: 'walk' was not declared in this scope
150 | template<class CB> void const_walk(CB&& cb, ns start, int l, int r) { walk<1>(cb, l, r, start, 1, n); }
| ^~~~
towers.cpp: At global scope:
towers.cpp:187:21: error: 'ns' is not a member of 'AINT<Sum>'
187 | map<int, AINT<Sum>::ns> sumroot;
| ^~
towers.cpp:187:23: error: template argument 2 is invalid
187 | map<int, AINT<Sum>::ns> sumroot;
| ^
towers.cpp:187:23: error: template argument 4 is invalid
towers.cpp:188:23: error: 'ns' is not a member of 'AINT<mnidx>'
188 | map<int, AINT<mnidx>::ns> raiseroot, fallroot;
| ^~
towers.cpp:188:25: error: template argument 2 is invalid
188 | map<int, AINT<mnidx>::ns> raiseroot, fallroot;
| ^
towers.cpp:188:25: error: template argument 4 is invalid
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:200:13: error: 'struct AINT<mxval>' has no member named 'walk'
200 | max_aint.walk([&](auto &a, int cl, int cr) { if(cl != cr) return 1; a.val = v[cl]; return 0; });
| ^~~~
towers.cpp:207:14: error: invalid types 'int[ll {aka long long int}]' for array subscript
207 | sumroot[timeout[x]] = sum_aint.walk([&](auto& a, int cl, int cr) { a.val = 1; return 0;}, x, x);
| ^
towers.cpp:207:38: error: 'struct AINT<Sum>' has no member named 'walk'
207 | sumroot[timeout[x]] = sum_aint.walk([&](auto& a, int cl, int cr) { a.val = 1; return 0;}, x, x);
| ^~~~
towers.cpp:212:13: error: invalid types 'int[int]' for array subscript
212 | raiseroot[0] = dir_aint.root;
| ^
towers.cpp:212:28: error: 'struct AINT<mnidx>' has no member named 'root'
212 | raiseroot[0] = dir_aint.root;
| ^~~~
towers.cpp:214:16: error: invalid types 'int[ll {aka long long int}]' for array subscript
214 | raiseroot[dir[x]] = dir_aint.walk([&](auto& a, int cl, int cr) { a.val = cl * (timeout[cl] != 0); return 0; }, x, x);
| ^
towers.cpp:214:36: error: 'struct AINT<mnidx>' has no member named 'walk'
214 | raiseroot[dir[x]] = dir_aint.walk([&](auto& a, int cl, int cr) { a.val = cl * (timeout[cl] != 0); return 0; }, x, x);
| ^~~~
towers.cpp:215:45: error: request for member 'count' in 'raiseroot', which is of non-class type 'int'
215 | for(int i = 1; i <= N; i++) if(raiseroot.count(i) == 0) raiseroot[i] = raiseroot[i - 1];
| ^~~~~
towers.cpp:215:69: error: invalid types 'int[ll {aka long long int}]' for array subscript
215 | for(int i = 1; i <= N; i++) if(raiseroot.count(i) == 0) raiseroot[i] = raiseroot[i - 1];
| ^
towers.cpp:215:84: error: invalid types 'int[ll {aka long long int}]' for array subscript
215 | for(int i = 1; i <= N; i++) if(raiseroot.count(i) == 0) raiseroot[i] = raiseroot[i - 1];
| ^
towers.cpp:219:12: error: invalid types 'int[int]' for array subscript
219 | fallroot[N + 1] = dir_aint.root;
| ^
towers.cpp:219:31: error: 'struct AINT<mnidx>' has no member named 'root'
219 | fallroot[N + 1] = dir_aint.root;
| ^~~~
towers.cpp:221:15: error: invalid types 'int[ll {aka long long int}]' for array subscript
221 | fallroot[dir[x]] = dir_aint.walk([&](auto& a, int cl, int cr) { a.val = cl * (timeout[cl] != 0); return 0; }, x, x);
| ^
towers.cpp:221:35: error: 'struct AINT<mnidx>' has no member named 'walk'
221 | fallroot[dir[x]] = dir_aint.walk([&](auto& a, int cl, int cr) { a.val = cl * (timeout[cl] != 0); return 0; }, x, x);
| ^~~~
towers.cpp:222:43: error: request for member 'count' in 'fallroot', which is of non-class type 'int'
222 | for(int i = N; i > 0; i--) if(fallroot.count(i) == 0) fallroot[i] = fallroot[i + 1];
| ^~~~~
towers.cpp:222:66: error: invalid types 'int[ll {aka long long int}]' for array subscript
222 | for(int i = N; i > 0; i--) if(fallroot.count(i) == 0) fallroot[i] = fallroot[i + 1];
| ^
towers.cpp:222:80: error: invalid types 'int[ll {aka long long int}]' for array subscript
222 | for(int i = N; i > 0; i--) if(fallroot.count(i) == 0) fallroot[i] = fallroot[i + 1];
| ^
towers.cpp: In function 'll query_max(ll, ll)':
towers.cpp:230:97: error: 'struct AINT<mxval>' has no member named 'root'
230 | max_aint.const_walk([&](auto& a, int cl, int cr) { mx = max(mx, a.val); return 0;}, max_aint.root, l, r);
| ^~~~
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:237:15: error: request for member 'lower_bound' in 'sumroot', which is of non-class type 'int'
237 | if(sumroot.lower_bound(D) == sumroot.end()) {
| ^~~~~~~~~~~
towers.cpp:237:41: error: request for member 'end' in 'sumroot', which is of non-class type 'int'
237 | if(sumroot.lower_bound(D) == sumroot.end()) {
| ^~~
towers.cpp:242:26: error: request for member 'lower_bound' in 'sumroot', which is of non-class type 'int'
242 | auto forsum = sumroot.lower_bound(D) -> second;
| ^~~~~~~~~~~
towers.cpp:249:115: error: invalid types 'int[int]' for array subscript
249 | dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mnL] > v[a.val]) mnL = a.val; return 0;}, raiseroot[L - 1], L , R);
| ^
towers.cpp:251:114: error: invalid types 'int[int]' for array subscript
251 | dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mnR] > v[a.val]) mnR = a.val; return 0;}, fallroot[R + 1], L , R);
| ^
towers.cpp:269:113: error: invalid types 'int[int]' for array subscript
269 | dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mn] > v[a.val]) mn = a.val; return 0;}, raiseroot[L - 1], L + 1, l - 1);
| ^
towers.cpp:285:112: error: invalid types 'int[int]' for array subscript
285 | dir_aint.const_walk([&](mnidx& a, int cl, int cr) { if(v[mn] > v[a.val]) mn = a.val; return 0;}, fallroot[R + 1], r + 1, R - 1);
| ^
towers.cpp: In instantiation of 'void AINT<T>::init(ll) [with T = mxval; ll = long long int]':
towers.cpp:199:19: required from here
towers.cpp:140:11: error: 'walk' was not declared in this scope
140 | walk([&](T& a, int cl, int cr) {
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
141 | if(cl != cr) return 1;
| ~~~~~~~~~~~~~~~~~~~~~~
142 | a = T();
| ~~~~~~~~
143 | return 0;
| ~~~~~~~~~
144 | });
| ~~
towers.cpp: In instantiation of 'void AINT<T>::init(ll) [with T = Sum; ll = long long int]':
towers.cpp:204:19: required from here
towers.cpp:140:11: error: 'walk' was not declared in this scope
towers.cpp: In instantiation of 'void AINT<T>::init(ll) [with T = mnidx; ll = long long int]':
towers.cpp:210:19: required from here
towers.cpp:140:11: error: 'walk' was not declared in this scope