#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#include "towers.h"
#include <vector>
int st=0;
int n;
vector<int> it;
int tree[4*200001][2];
void build(int no,int l,int r){
if(l==r){
tree[no][0]=it[l];
tree[no][1]=it[l];
}
else{
int mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
tree[no][0]=min(tree[no*2+1][0],tree[no*2+2][0]);
tree[no][1]=max(tree[no*2+1][1],tree[no*2+2][1]);
}
}
int query(int no,int l,int r,int aa,int bb){
if(r<aa or l>bb or aa>bb){
return 1e9+1;
}
if(aa<=l and r<=bb){
return tree[no][0];
}
int mid=(l+r)/2;
return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
int query2(int no,int l,int r,int aa,int bb){
if(r<aa or l>bb or aa>bb){
return -1;
}
if(aa<=l and r<=bb){
return tree[no][1];
}
int mid=(l+r)/2;
return max(query2(no*2+1,l,mid,aa,bb),query2(no*2+2,mid+1,r,aa,bb));
}
vector<int> mm;
void init(int nn, vector<int> ita) {
n=nn;
it=ita;
st=0;
build(0,0,n-1);
}
int le[100001];
int re2[100001];
int cc[100001][20];
int dd[100001][20];
int val[100001];
void rec(int l,int r){
mm.clear();
vector<pair<llo,llo>> ss;
for(int i=r;i>=l;i--){
while(ss.size()){
if(ss.back().a<=it[i]){
ss.pop_back();
}
else{
break;
}
}
re2[i]=n;
if(ss.size()){
re2[i]=ss.back().b;
}
ss.pb({it[i],i});
}
ss.clear();
for(int i=l;i<=r;i++){
while(ss.size()){
if(ss.back().a<it[i]){
ss.pop_back();
}
else{
break;
}
}
le[i]=-1;
if(ss.size()){
le[i]=ss.back().b;
}
ss.pb({it[i],i});
}
for(int i=0;i<n;i++){
int x=it[i]-query(0,0,n-1,le[i]+1,i);
int y=it[i]-query(0,0,n-1,i,re2[i]-1);
val[i]=min(x,y);
mm.pb(min(x,y));
}
for(int i=0;i<n;i++){
cc[i][0]=le[i];
dd[i][0]=re2[i];
// cout<<i<<":"<<le[i]<<endl;
// cout<<i<<":"<<re2[i]<<endl;
if(re2[i]==n){
dd[i][0]=-1;
}
}
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(cc[i][j-1]==-1){
cc[i][j]=-1;
}
else{
cc[i][j]=cc[cc[i][j-1]][j-1];
}
}
}
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(dd[i][j-1]==-1){
dd[i][j]=-1;
}
else{
dd[i][j]=dd[dd[i][j-1]][j-1];
}
}
}
sort(mm.begin(),mm.end());
}
set<pair<int,int>> xx[2];
vector<pair<pair<int,int>,int>> yy;
map<int,int> kk;
int max_towers(int l, int r, int d) {
if(st==0){
rec(0,n-1);
st=1;
}
//cout<<dd[0][0]<<"???"<<endl;
llo low=mm.size();
llo su=0;
for(int i=l;i<=r;i++){
if(val[i]>=d){
su++;
}
}
int ind=-1;
for(int i=l;i<=r;i++){
if(cc[i][0]<l){
ind=i;
break;
}
}
int ind2=ind;
for(int j=19;j>=0;j--){
if(dd[ind2][j]<=r and dd[ind2][j]>=0){
ind2=dd[ind2][j];
}
}
vector<llo> ss;
int ind3=-1;
if(query(0,0,n-1,l,ind-1)>it[ind]-d){
for(int j=19;j>=0;j--){
if(dd[ind][j]<=r and dd[ind][j]>=0){
if(query(0,0,n-1,l,dd[ind][j]-1)>it[dd[ind][j]]-d){
ind=dd[ind][j];
}
}
}
ind3=ind;
while(true){
ss.pb(ind3);
if(ind3==ind){
break;
}
ind3=dd[ind3][0];
}
}
//from ind3 to ind
//return su;
vector<int> tt;
ind=-1;
for(int i=r;i>=l;i--){
if(dd[i][0]>r or dd[i][0]==-1){
ind=i;
break;
}
}
if(query(0,0,n-1,ind+1,r)<=it[ind]-d){
}
else{
ind3=ind;
for(int j=19;j>=0;j--){
if(cc[ind][j]>=l){
if(query(0,0,n-1,cc[ind][j]+1,r)>it[cc[ind][j]]-d){
ind=cc[ind][j];
}
}
}
while(true){
tt.pb(ind3);
if(ind3==ind){
break;
}
ind3=cc[ind3][0];
}
}
for(auto j:ss){
if(val[j]>=d){
su--;
}
}
for(auto j:tt){
if(val[j]>=d){
su--;
}
}
if(ss.size() and tt.size() and ss.back()==tt.back()){
if(val[ss.back()]>=d){
su++;
}
}
su++;
return su;
}
Compilation message
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:144:6: warning: unused variable 'low' [-Wunused-variable]
144 | llo low=mm.size();
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1635 ms |
13084 KB |
Output is correct |
2 |
Correct |
3697 ms |
22296 KB |
Output is correct |
3 |
Correct |
3515 ms |
22328 KB |
Output is correct |
4 |
Correct |
3542 ms |
22428 KB |
Output is correct |
5 |
Execution timed out |
4016 ms |
22320 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
720 KB |
Output is correct |
3 |
Correct |
1 ms |
720 KB |
Output is correct |
4 |
Correct |
1 ms |
848 KB |
Output is correct |
5 |
Correct |
1 ms |
720 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
720 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
720 KB |
Output is correct |
10 |
Correct |
1 ms |
720 KB |
Output is correct |
11 |
Correct |
1 ms |
720 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
720 KB |
Output is correct |
14 |
Correct |
1 ms |
720 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
720 KB |
Output is correct |
18 |
Correct |
1 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
720 KB |
Output is correct |
20 |
Correct |
1 ms |
720 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
792 KB |
Output is correct |
24 |
Correct |
1 ms |
720 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
720 KB |
Output is correct |
27 |
Correct |
1 ms |
720 KB |
Output is correct |
28 |
Correct |
1 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
720 KB |
Output is correct |
30 |
Correct |
1 ms |
720 KB |
Output is correct |
31 |
Correct |
1 ms |
720 KB |
Output is correct |
32 |
Correct |
1 ms |
720 KB |
Output is correct |
33 |
Correct |
1 ms |
720 KB |
Output is correct |
34 |
Correct |
1 ms |
720 KB |
Output is correct |
35 |
Correct |
1 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
720 KB |
Output is correct |
3 |
Correct |
1 ms |
720 KB |
Output is correct |
4 |
Correct |
1 ms |
848 KB |
Output is correct |
5 |
Correct |
1 ms |
720 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
720 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
720 KB |
Output is correct |
10 |
Correct |
1 ms |
720 KB |
Output is correct |
11 |
Correct |
1 ms |
720 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
720 KB |
Output is correct |
14 |
Correct |
1 ms |
720 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
720 KB |
Output is correct |
18 |
Correct |
1 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
720 KB |
Output is correct |
20 |
Correct |
1 ms |
720 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
792 KB |
Output is correct |
24 |
Correct |
1 ms |
720 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
720 KB |
Output is correct |
27 |
Correct |
1 ms |
720 KB |
Output is correct |
28 |
Correct |
1 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
720 KB |
Output is correct |
30 |
Correct |
1 ms |
720 KB |
Output is correct |
31 |
Correct |
1 ms |
720 KB |
Output is correct |
32 |
Correct |
1 ms |
720 KB |
Output is correct |
33 |
Correct |
1 ms |
720 KB |
Output is correct |
34 |
Correct |
1 ms |
720 KB |
Output is correct |
35 |
Correct |
1 ms |
720 KB |
Output is correct |
36 |
Correct |
40 ms |
13256 KB |
Output is correct |
37 |
Correct |
56 ms |
20864 KB |
Output is correct |
38 |
Correct |
67 ms |
20868 KB |
Output is correct |
39 |
Correct |
70 ms |
20832 KB |
Output is correct |
40 |
Correct |
57 ms |
20860 KB |
Output is correct |
41 |
Correct |
56 ms |
20844 KB |
Output is correct |
42 |
Correct |
56 ms |
20780 KB |
Output is correct |
43 |
Correct |
58 ms |
22328 KB |
Output is correct |
44 |
Correct |
52 ms |
22336 KB |
Output is correct |
45 |
Correct |
59 ms |
21976 KB |
Output is correct |
46 |
Correct |
54 ms |
21736 KB |
Output is correct |
47 |
Correct |
57 ms |
20864 KB |
Output is correct |
48 |
Correct |
70 ms |
20764 KB |
Output is correct |
49 |
Correct |
60 ms |
20792 KB |
Output is correct |
50 |
Correct |
52 ms |
22328 KB |
Output is correct |
51 |
Correct |
52 ms |
22296 KB |
Output is correct |
52 |
Correct |
57 ms |
20740 KB |
Output is correct |
53 |
Correct |
62 ms |
20792 KB |
Output is correct |
54 |
Correct |
57 ms |
20788 KB |
Output is correct |
55 |
Correct |
62 ms |
22428 KB |
Output is correct |
56 |
Correct |
59 ms |
22200 KB |
Output is correct |
57 |
Correct |
54 ms |
20152 KB |
Output is correct |
58 |
Correct |
65 ms |
20816 KB |
Output is correct |
59 |
Correct |
74 ms |
20844 KB |
Output is correct |
60 |
Correct |
62 ms |
20788 KB |
Output is correct |
61 |
Correct |
63 ms |
20872 KB |
Output is correct |
62 |
Correct |
74 ms |
20788 KB |
Output is correct |
63 |
Correct |
63 ms |
20776 KB |
Output is correct |
64 |
Correct |
51 ms |
22428 KB |
Output is correct |
65 |
Correct |
63 ms |
22324 KB |
Output is correct |
66 |
Correct |
57 ms |
21748 KB |
Output is correct |
67 |
Correct |
54 ms |
22352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2831 ms |
20720 KB |
Output is correct |
2 |
Correct |
3198 ms |
20864 KB |
Output is correct |
3 |
Correct |
3233 ms |
20868 KB |
Output is correct |
4 |
Correct |
3158 ms |
20768 KB |
Output is correct |
5 |
Correct |
3117 ms |
20868 KB |
Output is correct |
6 |
Correct |
3081 ms |
20868 KB |
Output is correct |
7 |
Correct |
3216 ms |
20776 KB |
Output is correct |
8 |
Correct |
3502 ms |
22424 KB |
Output is correct |
9 |
Correct |
3290 ms |
22428 KB |
Output is correct |
10 |
Correct |
3666 ms |
21936 KB |
Output is correct |
11 |
Correct |
3190 ms |
21928 KB |
Output is correct |
12 |
Correct |
3416 ms |
22376 KB |
Output is correct |
13 |
Correct |
3364 ms |
22328 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
72 ms |
20968 KB |
Output is correct |
18 |
Correct |
62 ms |
20804 KB |
Output is correct |
19 |
Correct |
56 ms |
20772 KB |
Output is correct |
20 |
Correct |
53 ms |
22328 KB |
Output is correct |
21 |
Correct |
52 ms |
22304 KB |
Output is correct |
22 |
Correct |
56 ms |
20768 KB |
Output is correct |
23 |
Correct |
62 ms |
20972 KB |
Output is correct |
24 |
Correct |
63 ms |
20852 KB |
Output is correct |
25 |
Correct |
52 ms |
22332 KB |
Output is correct |
26 |
Correct |
56 ms |
22292 KB |
Output is correct |
27 |
Correct |
1 ms |
720 KB |
Output is correct |
28 |
Correct |
1 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
720 KB |
Output is correct |
30 |
Correct |
1 ms |
720 KB |
Output is correct |
31 |
Correct |
1 ms |
720 KB |
Output is correct |
32 |
Correct |
1 ms |
700 KB |
Output is correct |
33 |
Correct |
1 ms |
720 KB |
Output is correct |
34 |
Correct |
2 ms |
720 KB |
Output is correct |
35 |
Correct |
1 ms |
720 KB |
Output is correct |
36 |
Correct |
1 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
734 ms |
5200 KB |
Output is correct |
2 |
Execution timed out |
4030 ms |
20856 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
720 KB |
Output is correct |
3 |
Correct |
1 ms |
720 KB |
Output is correct |
4 |
Correct |
1 ms |
848 KB |
Output is correct |
5 |
Correct |
1 ms |
720 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
720 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
720 KB |
Output is correct |
10 |
Correct |
1 ms |
720 KB |
Output is correct |
11 |
Correct |
1 ms |
720 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
720 KB |
Output is correct |
14 |
Correct |
1 ms |
720 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
720 KB |
Output is correct |
18 |
Correct |
1 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
720 KB |
Output is correct |
20 |
Correct |
1 ms |
720 KB |
Output is correct |
21 |
Correct |
1 ms |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
720 KB |
Output is correct |
23 |
Correct |
1 ms |
792 KB |
Output is correct |
24 |
Correct |
1 ms |
720 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
720 KB |
Output is correct |
27 |
Correct |
1 ms |
720 KB |
Output is correct |
28 |
Correct |
1 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
720 KB |
Output is correct |
30 |
Correct |
1 ms |
720 KB |
Output is correct |
31 |
Correct |
1 ms |
720 KB |
Output is correct |
32 |
Correct |
1 ms |
720 KB |
Output is correct |
33 |
Correct |
1 ms |
720 KB |
Output is correct |
34 |
Correct |
1 ms |
720 KB |
Output is correct |
35 |
Correct |
1 ms |
720 KB |
Output is correct |
36 |
Correct |
40 ms |
13256 KB |
Output is correct |
37 |
Correct |
56 ms |
20864 KB |
Output is correct |
38 |
Correct |
67 ms |
20868 KB |
Output is correct |
39 |
Correct |
70 ms |
20832 KB |
Output is correct |
40 |
Correct |
57 ms |
20860 KB |
Output is correct |
41 |
Correct |
56 ms |
20844 KB |
Output is correct |
42 |
Correct |
56 ms |
20780 KB |
Output is correct |
43 |
Correct |
58 ms |
22328 KB |
Output is correct |
44 |
Correct |
52 ms |
22336 KB |
Output is correct |
45 |
Correct |
59 ms |
21976 KB |
Output is correct |
46 |
Correct |
54 ms |
21736 KB |
Output is correct |
47 |
Correct |
57 ms |
20864 KB |
Output is correct |
48 |
Correct |
70 ms |
20764 KB |
Output is correct |
49 |
Correct |
60 ms |
20792 KB |
Output is correct |
50 |
Correct |
52 ms |
22328 KB |
Output is correct |
51 |
Correct |
52 ms |
22296 KB |
Output is correct |
52 |
Correct |
57 ms |
20740 KB |
Output is correct |
53 |
Correct |
62 ms |
20792 KB |
Output is correct |
54 |
Correct |
57 ms |
20788 KB |
Output is correct |
55 |
Correct |
62 ms |
22428 KB |
Output is correct |
56 |
Correct |
59 ms |
22200 KB |
Output is correct |
57 |
Correct |
54 ms |
20152 KB |
Output is correct |
58 |
Correct |
65 ms |
20816 KB |
Output is correct |
59 |
Correct |
74 ms |
20844 KB |
Output is correct |
60 |
Correct |
62 ms |
20788 KB |
Output is correct |
61 |
Correct |
63 ms |
20872 KB |
Output is correct |
62 |
Correct |
74 ms |
20788 KB |
Output is correct |
63 |
Correct |
63 ms |
20776 KB |
Output is correct |
64 |
Correct |
51 ms |
22428 KB |
Output is correct |
65 |
Correct |
63 ms |
22324 KB |
Output is correct |
66 |
Correct |
57 ms |
21748 KB |
Output is correct |
67 |
Correct |
54 ms |
22352 KB |
Output is correct |
68 |
Correct |
2831 ms |
20720 KB |
Output is correct |
69 |
Correct |
3198 ms |
20864 KB |
Output is correct |
70 |
Correct |
3233 ms |
20868 KB |
Output is correct |
71 |
Correct |
3158 ms |
20768 KB |
Output is correct |
72 |
Correct |
3117 ms |
20868 KB |
Output is correct |
73 |
Correct |
3081 ms |
20868 KB |
Output is correct |
74 |
Correct |
3216 ms |
20776 KB |
Output is correct |
75 |
Correct |
3502 ms |
22424 KB |
Output is correct |
76 |
Correct |
3290 ms |
22428 KB |
Output is correct |
77 |
Correct |
3666 ms |
21936 KB |
Output is correct |
78 |
Correct |
3190 ms |
21928 KB |
Output is correct |
79 |
Correct |
3416 ms |
22376 KB |
Output is correct |
80 |
Correct |
3364 ms |
22328 KB |
Output is correct |
81 |
Correct |
0 ms |
336 KB |
Output is correct |
82 |
Correct |
1 ms |
720 KB |
Output is correct |
83 |
Correct |
1 ms |
720 KB |
Output is correct |
84 |
Correct |
72 ms |
20968 KB |
Output is correct |
85 |
Correct |
62 ms |
20804 KB |
Output is correct |
86 |
Correct |
56 ms |
20772 KB |
Output is correct |
87 |
Correct |
53 ms |
22328 KB |
Output is correct |
88 |
Correct |
52 ms |
22304 KB |
Output is correct |
89 |
Correct |
56 ms |
20768 KB |
Output is correct |
90 |
Correct |
62 ms |
20972 KB |
Output is correct |
91 |
Correct |
63 ms |
20852 KB |
Output is correct |
92 |
Correct |
52 ms |
22332 KB |
Output is correct |
93 |
Correct |
56 ms |
22292 KB |
Output is correct |
94 |
Correct |
1 ms |
720 KB |
Output is correct |
95 |
Correct |
1 ms |
720 KB |
Output is correct |
96 |
Correct |
1 ms |
720 KB |
Output is correct |
97 |
Correct |
1 ms |
720 KB |
Output is correct |
98 |
Correct |
1 ms |
720 KB |
Output is correct |
99 |
Correct |
1 ms |
700 KB |
Output is correct |
100 |
Correct |
1 ms |
720 KB |
Output is correct |
101 |
Correct |
2 ms |
720 KB |
Output is correct |
102 |
Correct |
1 ms |
720 KB |
Output is correct |
103 |
Correct |
1 ms |
720 KB |
Output is correct |
104 |
Correct |
2441 ms |
18736 KB |
Output is correct |
105 |
Correct |
3270 ms |
20780 KB |
Output is correct |
106 |
Correct |
3216 ms |
20836 KB |
Output is correct |
107 |
Correct |
3717 ms |
20776 KB |
Output is correct |
108 |
Correct |
3337 ms |
20776 KB |
Output is correct |
109 |
Correct |
3346 ms |
20788 KB |
Output is correct |
110 |
Correct |
3635 ms |
20872 KB |
Output is correct |
111 |
Correct |
3675 ms |
22504 KB |
Output is correct |
112 |
Execution timed out |
4011 ms |
22420 KB |
Time limit exceeded |
113 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1635 ms |
13084 KB |
Output is correct |
2 |
Correct |
3697 ms |
22296 KB |
Output is correct |
3 |
Correct |
3515 ms |
22328 KB |
Output is correct |
4 |
Correct |
3542 ms |
22428 KB |
Output is correct |
5 |
Execution timed out |
4016 ms |
22320 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |