#include "towers.h"
#include <bits/stdc++.h>
#define pb push_back
#define left (h<<1),l ,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
#define s second
#define f first
using namespace std;
const int N = 100005;
int n,a[N],l,r,d,L[N],R[N],t[4*N];
bool b[N];
vector <pair<pair<int,int>,int> > val;
struct {
int maxr;
int maxl;
} T[4*N];
void upd(int h,int l,int r,int idx){
if (l == r){
T[h].maxl = l;
T[h].maxr = l;
return;
}
if (idx > (l+r)/2) upd(right,idx);
else upd(left,idx);
//maxl
if (b[T[h*2].maxl]) T[h].maxl = T[h*2].maxl;
else if (b[T[h*2 + 1].maxl]) T[h].maxl = T[h*2+1].maxl;
else T[h].maxl = 0;
//maxr
if (b[T[h*2+1].maxr]) T[h].maxr = T[h*2+1].maxr;
else if (b[T[h*2].maxr]) T[h].maxr = T[h*2].maxr;
else T[h].maxr = 0;
}
int getmaxl(int h,int l,int r,int L,int R){
if (r < L || R < l) return 0;
if (L <= l && r <= R) return T[h].maxl;
int x = getmaxl(left,L,R),y = getmaxl(right,L,R);
if (b[x]) return x;
if (b[y]) return y;
return 0;
}
int getmaxr(int h,int l,int r,int L,int R){
if (r < L || R < l) return 0;
if (L <= l && r <= R) return T[h].maxr;
int x = getmaxr(left,L,R),y = getmaxr(right,L,R);
if (b[y]) return y;
if (b[x]) return x;
return 0;
}
void find(){
for (int i =1;i<=n;i++)
b[i]=0;
for (int j = 0;j < val.size(); j++){
int tp = val[j].f.s,id = val[j].s;
if (tp == 2){
b[id] = 1;
upd(1,1,n,id);
}else{
L[id] = getmaxr(1,1,n,l,id - 1);
R[id] = getmaxl(1,1,n,id + 1,r);
if (!L[id]) L[id] = l;
if (!R[id]) R[id] = r;
}
}
}
void build(int h,int l,int r){
if (l== r){
t[h]=a[l];
return;
}
build(left),build(right);
t[h] = min(t[h*2],t[h*2+1]);
}
int getmin(int h,int l,int r,int L,int R){
if (r < L || R < l) return 1e9;
if (L <= l && r <= R) return t[h];
return min(getmin(left,L,R),getmin(right,L,R));
}
void init(int N, vector<int> H) {
n = N;
for (int i = 1; i <= n; i++)
a[i] = H[i-1];
build(1,1,n);
}
int max_towers(int lf, int rg, int D) {
l=lf+1,r=rg+1,d=D;
vector <pair<int,int> > vec;
vec.clear(); val.clear();
for (int i = l; i <= r; i++){
val.pb({{a[i]-d,2},i});
val.pb({{a[i],1},i});
vec.pb({a[i],i});
}
using P = pair<pair<int,int>,int> ;
sort(val.begin(),val.end(), greater <P> ());
sort(vec.begin(),vec.end());
find();
int ans=0;
for (auto [x,id]: vec)
if (getmin(1,1,n, L[id], R[id]) >= x) ans++;
return ans;
}
Compilation message
towers.cpp: In function 'void find()':
towers.cpp:64:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int j = 0;j < val.size(); j++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4030 ms |
5144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
388 KB |
Output is correct |
16 |
Correct |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
476 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
508 KB |
Output is correct |
30 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
388 KB |
Output is correct |
16 |
Correct |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
476 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
508 KB |
Output is correct |
30 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
36 |
Correct |
28 ms |
2636 KB |
Output is correct |
37 |
Correct |
53 ms |
4912 KB |
Output is correct |
38 |
Correct |
27 ms |
3300 KB |
Output is correct |
39 |
Correct |
47 ms |
4724 KB |
Output is correct |
40 |
Correct |
25 ms |
3264 KB |
Output is correct |
41 |
Correct |
115 ms |
8240 KB |
Output is correct |
42 |
Correct |
18 ms |
2528 KB |
Output is correct |
43 |
Correct |
56 ms |
5716 KB |
Output is correct |
44 |
Correct |
21 ms |
3272 KB |
Output is correct |
45 |
Correct |
14 ms |
2468 KB |
Output is correct |
46 |
Correct |
22 ms |
2480 KB |
Output is correct |
47 |
Correct |
58 ms |
4784 KB |
Output is correct |
48 |
Correct |
37 ms |
4028 KB |
Output is correct |
49 |
Correct |
29 ms |
2760 KB |
Output is correct |
50 |
Correct |
29 ms |
4048 KB |
Output is correct |
51 |
Correct |
42 ms |
5148 KB |
Output is correct |
52 |
Correct |
121 ms |
9264 KB |
Output is correct |
53 |
Correct |
111 ms |
9104 KB |
Output is correct |
54 |
Correct |
116 ms |
9212 KB |
Output is correct |
55 |
Correct |
74 ms |
9144 KB |
Output is correct |
56 |
Correct |
80 ms |
9120 KB |
Output is correct |
57 |
Correct |
120 ms |
9068 KB |
Output is correct |
58 |
Correct |
105 ms |
9104 KB |
Output is correct |
59 |
Correct |
117 ms |
9208 KB |
Output is correct |
60 |
Correct |
135 ms |
9112 KB |
Output is correct |
61 |
Correct |
107 ms |
9208 KB |
Output is correct |
62 |
Correct |
101 ms |
9100 KB |
Output is correct |
63 |
Correct |
104 ms |
9208 KB |
Output is correct |
64 |
Correct |
74 ms |
9144 KB |
Output is correct |
65 |
Correct |
75 ms |
9116 KB |
Output is correct |
66 |
Correct |
76 ms |
9108 KB |
Output is correct |
67 |
Correct |
64 ms |
9144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4003 ms |
9076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4029 ms |
2440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
388 KB |
Output is correct |
16 |
Correct |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
476 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
508 KB |
Output is correct |
30 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
35 |
Correct |
2 ms |
464 KB |
Output is correct |
36 |
Correct |
28 ms |
2636 KB |
Output is correct |
37 |
Correct |
53 ms |
4912 KB |
Output is correct |
38 |
Correct |
27 ms |
3300 KB |
Output is correct |
39 |
Correct |
47 ms |
4724 KB |
Output is correct |
40 |
Correct |
25 ms |
3264 KB |
Output is correct |
41 |
Correct |
115 ms |
8240 KB |
Output is correct |
42 |
Correct |
18 ms |
2528 KB |
Output is correct |
43 |
Correct |
56 ms |
5716 KB |
Output is correct |
44 |
Correct |
21 ms |
3272 KB |
Output is correct |
45 |
Correct |
14 ms |
2468 KB |
Output is correct |
46 |
Correct |
22 ms |
2480 KB |
Output is correct |
47 |
Correct |
58 ms |
4784 KB |
Output is correct |
48 |
Correct |
37 ms |
4028 KB |
Output is correct |
49 |
Correct |
29 ms |
2760 KB |
Output is correct |
50 |
Correct |
29 ms |
4048 KB |
Output is correct |
51 |
Correct |
42 ms |
5148 KB |
Output is correct |
52 |
Correct |
121 ms |
9264 KB |
Output is correct |
53 |
Correct |
111 ms |
9104 KB |
Output is correct |
54 |
Correct |
116 ms |
9212 KB |
Output is correct |
55 |
Correct |
74 ms |
9144 KB |
Output is correct |
56 |
Correct |
80 ms |
9120 KB |
Output is correct |
57 |
Correct |
120 ms |
9068 KB |
Output is correct |
58 |
Correct |
105 ms |
9104 KB |
Output is correct |
59 |
Correct |
117 ms |
9208 KB |
Output is correct |
60 |
Correct |
135 ms |
9112 KB |
Output is correct |
61 |
Correct |
107 ms |
9208 KB |
Output is correct |
62 |
Correct |
101 ms |
9100 KB |
Output is correct |
63 |
Correct |
104 ms |
9208 KB |
Output is correct |
64 |
Correct |
74 ms |
9144 KB |
Output is correct |
65 |
Correct |
75 ms |
9116 KB |
Output is correct |
66 |
Correct |
76 ms |
9108 KB |
Output is correct |
67 |
Correct |
64 ms |
9144 KB |
Output is correct |
68 |
Execution timed out |
4003 ms |
9076 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4030 ms |
5144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |