#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]--;
}
b.assign(n, m_p(m_p(0, 0), m_p(0, 0)));
s.clear();
for (int i = 1; i <= n; ++i)
{
while (!s.empty() && (ur[s.back()] < ul[i] || ul[s.back()] < ul[i]))
s.pop_back();
if (s.empty() || s.back() < ul[i])
b[i - 1].fi.fi = 1;
else
b[i - 1].fi.fi = s.back() + 1;
b[i - 1].fi.se = i;
s.push_back(i);
}
s.clear();
for (int i = n; i >= 1; --i)
{
while (!s.empty() && (ur[i] < ul[s.back()] || ul[s.back()] < ul[i] || m_p(ul[i], ur[i]) == m_p(ul[s.back()], ur[s.back()])))
s.pop_back();
if (s.empty() || ur[i] < s.back())
b[i - 1].se.se = n;
else
b[i - 1].se.se = s.back() - 1;
b[i - 1].se.fi = i;
s.push_back(i);
}
}
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 |
1 |
Execution timed out |
4003 ms |
2616 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
260 KB |
1st lines differ - on the 1st token, expected: '13', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
260 KB |
1st lines differ - on the 1st token, expected: '13', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4088 ms |
3404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2933 ms |
1072 KB |
1st lines differ - on the 1st token, expected: '7197', found: '7064' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
260 KB |
1st lines differ - on the 1st token, expected: '13', found: '12' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4003 ms |
2616 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |