#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int root = 0;
int par[N];
int L[N], R[N], mn[N];
struct ST {
vector<pair<int,int>> t;
int n;
ST(){}
ST(vector<int>&h) {
n = h.size();
t.resize(2*n);
for(int i = 0;i<n;i++)t[i+n] = {h[i], i};
for(int i = n-1;i>0;i--)t[i] = max(t[2*i], t[2*i+1]);
}
int query(int l, int r) {
l+=n,r+=n+1;
pair<int,int> ans = {-1, -1};
for(;l<r;l>>=1,r>>=1) {
if(l&1)ans = max(ans, t[l++]);
if(r&1)ans = max(ans, t[--r]);
}
return ans.second;
}
};
struct ST2 {
vector<int> t;
int n;
ST2(){}
ST2(vector<int>&h) {
n = h.size();
t.resize(2*n);
for(int i = 0;i<n;i++)t[i+n] = h[i];
for(int i = n-1;i>0;i--)t[i] = min(t[2*i], t[2*i+1]);
}
int query(int l, int r) {
l+=n,r+=n+1;
int ans = 2e9;
for(;l<r;l>>=1,r>>=1) {
if(l&1)ans = min(ans, t[l++]);
if(r&1)ans = min(ans, t[--r]);
}
return ans;
}
};
struct ST3 {
vector<vector<int>> t;
int n;
ST3(){}
ST3(vector<vector<int>>&h) {
n = h.size();
t.resize(2*n);
for(int i = 0;i<n;i++){
t[i+n]=h[i];
sort(t[i+n].begin(), t[i+n].end());
}
for(int i = n-1;i>0;i--){
merge(t[2*i].begin(), t[2*i].end(), t[2*i+1].begin(), t[2*i+1].end(), back_inserter(t[i]));
}
}
int query(int l, int r, int key) {
l+=n,r+=n+1;
int ans=0;
for(;l<r;l>>=1,r>>=1) {
if(l&1) {
ans+=upper_bound(t[l].begin(), t[l].end(), key) - t[l].begin();
l++;
}
if(r&1) {
--r;
ans+=upper_bound(t[r].begin(), t[r].end(), key) - t[r].begin();
}
}
return ans;
}
};
vector<int> H;
ST st;
ST2 st2;
ST3 st3;
int pref[N+1];
int build(int l, int r) {
if(r < l)return -1;
int p = st.query(l, r);
L[p] = build(l, p-1);
R[p] = build(p+1, r);
mn[p] = H[p];
if(L[p]!=-1)mn[p] = min(mn[p], mn[L[p]]);
if(R[p]!=-1)mn[p] = min(mn[p], mn[R[p]]);
return p;
}
void init(int n, std::vector<int> h) {
st = ST(h);
st2 = ST2(h);
H = h;
root = build(0, n-1);
for(int i = 0;i<n;i++) {
bool check=1;
if(i and H[i-1] < H[i])check=false;
if(i+1 < n and H[i+1] < H[i])check=false;
if(i)pref[i] =pref[i-1];
pref[i]+=check;
}
}
int query(int v, int l, int r, int MX, int D) {
if(l > r)return 0;
if(v == -1)return 0;
if(v > r) {
return query(L[v], l, r, MX, D);
}
if(v < l)
return query(R[v], l, r, MX, D);
int ans = 0;
if(st2.query(l, r) <= MX)ans=1;
ans = max(ans, query(L[v], l, v-1, H[v]-D, D)+query(R[v], v+1, r, H[v]-D, D));
return ans;
}
int query2(int v, int l, int r) {
if(l > r)return 0;
if(v == -1)return 0;
if(v>r)return query2(L[v], l, r);
if(v<l)return query2(R[v], l, r);
int ans=0;
if(L[v] < l and (R[v] == -1 or R[v] > r))return 1;
ans+=query2(L[v], l, r);
ans+=query2(R[v], l, r);
return ans;
}
int max_towers(int l, int r, int D) {
if(D == 1) {
if(l == r)return 1;
if(l + 1 == r)return 1;
int ans = pref[r-1]-pref[l];
if(r and H[r-1] > H[r])ans++;
if(l+1<H.size() and H[l+1] > H[l])ans++;
return ans;
}
return query(root, l, r, 2e9, D);
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:140:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | if(l+1<H.size() and H[l+1] > H[l])ans++;
| ~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4002 ms |
6976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
372 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 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 |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
372 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 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 |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
14 ms |
3672 KB |
Output is correct |
37 |
Correct |
14 ms |
5464 KB |
Output is correct |
38 |
Correct |
14 ms |
5464 KB |
Output is correct |
39 |
Correct |
13 ms |
5464 KB |
Output is correct |
40 |
Correct |
12 ms |
5320 KB |
Output is correct |
41 |
Correct |
14 ms |
5464 KB |
Output is correct |
42 |
Correct |
13 ms |
5464 KB |
Output is correct |
43 |
Correct |
25 ms |
11576 KB |
Output is correct |
44 |
Correct |
22 ms |
11608 KB |
Output is correct |
45 |
Correct |
21 ms |
9920 KB |
Output is correct |
46 |
Correct |
19 ms |
9444 KB |
Output is correct |
47 |
Correct |
13 ms |
5464 KB |
Output is correct |
48 |
Correct |
13 ms |
5452 KB |
Output is correct |
49 |
Correct |
12 ms |
5400 KB |
Output is correct |
50 |
Correct |
23 ms |
11600 KB |
Output is correct |
51 |
Correct |
31 ms |
11604 KB |
Output is correct |
52 |
Correct |
21 ms |
5456 KB |
Output is correct |
53 |
Correct |
13 ms |
5464 KB |
Output is correct |
54 |
Correct |
12 ms |
5464 KB |
Output is correct |
55 |
Correct |
27 ms |
11592 KB |
Output is correct |
56 |
Correct |
22 ms |
11096 KB |
Output is correct |
57 |
Correct |
15 ms |
5208 KB |
Output is correct |
58 |
Correct |
23 ms |
5300 KB |
Output is correct |
59 |
Correct |
15 ms |
5464 KB |
Output is correct |
60 |
Correct |
13 ms |
5464 KB |
Output is correct |
61 |
Correct |
14 ms |
5464 KB |
Output is correct |
62 |
Correct |
15 ms |
5464 KB |
Output is correct |
63 |
Correct |
20 ms |
5464 KB |
Output is correct |
64 |
Correct |
30 ms |
12932 KB |
Output is correct |
65 |
Correct |
28 ms |
12752 KB |
Output is correct |
66 |
Correct |
29 ms |
9672 KB |
Output is correct |
67 |
Correct |
29 ms |
12496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
491 ms |
5464 KB |
Output is correct |
2 |
Correct |
588 ms |
5464 KB |
Output is correct |
3 |
Correct |
639 ms |
5464 KB |
Output is correct |
4 |
Correct |
679 ms |
5464 KB |
Output is correct |
5 |
Correct |
689 ms |
5452 KB |
Output is correct |
6 |
Correct |
607 ms |
5464 KB |
Output is correct |
7 |
Correct |
632 ms |
5460 KB |
Output is correct |
8 |
Correct |
604 ms |
11856 KB |
Output is correct |
9 |
Correct |
690 ms |
11776 KB |
Output is correct |
10 |
Correct |
593 ms |
10072 KB |
Output is correct |
11 |
Correct |
667 ms |
10072 KB |
Output is correct |
12 |
Correct |
645 ms |
11608 KB |
Output is correct |
13 |
Correct |
627 ms |
11600 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
15 ms |
5464 KB |
Output is correct |
18 |
Correct |
12 ms |
5360 KB |
Output is correct |
19 |
Correct |
13 ms |
5464 KB |
Output is correct |
20 |
Correct |
22 ms |
11608 KB |
Output is correct |
21 |
Correct |
30 ms |
11596 KB |
Output is correct |
22 |
Correct |
13 ms |
5464 KB |
Output is correct |
23 |
Correct |
19 ms |
5452 KB |
Output is correct |
24 |
Correct |
12 ms |
5464 KB |
Output is correct |
25 |
Correct |
24 ms |
11592 KB |
Output is correct |
26 |
Correct |
20 ms |
11096 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 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 |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4038 ms |
1624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
372 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 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 |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
14 ms |
3672 KB |
Output is correct |
37 |
Correct |
14 ms |
5464 KB |
Output is correct |
38 |
Correct |
14 ms |
5464 KB |
Output is correct |
39 |
Correct |
13 ms |
5464 KB |
Output is correct |
40 |
Correct |
12 ms |
5320 KB |
Output is correct |
41 |
Correct |
14 ms |
5464 KB |
Output is correct |
42 |
Correct |
13 ms |
5464 KB |
Output is correct |
43 |
Correct |
25 ms |
11576 KB |
Output is correct |
44 |
Correct |
22 ms |
11608 KB |
Output is correct |
45 |
Correct |
21 ms |
9920 KB |
Output is correct |
46 |
Correct |
19 ms |
9444 KB |
Output is correct |
47 |
Correct |
13 ms |
5464 KB |
Output is correct |
48 |
Correct |
13 ms |
5452 KB |
Output is correct |
49 |
Correct |
12 ms |
5400 KB |
Output is correct |
50 |
Correct |
23 ms |
11600 KB |
Output is correct |
51 |
Correct |
31 ms |
11604 KB |
Output is correct |
52 |
Correct |
21 ms |
5456 KB |
Output is correct |
53 |
Correct |
13 ms |
5464 KB |
Output is correct |
54 |
Correct |
12 ms |
5464 KB |
Output is correct |
55 |
Correct |
27 ms |
11592 KB |
Output is correct |
56 |
Correct |
22 ms |
11096 KB |
Output is correct |
57 |
Correct |
15 ms |
5208 KB |
Output is correct |
58 |
Correct |
23 ms |
5300 KB |
Output is correct |
59 |
Correct |
15 ms |
5464 KB |
Output is correct |
60 |
Correct |
13 ms |
5464 KB |
Output is correct |
61 |
Correct |
14 ms |
5464 KB |
Output is correct |
62 |
Correct |
15 ms |
5464 KB |
Output is correct |
63 |
Correct |
20 ms |
5464 KB |
Output is correct |
64 |
Correct |
30 ms |
12932 KB |
Output is correct |
65 |
Correct |
28 ms |
12752 KB |
Output is correct |
66 |
Correct |
29 ms |
9672 KB |
Output is correct |
67 |
Correct |
29 ms |
12496 KB |
Output is correct |
68 |
Correct |
491 ms |
5464 KB |
Output is correct |
69 |
Correct |
588 ms |
5464 KB |
Output is correct |
70 |
Correct |
639 ms |
5464 KB |
Output is correct |
71 |
Correct |
679 ms |
5464 KB |
Output is correct |
72 |
Correct |
689 ms |
5452 KB |
Output is correct |
73 |
Correct |
607 ms |
5464 KB |
Output is correct |
74 |
Correct |
632 ms |
5460 KB |
Output is correct |
75 |
Correct |
604 ms |
11856 KB |
Output is correct |
76 |
Correct |
690 ms |
11776 KB |
Output is correct |
77 |
Correct |
593 ms |
10072 KB |
Output is correct |
78 |
Correct |
667 ms |
10072 KB |
Output is correct |
79 |
Correct |
645 ms |
11608 KB |
Output is correct |
80 |
Correct |
627 ms |
11600 KB |
Output is correct |
81 |
Correct |
1 ms |
344 KB |
Output is correct |
82 |
Correct |
1 ms |
600 KB |
Output is correct |
83 |
Correct |
1 ms |
600 KB |
Output is correct |
84 |
Correct |
15 ms |
5464 KB |
Output is correct |
85 |
Correct |
12 ms |
5360 KB |
Output is correct |
86 |
Correct |
13 ms |
5464 KB |
Output is correct |
87 |
Correct |
22 ms |
11608 KB |
Output is correct |
88 |
Correct |
30 ms |
11596 KB |
Output is correct |
89 |
Correct |
13 ms |
5464 KB |
Output is correct |
90 |
Correct |
19 ms |
5452 KB |
Output is correct |
91 |
Correct |
12 ms |
5464 KB |
Output is correct |
92 |
Correct |
24 ms |
11592 KB |
Output is correct |
93 |
Correct |
20 ms |
11096 KB |
Output is correct |
94 |
Correct |
1 ms |
344 KB |
Output is correct |
95 |
Correct |
1 ms |
344 KB |
Output is correct |
96 |
Correct |
0 ms |
344 KB |
Output is correct |
97 |
Correct |
1 ms |
600 KB |
Output is correct |
98 |
Correct |
1 ms |
600 KB |
Output is correct |
99 |
Correct |
1 ms |
344 KB |
Output is correct |
100 |
Correct |
1 ms |
344 KB |
Output is correct |
101 |
Correct |
1 ms |
344 KB |
Output is correct |
102 |
Correct |
1 ms |
600 KB |
Output is correct |
103 |
Correct |
1 ms |
600 KB |
Output is correct |
104 |
Execution timed out |
4034 ms |
4952 KB |
Time limit exceeded |
105 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4002 ms |
6976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |