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;
int n;
int treeN = 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, -1});
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);
}
int max_towers(int L, int R, int D)
{
vi dp(R + 1, 1);
for (int i = R - 2; i >= L; i--)
{
int lo = i + 2, hi = R;
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 [mx, mxIx] = query(1, 0, treeN - 1, i + 1, m - 1, 0);
auto [mn, mnIx] = query(1, 0, treeN - 1, mxIx + 1, m, 1);
if (mx - max(mn, h[i]) >= D)
{
lastIx = mnIx;
suc = 1;
if (lo == hi)
break;
hi = m;
}
else
{
lo = m + 1;
}
}
if (suc)
{
dp[i] = dp[lastIx] + 1;
}
}
return *max_element(ALL(dp));
}
# | 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... |