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 "towers.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T1, typename T2>
using indexed_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using indexed_set = indexed_map<T, null_type>;
#define loop(x, i) for (int i = 0; i < (x); i++)
#define loop1(x, i) for (int i = 1; i <= (x); i++)
#define rev(x, i) for (int i = (int)(x) - 1; i >= 0; i--)
#define itloop(x) for (auto it = begin(x); x != end(x); it++)
#define itrev(x) for (auto it = rbegin(x); x != rend(x); it++)
#define INF ((int64_t)(4e18 + 1))
#define INF32 ((int32_t)(2e9 + 1))
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define removeIn(x, l) l.erase(find(ALL(l), x))
#define pb push_back
#define sz(x) (int)(x).size()
#define F first
#define S second
#define var const auto &
#define foreach(l) for (var e : l)
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vii> vvii;
typedef vector<viii> vviii;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
typedef vector<si> vsi;
typedef vector<sii> vsii;
typedef vector<vsi> vvsi;
typedef vector<string> vstr;
typedef vector<vector<string>> vvstr;
typedef vector<bool> vb;
typedef vector<vb> vvb;
vi h;
vii minTree, maxTree;
vi dp, maxDP;
vvi p;
int n;
int treeN = 1;
int lastD = -1;
void init(int N, vi H)
{
h = H;
n = N;
while (treeN < N)
treeN *= 2;
minTree = vii(treeN * 2, {1e9, 1e9});
maxTree = vii(treeN * 2, {0, -1e9});
loop(n, i)
{
minTree[treeN + i] = {H[i], i};
maxTree[treeN + i] = {H[i], -i};
}
rev(treeN, i)
{
minTree[i] = min(minTree[2 * i + 1], minTree[2 * i]);
maxTree[i] = max(maxTree[2 * i + 1], maxTree[2 * i]);
}
}
ii combine(ii a, ii b, bool minOp)
{
if (minOp)
return min(a, b);
return max(a, b);
}
// v, ix
ii query(int i, int l, int r, int ql, int qr, bool minOp)
{
if (r < ql || qr < l)
return {1e9 * minOp, 1e9 * minOp};
int m = l + (r - l) / 2;
if (ql <= l && r <= qr)
{
if (minOp)
return minTree[i];
return maxTree[i];
}
return combine(query(2 * i, l, m, ql, qr, minOp),
query(2 * i + 1, m + 1, r, ql, qr, minOp), minOp);
}
void calcDP(int D)
{
lastD = D;
dp = maxDP = vi(n + 1, 1);
p = vvi(20, vi(n + 1));
for (int i = n - 2; i >= 0; i--)
{
int lo = i + 2, hi = n;
bool suc = 0;
int lastIx = -1;
while (hi >= lo)
{
int m = lo + (hi - lo) / 2;
if (i + 1 > m - 1)
{
lo = m + 1;
continue;
}
auto [mn, mnIx] = query(1, 0, treeN - 1, i + 2, m, 1);
auto [mx, mxIx] = query(1, 0, treeN - 1, i + 1, mnIx - 1, 0);
mxIx *= -1;
if (mx - max(mn, h[i]) >= D)
{
lastIx = mnIx;
suc = 1;
if (lo == hi)
break;
hi = m;
}
else
{
lo = m + 1;
}
}
if (suc)
{
p[0][i] = lastIx;
dp[i] = maxDP[lastIx] + 1;
}
else
p[0][i] = i;
maxDP[i] = max(dp[i], maxDP[i + 1]);
}
loop1(19, x)
{
loop(n, i)
{
p[x][i] = p[x - 1][p[x - 1][i]];
}
}
}
int getParent(int i, int k)
{
loop(20, x)
{
if ((1 << x) & k)
{
i = p[x][i];
}
}
return i;
}
int max_towers(int L, int R, int D)
{
if (lastD != D)
{
calcDP(D);
}
int lo = 0, hi = maxDP[L] - 1;
while (hi > lo)
{
int m = lo + (hi - lo + 1) / 2;
int j = getParent(L, m);
if (j == R)
{
lo = hi = m;
}
if (j > R)
{
hi = m - 1;
}
else
lo = m;
}
return maxDP[L] - (maxDP[getParent(L, lo)] - 1);
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |