#include "towers.h"
#include <vector>
#include <numeric>
#include <algorithm>
#include <set>
struct SegmentTree
{
private:
std::vector<int> data;
struct SegTreeReference
{
private:
const int pos;
SegmentTree& t;
public:
SegTreeReference(int pos,SegmentTree& t) : pos(pos), t(t) {}
int operator=(int val)
{
t.assign(pos,val);
return val;
}
operator int() const {return t.data[t.data.size()/2+pos];}
};
public:
SegmentTree() : data(0) {}
SegmentTree(int num) : data(num*2,0) {}
SegmentTree(const std::vector<int>& vec) : data(vec.size()*2)
{
for(int i = 0; i < vec.size(); i++) data[vec.size()+i] = vec[i];
for(int i = vec.size()-1; i > 0; i--)
data[i] = std::max(data[2*i],data[2*i+1]);
}
void assign(int pos, int val)
{
data[pos] = val;
pos >>= 1;
for(;pos>0;pos>>=1)
data[pos] = std::max(data[2*pos],data[2*pos+1]);
}
SegTreeReference operator[](int pos)
{
return SegTreeReference(pos,*this);
}
int query(int l, int r)
{
l += data.size()/2;
r += data.size()/2;
int o = -(1<<31);
while(l < r)
{
if(l&1) o = std::max(o,data[l++]);
if(r&1) o = std::max(o,data[--r]);
l>>=1;
r>>=1;
}
return o;
}
void push_back(int v)
{
int N = data.size()/2;
data.push_back(data[N]);
data.push_back(v);
data[N] = std::max(data[2*N],data[2*N+1]);
}
};
std::vector<int> H;
int m;
void init(int N, std::vector<int> _H) {
H = std::move(_H);
}
int max_towers(int L, int R, int D) {
std::vector<int> idx_sorted(H.size());
SegmentTree st(H);
std::iota(idx_sorted.begin(),idx_sorted.end(),0);
std::sort(idx_sorted.begin(),idx_sorted.end(),[](auto a, auto b) {return H[a] < H[b];});
std::set<int> included;
for(auto i : idx_sorted)
{
if(i < L || i > R) continue;
auto p = included.lower_bound(i);
if(p!=included.begin())
{
auto l = *std::prev(p);
l++;
if(l==i) continue;
if (st.query(l,i)-D < H[i]) continue;
}
if(p!=included.end())
{
auto r = *p;
if(r==i+1) continue;
if(st.query(i+1,r)-D < H[i]) continue;
}
// bool inv = false;
// for(auto j : included)
// {
// auto l = std::min(i,j);
// l++;
// auto r = std::max(i,j);
// if(l==r) {inv = true;break;}
// auto c = st.query(l,r);
// if(H[i] > c-D || H[j] > c-D) {inv = true;break;}
// }
included.insert(i);
}
return included.size();
}
Compilation message
towers.cpp: In constructor 'SegmentTree::SegmentTree(const std::vector<int>&)':
towers.cpp:31:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0; i < vec.size(); i++) data[vec.size()+i] = vec[i];
| ~~^~~~~~~~~~~~
towers.cpp: In member function 'int SegmentTree::query(int, int)':
towers.cpp:51:17: warning: integer overflow in expression of type 'int' results in '-2147483648' [-Woverflow]
51 | int o = -(1<<31);
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4030 ms |
1492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
14 ms |
1960 KB |
Output is correct |
37 |
Correct |
22 ms |
2592 KB |
Output is correct |
38 |
Correct |
19 ms |
2356 KB |
Output is correct |
39 |
Correct |
21 ms |
2352 KB |
Output is correct |
40 |
Correct |
20 ms |
2392 KB |
Output is correct |
41 |
Correct |
32 ms |
3288 KB |
Output is correct |
42 |
Correct |
18 ms |
2384 KB |
Output is correct |
43 |
Correct |
15 ms |
2348 KB |
Output is correct |
44 |
Correct |
11 ms |
2356 KB |
Output is correct |
45 |
Correct |
13 ms |
2360 KB |
Output is correct |
46 |
Correct |
11 ms |
2352 KB |
Output is correct |
47 |
Correct |
24 ms |
2856 KB |
Output is correct |
48 |
Correct |
21 ms |
2860 KB |
Output is correct |
49 |
Correct |
19 ms |
2356 KB |
Output is correct |
50 |
Correct |
12 ms |
2392 KB |
Output is correct |
51 |
Correct |
14 ms |
2356 KB |
Output is correct |
52 |
Correct |
38 ms |
3736 KB |
Output is correct |
53 |
Correct |
41 ms |
4664 KB |
Output is correct |
54 |
Correct |
41 ms |
4632 KB |
Output is correct |
55 |
Correct |
18 ms |
2388 KB |
Output is correct |
56 |
Correct |
17 ms |
2364 KB |
Output is correct |
57 |
Correct |
32 ms |
2840 KB |
Output is correct |
58 |
Correct |
36 ms |
3152 KB |
Output is correct |
59 |
Correct |
45 ms |
3540 KB |
Output is correct |
60 |
Correct |
40 ms |
4656 KB |
Output is correct |
61 |
Correct |
41 ms |
4144 KB |
Output is correct |
62 |
Correct |
37 ms |
3708 KB |
Output is correct |
63 |
Correct |
41 ms |
4372 KB |
Output is correct |
64 |
Correct |
17 ms |
2136 KB |
Output is correct |
65 |
Correct |
18 ms |
2356 KB |
Output is correct |
66 |
Correct |
17 ms |
2356 KB |
Output is correct |
67 |
Correct |
22 ms |
2332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4033 ms |
3752 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4059 ms |
1252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
14 ms |
1960 KB |
Output is correct |
37 |
Correct |
22 ms |
2592 KB |
Output is correct |
38 |
Correct |
19 ms |
2356 KB |
Output is correct |
39 |
Correct |
21 ms |
2352 KB |
Output is correct |
40 |
Correct |
20 ms |
2392 KB |
Output is correct |
41 |
Correct |
32 ms |
3288 KB |
Output is correct |
42 |
Correct |
18 ms |
2384 KB |
Output is correct |
43 |
Correct |
15 ms |
2348 KB |
Output is correct |
44 |
Correct |
11 ms |
2356 KB |
Output is correct |
45 |
Correct |
13 ms |
2360 KB |
Output is correct |
46 |
Correct |
11 ms |
2352 KB |
Output is correct |
47 |
Correct |
24 ms |
2856 KB |
Output is correct |
48 |
Correct |
21 ms |
2860 KB |
Output is correct |
49 |
Correct |
19 ms |
2356 KB |
Output is correct |
50 |
Correct |
12 ms |
2392 KB |
Output is correct |
51 |
Correct |
14 ms |
2356 KB |
Output is correct |
52 |
Correct |
38 ms |
3736 KB |
Output is correct |
53 |
Correct |
41 ms |
4664 KB |
Output is correct |
54 |
Correct |
41 ms |
4632 KB |
Output is correct |
55 |
Correct |
18 ms |
2388 KB |
Output is correct |
56 |
Correct |
17 ms |
2364 KB |
Output is correct |
57 |
Correct |
32 ms |
2840 KB |
Output is correct |
58 |
Correct |
36 ms |
3152 KB |
Output is correct |
59 |
Correct |
45 ms |
3540 KB |
Output is correct |
60 |
Correct |
40 ms |
4656 KB |
Output is correct |
61 |
Correct |
41 ms |
4144 KB |
Output is correct |
62 |
Correct |
37 ms |
3708 KB |
Output is correct |
63 |
Correct |
41 ms |
4372 KB |
Output is correct |
64 |
Correct |
17 ms |
2136 KB |
Output is correct |
65 |
Correct |
18 ms |
2356 KB |
Output is correct |
66 |
Correct |
17 ms |
2356 KB |
Output is correct |
67 |
Correct |
22 ms |
2332 KB |
Output is correct |
68 |
Execution timed out |
4033 ms |
3752 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4030 ms |
1492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |