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;
int z;
int s[N * 18 * 18];
int sl[N * 18 * 18], sr[N * 18 * 18];
void ubdy(int tl, int tr, int l, int r, int posy)
{
if (l > r)
return;
if (tl == l && tr == r)
{
s[posy]++;
return;
}
int m = (tl + tr) / 2;
if (sl[posy] == 0)
sl[posy] = ++z;
if (sr[posy] == 0)
sr[posy] = ++z;
ubdy(tl, m, l, min(m, r), sl[posy]);
ubdy(m + 1, tr, max(m + 1, l), r, sr[posy]);
}
int r[N * 4];
void ubdx(int tl, int tr, int lx, int rx, int ly, int ry, int posx)
{
if (lx > rx)
return;
if (tl == lx && tr == rx)
{
if (r[posx] == 0)
r[posx] = ++z;
ubdy(1, n, ly, ry, r[posx]);
return;
}
int m = (tl + tr) / 2;
ubdx(tl, m, lx, min(m, rx), ly, ry, posx * 2);
ubdx(m + 1, tr, max(m + 1, lx), rx, ly, ry, posx * 2 + 1);
}
int qryy(int tl, int tr, int y, int posy)
{
if (posy == 0)
return 0;
if (tl == tr)
return s[posy];
int m = (tl + tr) / 2;
if (y <= m)
return qryy(tl, m, y, sl[posy]) + s[posy];
return qryy(m + 1, tr, y, sr[posy]) + s[posy];
}
int qryx(int tl, int tr, int x, int y, int posx)
{
if (tl == tr)
return qryy(1, n, y, r[posx]);
int m = (tl + tr) / 2;
if (x <= m)
return qryx(tl, m, x, y, posx * 2) + qryy(1, n, y, r[posx]);
return qryx(m + 1, tr, x, y, posx * 2 + 1) + qryy(1, n, y, r[posx]);
}
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;
}
for (int i = 0; i < n; ++i)
{
ubdx(1, n, b[i].fi.fi, b[i].fi.se, b[i].se.fi, b[i].se.se, 1);
}
}
int x;
void init(int N, std::vector<int> H)
{
n = N;
for (int i = 1; i <= n; ++i)
a[i] = H[i - 1];
x = 1;
while (x < n && a[x] < a[x + 1])
++x;
for (int i = x + 1; i <= n; ++i)
{
if (!(a[i] < a[i - 1]))
{
x = 0;
break;
}
}
}
int max_towers(int L, int R, int D)
{
++L;
++R;
if (x)
{
if (L < x && x < R)
{
if (a[L] <= a[x] - D && a[R] <= a[x] - D)
return 2;
}
return 1;
}
if (b.empty())
{
d = D;
pre();
}
return qryx(1, n, L, R, 1);
}
/*
7 1
10 20 30 60 50 30 10
0 4 1
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... |