#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
#define tol(bi) ((1LL)<<((int)(bi)))
constexpr int MAXN = 1e5;
constexpr int LOG = 20;
int st[MAXN*4][LOG], tin[MAXN], rev[MAXN*4];
vector<int> tree[MAXN];
map<int,int> hsh;
vector<int> H;
int query(int l, int r){
int ara = r-l+1;
int lg = log2(ara);
return min(st[l][lg],st[r-tol(lg)+1][lg]);
}
int lca(int a, int b){
if (tin[a]>tin[b]) swap(a,b);
return rev[query(tin[a],tin[b])];
}
int ind;
void dfs(int node){
st[ind][0]=ind;
rev[ind]=node;
tin[node]=ind++;
for (auto it : tree[node]){
dfs(it);
st[ind++][0]=tin[node];
}
}
void init(int N, std::vector<int> H) {
::H=H;
ind=0;
for (int i = 0; i < N; i++){
hsh[H[i]]=i;
st[i][0]=-H[i];
}
for (int j = 1; j < LOG; j++){
for (int i = 0; i < N; i++){
st[i][j]=-1;
if (st[i][j-1]==-1 || i+tol(j-1)>=N) continue;
st[i][j]=min(st[i][j-1],st[i+tol(j-1)][j-1]);
}
}
for (int i = 0; i < N; ++i)
{
tree[i].clear();
}
function<int(int,int)> partition = [&](int l, int r)->int{
if (l==r) return l;
int ele = hsh[-query(l,r)];
if (l<=ele-1) tree[ele].push_back(partition(l,ele-1));
if (ele+1<=r) tree[ele].push_back(partition(ele+1,r));
return ele;
};
int root = partition(0,N-1);
dfs(root);
for (int j = 1; j < LOG; j++){
for (int i = 0; i < ind; i++){
st[i][j]=-1;
if (st[i][j-1]==-1 || i+tol(j-1)>=ind) continue;
st[i][j]=min(st[i][j-1],st[i+tol(j-1)][j-1]);
}
}
}
int max_towers(int L, int R, int D) {
vector<int> leafs;
for (int i = L; i <= R; ++i)
{
bool isleaf=true;
for (auto it : tree[i]){
if (it>=L && it<=R) isleaf=false;
}
if (isleaf) leafs.push_back(i);
}
sort(leafs.begin(), leafs.end(), [&](int a, int b){
return tin[a]<tin[b];
});
if (leafs.size()==0) return 0;
int curn = leafs[0];
int sz = 1;
for (int i = 1; i < leafs.size(); i++){
int ara = lca(curn,leafs[i]);
if (max(H[leafs[i]],H[curn]) <= H[ara]-D){
sz++;
curn=leafs[i];
}
else if (H[curn]>H[leafs[i]]){
curn=leafs[i];
}
}
return sz;
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:83:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 1; i < leafs.size(); i++){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1310 ms |
24152 KB |
Output is correct |
2 |
Execution timed out |
4072 ms |
37456 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6744 KB |
Output is correct |
3 |
Correct |
2 ms |
6744 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
7000 KB |
Output is correct |
9 |
Correct |
1 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
7000 KB |
Output is correct |
11 |
Correct |
1 ms |
7000 KB |
Output is correct |
12 |
Correct |
1 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
7020 KB |
Output is correct |
15 |
Correct |
1 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
7000 KB |
Output is correct |
19 |
Correct |
1 ms |
7000 KB |
Output is correct |
20 |
Correct |
2 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
7000 KB |
Output is correct |
24 |
Correct |
2 ms |
7000 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6744 KB |
Output is correct |
27 |
Correct |
1 ms |
6824 KB |
Output is correct |
28 |
Correct |
1 ms |
6744 KB |
Output is correct |
29 |
Correct |
2 ms |
6744 KB |
Output is correct |
30 |
Correct |
1 ms |
6744 KB |
Output is correct |
31 |
Correct |
1 ms |
6744 KB |
Output is correct |
32 |
Correct |
1 ms |
7000 KB |
Output is correct |
33 |
Correct |
1 ms |
7000 KB |
Output is correct |
34 |
Correct |
1 ms |
6992 KB |
Output is correct |
35 |
Correct |
1 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6744 KB |
Output is correct |
3 |
Correct |
2 ms |
6744 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
7000 KB |
Output is correct |
9 |
Correct |
1 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
7000 KB |
Output is correct |
11 |
Correct |
1 ms |
7000 KB |
Output is correct |
12 |
Correct |
1 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
7020 KB |
Output is correct |
15 |
Correct |
1 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
7000 KB |
Output is correct |
19 |
Correct |
1 ms |
7000 KB |
Output is correct |
20 |
Correct |
2 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
7000 KB |
Output is correct |
24 |
Correct |
2 ms |
7000 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6744 KB |
Output is correct |
27 |
Correct |
1 ms |
6824 KB |
Output is correct |
28 |
Correct |
1 ms |
6744 KB |
Output is correct |
29 |
Correct |
2 ms |
6744 KB |
Output is correct |
30 |
Correct |
1 ms |
6744 KB |
Output is correct |
31 |
Correct |
1 ms |
6744 KB |
Output is correct |
32 |
Correct |
1 ms |
7000 KB |
Output is correct |
33 |
Correct |
1 ms |
7000 KB |
Output is correct |
34 |
Correct |
1 ms |
6992 KB |
Output is correct |
35 |
Correct |
1 ms |
7000 KB |
Output is correct |
36 |
Correct |
39 ms |
20056 KB |
Output is correct |
37 |
Correct |
62 ms |
29008 KB |
Output is correct |
38 |
Correct |
57 ms |
29024 KB |
Output is correct |
39 |
Correct |
53 ms |
28524 KB |
Output is correct |
40 |
Correct |
57 ms |
28536 KB |
Output is correct |
41 |
Correct |
64 ms |
28444 KB |
Output is correct |
42 |
Correct |
53 ms |
28496 KB |
Output is correct |
43 |
Correct |
45 ms |
37712 KB |
Output is correct |
44 |
Correct |
44 ms |
37712 KB |
Output is correct |
45 |
Correct |
41 ms |
35672 KB |
Output is correct |
46 |
Correct |
45 ms |
34864 KB |
Output is correct |
47 |
Correct |
61 ms |
29268 KB |
Output is correct |
48 |
Correct |
53 ms |
28524 KB |
Output is correct |
49 |
Correct |
53 ms |
28492 KB |
Output is correct |
50 |
Correct |
45 ms |
37716 KB |
Output is correct |
51 |
Correct |
44 ms |
37712 KB |
Output is correct |
52 |
Correct |
59 ms |
29032 KB |
Output is correct |
53 |
Correct |
55 ms |
28616 KB |
Output is correct |
54 |
Correct |
54 ms |
28500 KB |
Output is correct |
55 |
Correct |
44 ms |
37712 KB |
Output is correct |
56 |
Correct |
46 ms |
37200 KB |
Output is correct |
57 |
Correct |
56 ms |
28752 KB |
Output is correct |
58 |
Correct |
61 ms |
29008 KB |
Output is correct |
59 |
Correct |
58 ms |
29004 KB |
Output is correct |
60 |
Correct |
54 ms |
28452 KB |
Output is correct |
61 |
Correct |
54 ms |
28460 KB |
Output is correct |
62 |
Correct |
55 ms |
28616 KB |
Output is correct |
63 |
Correct |
54 ms |
28496 KB |
Output is correct |
64 |
Correct |
45 ms |
37712 KB |
Output is correct |
65 |
Correct |
43 ms |
37712 KB |
Output is correct |
66 |
Correct |
41 ms |
34760 KB |
Output is correct |
67 |
Correct |
46 ms |
37456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4059 ms |
28864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4043 ms |
10584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6744 KB |
Output is correct |
3 |
Correct |
2 ms |
6744 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
7000 KB |
Output is correct |
9 |
Correct |
1 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
7000 KB |
Output is correct |
11 |
Correct |
1 ms |
7000 KB |
Output is correct |
12 |
Correct |
1 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
14 |
Correct |
1 ms |
7020 KB |
Output is correct |
15 |
Correct |
1 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6744 KB |
Output is correct |
18 |
Correct |
2 ms |
7000 KB |
Output is correct |
19 |
Correct |
1 ms |
7000 KB |
Output is correct |
20 |
Correct |
2 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
7000 KB |
Output is correct |
24 |
Correct |
2 ms |
7000 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6744 KB |
Output is correct |
27 |
Correct |
1 ms |
6824 KB |
Output is correct |
28 |
Correct |
1 ms |
6744 KB |
Output is correct |
29 |
Correct |
2 ms |
6744 KB |
Output is correct |
30 |
Correct |
1 ms |
6744 KB |
Output is correct |
31 |
Correct |
1 ms |
6744 KB |
Output is correct |
32 |
Correct |
1 ms |
7000 KB |
Output is correct |
33 |
Correct |
1 ms |
7000 KB |
Output is correct |
34 |
Correct |
1 ms |
6992 KB |
Output is correct |
35 |
Correct |
1 ms |
7000 KB |
Output is correct |
36 |
Correct |
39 ms |
20056 KB |
Output is correct |
37 |
Correct |
62 ms |
29008 KB |
Output is correct |
38 |
Correct |
57 ms |
29024 KB |
Output is correct |
39 |
Correct |
53 ms |
28524 KB |
Output is correct |
40 |
Correct |
57 ms |
28536 KB |
Output is correct |
41 |
Correct |
64 ms |
28444 KB |
Output is correct |
42 |
Correct |
53 ms |
28496 KB |
Output is correct |
43 |
Correct |
45 ms |
37712 KB |
Output is correct |
44 |
Correct |
44 ms |
37712 KB |
Output is correct |
45 |
Correct |
41 ms |
35672 KB |
Output is correct |
46 |
Correct |
45 ms |
34864 KB |
Output is correct |
47 |
Correct |
61 ms |
29268 KB |
Output is correct |
48 |
Correct |
53 ms |
28524 KB |
Output is correct |
49 |
Correct |
53 ms |
28492 KB |
Output is correct |
50 |
Correct |
45 ms |
37716 KB |
Output is correct |
51 |
Correct |
44 ms |
37712 KB |
Output is correct |
52 |
Correct |
59 ms |
29032 KB |
Output is correct |
53 |
Correct |
55 ms |
28616 KB |
Output is correct |
54 |
Correct |
54 ms |
28500 KB |
Output is correct |
55 |
Correct |
44 ms |
37712 KB |
Output is correct |
56 |
Correct |
46 ms |
37200 KB |
Output is correct |
57 |
Correct |
56 ms |
28752 KB |
Output is correct |
58 |
Correct |
61 ms |
29008 KB |
Output is correct |
59 |
Correct |
58 ms |
29004 KB |
Output is correct |
60 |
Correct |
54 ms |
28452 KB |
Output is correct |
61 |
Correct |
54 ms |
28460 KB |
Output is correct |
62 |
Correct |
55 ms |
28616 KB |
Output is correct |
63 |
Correct |
54 ms |
28496 KB |
Output is correct |
64 |
Correct |
45 ms |
37712 KB |
Output is correct |
65 |
Correct |
43 ms |
37712 KB |
Output is correct |
66 |
Correct |
41 ms |
34760 KB |
Output is correct |
67 |
Correct |
46 ms |
37456 KB |
Output is correct |
68 |
Execution timed out |
4059 ms |
28864 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1310 ms |
24152 KB |
Output is correct |
2 |
Execution timed out |
4072 ms |
37456 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |