#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 |
1 |
Execution timed out |
4101 ms |
5484 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
41 ms |
5400 KB |
Output is correct |
37 |
Correct |
43 ms |
8036 KB |
Output is correct |
38 |
Correct |
49 ms |
8120 KB |
Output is correct |
39 |
Correct |
38 ms |
8120 KB |
Output is correct |
40 |
Correct |
45 ms |
8120 KB |
Output is correct |
41 |
Correct |
45 ms |
7996 KB |
Output is correct |
42 |
Correct |
43 ms |
8120 KB |
Output is correct |
43 |
Correct |
44 ms |
8484 KB |
Output is correct |
44 |
Correct |
37 ms |
8460 KB |
Output is correct |
45 |
Correct |
41 ms |
8368 KB |
Output is correct |
46 |
Correct |
40 ms |
8336 KB |
Output is correct |
47 |
Correct |
47 ms |
8088 KB |
Output is correct |
48 |
Correct |
40 ms |
8032 KB |
Output is correct |
49 |
Correct |
41 ms |
8032 KB |
Output is correct |
50 |
Correct |
38 ms |
8520 KB |
Output is correct |
51 |
Correct |
42 ms |
8460 KB |
Output is correct |
52 |
Correct |
46 ms |
8052 KB |
Output is correct |
53 |
Correct |
41 ms |
8028 KB |
Output is correct |
54 |
Correct |
43 ms |
8096 KB |
Output is correct |
55 |
Correct |
46 ms |
8420 KB |
Output is correct |
56 |
Correct |
37 ms |
8496 KB |
Output is correct |
57 |
Correct |
41 ms |
7876 KB |
Output is correct |
58 |
Correct |
43 ms |
8092 KB |
Output is correct |
59 |
Correct |
48 ms |
8108 KB |
Output is correct |
60 |
Correct |
42 ms |
8032 KB |
Output is correct |
61 |
Correct |
45 ms |
8092 KB |
Output is correct |
62 |
Correct |
48 ms |
8220 KB |
Output is correct |
63 |
Correct |
47 ms |
8100 KB |
Output is correct |
64 |
Correct |
41 ms |
8516 KB |
Output is correct |
65 |
Correct |
49 ms |
8504 KB |
Output is correct |
66 |
Correct |
48 ms |
8508 KB |
Output is correct |
67 |
Correct |
47 ms |
8384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4014 ms |
8000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4094 ms |
2120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
464 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
464 KB |
Output is correct |
32 |
Correct |
2 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
41 ms |
5400 KB |
Output is correct |
37 |
Correct |
43 ms |
8036 KB |
Output is correct |
38 |
Correct |
49 ms |
8120 KB |
Output is correct |
39 |
Correct |
38 ms |
8120 KB |
Output is correct |
40 |
Correct |
45 ms |
8120 KB |
Output is correct |
41 |
Correct |
45 ms |
7996 KB |
Output is correct |
42 |
Correct |
43 ms |
8120 KB |
Output is correct |
43 |
Correct |
44 ms |
8484 KB |
Output is correct |
44 |
Correct |
37 ms |
8460 KB |
Output is correct |
45 |
Correct |
41 ms |
8368 KB |
Output is correct |
46 |
Correct |
40 ms |
8336 KB |
Output is correct |
47 |
Correct |
47 ms |
8088 KB |
Output is correct |
48 |
Correct |
40 ms |
8032 KB |
Output is correct |
49 |
Correct |
41 ms |
8032 KB |
Output is correct |
50 |
Correct |
38 ms |
8520 KB |
Output is correct |
51 |
Correct |
42 ms |
8460 KB |
Output is correct |
52 |
Correct |
46 ms |
8052 KB |
Output is correct |
53 |
Correct |
41 ms |
8028 KB |
Output is correct |
54 |
Correct |
43 ms |
8096 KB |
Output is correct |
55 |
Correct |
46 ms |
8420 KB |
Output is correct |
56 |
Correct |
37 ms |
8496 KB |
Output is correct |
57 |
Correct |
41 ms |
7876 KB |
Output is correct |
58 |
Correct |
43 ms |
8092 KB |
Output is correct |
59 |
Correct |
48 ms |
8108 KB |
Output is correct |
60 |
Correct |
42 ms |
8032 KB |
Output is correct |
61 |
Correct |
45 ms |
8092 KB |
Output is correct |
62 |
Correct |
48 ms |
8220 KB |
Output is correct |
63 |
Correct |
47 ms |
8100 KB |
Output is correct |
64 |
Correct |
41 ms |
8516 KB |
Output is correct |
65 |
Correct |
49 ms |
8504 KB |
Output is correct |
66 |
Correct |
48 ms |
8508 KB |
Output is correct |
67 |
Correct |
47 ms |
8384 KB |
Output is correct |
68 |
Execution timed out |
4014 ms |
8000 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4101 ms |
5484 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |