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>
using namespace std;
#define fi first
#define se second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 100005;
int n, d;
int a[N];
int ul[N], ur[N];
vector<pair<pair<int, int>, pair<int, int> > > b;
void pre()
{
b.clear();
vector<int> s;
for (int i = 1; i <= n; ++i)
{
while (!s.empty() && a[s.back()] <= a[i])
s.pop_back();
s.push_back(i);
int l = 0, r = sz(s) - 1;
ul[i] = 0;
while (l <= r)
{
int m = (l + r) / 2;
if (a[s[m]] - d >= a[i])
{
ul[i] = s[m];
l = m + 1;
}
else
{
r = m - 1;
}
}
}
s.clear();
for (int i = n; i >= 1; --i)
{
while (!s.empty() && a[s.back()] <= a[i])
s.pop_back();
s.push_back(i);
int l = 0, r = sz(s) - 1;
ur[i] = n + 1;
while (l <= r)
{
int m = (l + r) / 2;
if (a[s[m]] - d >= a[i])
{
ur[i] = s[m];
l = m + 1;
}
else
r = m - 1;
}
}
for (int i = 1; i <= n; ++i)
{
ul[i]++;
ur[i]--;
}
for (int i = 1; i <= n; ++i)
{
b.push_back(m_p(m_p(ur[i] - ul[i] + 1, i), m_p(ul[i], ur[i])));
}
sort(all(b));
set<int> t;
t.insert(0);
t.insert(n + 1);
for (int ii = 0; ii < n; ++ii)
{
int i = b[ii].fi.se;
t.insert(i);
auto it = t.find(i);
--it;
if (ur[*it] < ul[i])
b[ii].fi.fi = 1;
else
b[ii].fi.fi = *it + 1;
b[ii].fi.se = i;
++it;
++it;
b[ii].se.fi = i;
if (ur[i] < ul[*it])
b[ii].se.se = n;
else
b[ii].se.se = *it - 1;
}
}
void init(int N, std::vector<int> H)
{
n = N;
for (int i = 1; i <= n; ++i)
a[i] = H[i - 1];
}
int max_towers(int L, int R, int D)
{
++L;
++R;
//if (b.empty())
{
d = D;
pre();
}
int ans = 0;
for (int i = 0; i < n; ++i)
{
if (b[i].fi.fi <= L && L <= b[i].fi.se && b[i].se.fi <= R && R <= b[i].se.se)
++ans;
}
return ans;
}
/*
7 1
10 20 60 40 50 30 70
0 1 17
7 3
10 20 60 40 50 30 70
1 5 10
2 2 100
0 6 17
*/
# | 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... |