답안 #787141

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
787141 2023-07-18T21:10:16 Z alexander707070 송신탑 (IOI22_towers) C++17
73 / 100
4000 ms 7360 KB
#include<bits/stdc++.h>
#define MAXN 100007
using namespace std;

const long long inf=1e16;
 
int n,h[MAXN],maxh,num,delta,from[MAXN],to[MAXN],border,maxdelta[MAXN];
int dp[MAXN],ans,pref[MAXN],se,te;
 
pair<int,int> tree[4*MAXN];
int mins[4*MAXN];
 
pair<int,int> combine(pair<int,int> fr,pair<int,int> sc){
    if(fr.first>sc.first)return fr;
    return sc;
}
 
void build(int v,int l,int r){
    if(l==r){
        tree[v]={h[l],l};
        mins[v]=h[l];
    }else{
        int tt=(l+r)/2;
        build(2*v,l,tt);
        build(2*v+1,tt+1,r);
        tree[v]=combine(tree[2*v],tree[2*v+1]);
        mins[v]=min(mins[2*v],mins[2*v+1]);
    }
}
 
pair<int,int> best(int v,int l,int r,int ll,int rr){
    if(ll>rr)return {-1,0};
    if(l==ll and r==rr){
        return tree[v];
    }else{
        int tt=(l+r)/2;
        return combine( best(2*v,l,tt,ll,min(tt,rr)) , best(2*v+1,tt+1,r,max(tt+1,ll),rr) );
    }
}
 
int minh(int v,int l,int r,int ll,int rr){
    if(ll>rr)return 1000000000;
    if(l==ll and r==rr){
        return mins[v];
    }else{
        int tt=(l+r)/2;
        return min( minh(2*v,l,tt,ll,min(tt,rr)) , minh(2*v+1,tt+1,r,max(tt+1,ll),rr) );
    }
}

vector<int> st,poss,lt,rt;
 
void calc(){
    for(int i=1;i<=n;i++){
        while(!st.empty() and h[i]>h[st.back()]){
            st.pop_back();
        }
        st.push_back(i);

        int l=-1,r=st.size(),tt;
        while(l+1<r){
            tt=(l+r)/2;
            if(h[st[tt]]>=h[i]+delta){
                l=tt;
            }else{
                r=tt;
            }
        }

        if(l!=-1 and minh(1,0,n+1,st[l],i)>=h[i])from[i]=st[l];
        else from[i]=0;
    }

    while(!st.empty())st.pop_back();

    for(int i=n;i>=1;i--){
        while(!st.empty() and h[i]>h[st.back()]){
            st.pop_back();
        }
        st.push_back(i);

        int l=-1,r=st.size(),tt;
        while(l+1<r){
            tt=(l+r)/2;
            if(h[st[tt]]>=h[i]+delta){
                l=tt;
            }else{
                r=tt;
            }
        }
        if(l!=-1 and minh(1,0,n+1,i,st[l])>=h[i])to[i]=st[l];
        else to[i]=n+1;
    }

    for(int i=1;i<=n;i++){
        if(from[i]>=1 and to[i]<=n){
            poss.push_back(i);
        }
        if(to[i]<=n)lt.push_back(i);
        if(from[i]>=1)rt.push_back(i);
    }
}

void calc2(){
    for(int i=1;i<=n;i++)maxdelta[i]=inf;

    for(int i=0;i<=n;i++){
        while(!st.empty() and h[i]>h[st.back()]){
            st.pop_back();
        }
        st.push_back(i);

        int l=-1,r=st.size(),tt;
        while(l+1<r){
            tt=(l+r)/2;
            if(minh(1,0,n+1,st[tt],i)>=h[i]){
                r=tt;
            }else{
                l=tt;
            }
        }


        maxdelta[i]=min(maxdelta[i],h[st[r]]-h[i]);
    }
    while(!st.empty())st.pop_back();

    for(int i=n+1;i>=1;i--){
        while(!st.empty() and h[i]>h[st.back()]){
            st.pop_back();
        }
        st.push_back(i);

        int l=-1,r=st.size(),tt;
        while(l+1<r){
            tt=(l+r)/2;
            if(minh(1,0,n+1,i,st[tt])>=h[i]){
                r=tt;
            }else{
                l=tt;
            }
        }
        
        maxdelta[i]=min(maxdelta[i],h[st[r]]-h[i]);
    }
    while(!st.empty())st.pop_back();

    sort(maxdelta+1,maxdelta+n+1);
}

void init(int N,vector<int> H){
    n=N;
    for(int i=1;i<=n;i++){
        h[i]=H[i-1];
    }
    h[0]=h[n+1]=1000000000;

    build(1,0,n+1);
    calc2();
}
 
int solve(int l,int r,int maxh,int d){
    if(l>r)return 0;
 
    int pos=best(1,0,n+1,l,r).second;
 
    int z=0;
    if(minh(1,0,n+1,l,r)<=maxh)z=1;
 
    return max(z, solve(l,pos-1,h[pos]-d,d)+solve(pos+1,r,h[pos]-d,d) );
}
 
int max_towers(int L, int R, int D){
    if(delta==0){
        delta=D;
        calc();
    }

    L++; R++;
    if(D==delta){
        ans=0;

        int l=-1,r=poss.size(),tt;
        while(l+1<r){
            tt=(l+r)/2;
            if(from[poss[tt]]>=L){
                r=tt;
            }else{
                l=tt;
            }
        }
        se=r;

        l=-1; r=poss.size();
        while(l+1<r){
            tt=(l+r)/2;
            if(to[poss[tt]]<=R){
                l=tt;
            }else{
                r=tt;
            }
        }
        te=l;

        if(se<=te)ans+=te-se+1;

        if(se==poss.size())se=R+1;
        else se=poss[se];

        if(te==-1)te=L-1;
        else te=poss[te];

        l=-1; r=lt.size();
        while(l+1<r){
            tt=(l+r)/2;
            if(lt[tt]>=L){
                r=tt;
            }else{
                l=tt;
            }
        }

        border=-1;
        if(r!=lt.size() and to[lt[r]]<se){
            border=to[lt[r]]; ans++;
        }

        l=-1; r=rt.size();
        while(l+1<r){
            tt=(l+r)/2;
            if(rt[tt]<=R){
                l=tt;
            }else{
                r=tt;
            }
        }
        if(l!=-1 and from[rt[l]]>=max(border,te+1))ans++;

        if(ans==0)ans++;

        return ans;   
    }

    if(L==1 and R==n){
        int l=0,r=n+1,tt;
        while(l+1<r){
            tt=(l+r)/2;
            if(maxdelta[tt]>=D){
                r=tt;
            }else{
                l=tt;
            }
        }
        return max(n-r+1,1);
    }

    return solve(L,R,1000000000,D);
}
 
/*
int main(){
    init(7, {10, 20, 60, 40, 50, 30, 70});
    cout<<max_towers(1,5,10)<<"\n";
    cout<<max_towers(0, 6, 17)<<"\n";
}
*/

Compilation message

towers.cpp: In function 'void calc2()':
towers.cpp:105:38: warning: overflow in conversion from 'long long int' to 'int' changes value from '10000000000000000' to '1874919424' [-Woverflow]
  105 |     for(int i=1;i<=n;i++)maxdelta[i]=inf;
      |                                      ^~~
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:207:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  207 |         if(se==poss.size())se=R+1;
      |            ~~^~~~~~~~~~~~~
towers.cpp:224:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  224 |         if(r!=lt.size() and to[lt[r]]<se){
      |            ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4003 ms 7360 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 3 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 3 ms 336 KB Output is correct
11 Correct 4 ms 336 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 3 ms 424 KB Output is correct
14 Correct 3 ms 336 KB Output is correct
15 Correct 3 ms 332 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 3 ms 412 KB Output is correct
18 Correct 3 ms 424 KB Output is correct
19 Correct 3 ms 336 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 464 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 3 ms 388 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 2 ms 336 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 2 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 2 ms 336 KB Output is correct
32 Correct 3 ms 336 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 4 ms 336 KB Output is correct
35 Correct 4 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 3 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 3 ms 336 KB Output is correct
11 Correct 4 ms 336 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 3 ms 424 KB Output is correct
14 Correct 3 ms 336 KB Output is correct
15 Correct 3 ms 332 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 3 ms 412 KB Output is correct
18 Correct 3 ms 424 KB Output is correct
19 Correct 3 ms 336 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 464 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 3 ms 388 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 2 ms 336 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 2 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 2 ms 336 KB Output is correct
32 Correct 3 ms 336 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 4 ms 336 KB Output is correct
35 Correct 4 ms 336 KB Output is correct
36 Correct 108 ms 3644 KB Output is correct
37 Correct 129 ms 5556 KB Output is correct
38 Correct 140 ms 6096 KB Output is correct
39 Correct 119 ms 5340 KB Output is correct
40 Correct 141 ms 6228 KB Output is correct
41 Correct 138 ms 5844 KB Output is correct
42 Correct 141 ms 6232 KB Output is correct
43 Correct 304 ms 6528 KB Output is correct
44 Correct 301 ms 6456 KB Output is correct
45 Correct 291 ms 5720 KB Output is correct
46 Correct 307 ms 5956 KB Output is correct
47 Correct 156 ms 6220 KB Output is correct
48 Correct 150 ms 6228 KB Output is correct
49 Correct 140 ms 6172 KB Output is correct
50 Correct 299 ms 6580 KB Output is correct
51 Correct 288 ms 6540 KB Output is correct
52 Correct 153 ms 6320 KB Output is correct
53 Correct 143 ms 6204 KB Output is correct
54 Correct 148 ms 6252 KB Output is correct
55 Correct 292 ms 6700 KB Output is correct
56 Correct 323 ms 6572 KB Output is correct
57 Correct 127 ms 5628 KB Output is correct
58 Correct 138 ms 5932 KB Output is correct
59 Correct 140 ms 6168 KB Output is correct
60 Correct 143 ms 6224 KB Output is correct
61 Correct 147 ms 6104 KB Output is correct
62 Correct 133 ms 6064 KB Output is correct
63 Correct 138 ms 6208 KB Output is correct
64 Correct 294 ms 6100 KB Output is correct
65 Correct 301 ms 6116 KB Output is correct
66 Correct 300 ms 6012 KB Output is correct
67 Correct 295 ms 6156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 735 ms 6160 KB Output is correct
2 Correct 816 ms 6160 KB Output is correct
3 Correct 834 ms 6220 KB Output is correct
4 Correct 804 ms 6232 KB Output is correct
5 Correct 943 ms 6236 KB Output is correct
6 Correct 876 ms 6160 KB Output is correct
7 Correct 940 ms 6232 KB Output is correct
8 Correct 995 ms 6648 KB Output is correct
9 Correct 941 ms 6652 KB Output is correct
10 Correct 1000 ms 6544 KB Output is correct
11 Correct 978 ms 6552 KB Output is correct
12 Correct 946 ms 6644 KB Output is correct
13 Correct 989 ms 6648 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 4 ms 336 KB Output is correct
17 Correct 152 ms 6220 KB Output is correct
18 Correct 143 ms 6248 KB Output is correct
19 Correct 141 ms 6232 KB Output is correct
20 Correct 290 ms 6544 KB Output is correct
21 Correct 298 ms 6612 KB Output is correct
22 Correct 155 ms 6184 KB Output is correct
23 Correct 150 ms 6204 KB Output is correct
24 Correct 142 ms 6220 KB Output is correct
25 Correct 289 ms 6548 KB Output is correct
26 Correct 313 ms 6572 KB Output is correct
27 Correct 3 ms 336 KB Output is correct
28 Correct 3 ms 336 KB Output is correct
29 Correct 3 ms 432 KB Output is correct
30 Correct 3 ms 336 KB Output is correct
31 Correct 3 ms 336 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 2 ms 336 KB Output is correct
35 Correct 3 ms 336 KB Output is correct
36 Correct 4 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 257 ms 1764 KB Output is correct
2 Correct 796 ms 5824 KB Output is correct
3 Correct 875 ms 6176 KB Output is correct
4 Correct 830 ms 5716 KB Output is correct
5 Correct 827 ms 5708 KB Output is correct
6 Correct 866 ms 5292 KB Output is correct
7 Correct 889 ms 5712 KB Output is correct
8 Correct 982 ms 6124 KB Output is correct
9 Correct 840 ms 6592 KB Output is correct
10 Correct 949 ms 6476 KB Output is correct
11 Correct 923 ms 5904 KB Output is correct
12 Correct 154 ms 6224 KB Output is correct
13 Correct 155 ms 6240 KB Output is correct
14 Correct 146 ms 6224 KB Output is correct
15 Correct 303 ms 6628 KB Output is correct
16 Correct 337 ms 6580 KB Output is correct
17 Correct 138 ms 5672 KB Output is correct
18 Correct 138 ms 5720 KB Output is correct
19 Correct 142 ms 6056 KB Output is correct
20 Correct 146 ms 6204 KB Output is correct
21 Correct 139 ms 6220 KB Output is correct
22 Correct 134 ms 6100 KB Output is correct
23 Correct 140 ms 6324 KB Output is correct
24 Correct 290 ms 6084 KB Output is correct
25 Correct 297 ms 6120 KB Output is correct
26 Correct 302 ms 5984 KB Output is correct
27 Correct 312 ms 6228 KB Output is correct
28 Correct 3 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 3 ms 336 KB Output is correct
31 Correct 3 ms 336 KB Output is correct
32 Correct 3 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 2 ms 336 KB Output is correct
35 Correct 3 ms 336 KB Output is correct
36 Correct 2 ms 336 KB Output is correct
37 Correct 2 ms 336 KB Output is correct
38 Correct 2 ms 336 KB Output is correct
39 Correct 2 ms 336 KB Output is correct
40 Correct 3 ms 336 KB Output is correct
41 Correct 3 ms 428 KB Output is correct
42 Correct 3 ms 336 KB Output is correct
43 Correct 4 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 3 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 3 ms 336 KB Output is correct
11 Correct 4 ms 336 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 3 ms 424 KB Output is correct
14 Correct 3 ms 336 KB Output is correct
15 Correct 3 ms 332 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 3 ms 412 KB Output is correct
18 Correct 3 ms 424 KB Output is correct
19 Correct 3 ms 336 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 464 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 3 ms 388 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 2 ms 336 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 2 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 2 ms 336 KB Output is correct
32 Correct 3 ms 336 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 4 ms 336 KB Output is correct
35 Correct 4 ms 336 KB Output is correct
36 Correct 108 ms 3644 KB Output is correct
37 Correct 129 ms 5556 KB Output is correct
38 Correct 140 ms 6096 KB Output is correct
39 Correct 119 ms 5340 KB Output is correct
40 Correct 141 ms 6228 KB Output is correct
41 Correct 138 ms 5844 KB Output is correct
42 Correct 141 ms 6232 KB Output is correct
43 Correct 304 ms 6528 KB Output is correct
44 Correct 301 ms 6456 KB Output is correct
45 Correct 291 ms 5720 KB Output is correct
46 Correct 307 ms 5956 KB Output is correct
47 Correct 156 ms 6220 KB Output is correct
48 Correct 150 ms 6228 KB Output is correct
49 Correct 140 ms 6172 KB Output is correct
50 Correct 299 ms 6580 KB Output is correct
51 Correct 288 ms 6540 KB Output is correct
52 Correct 153 ms 6320 KB Output is correct
53 Correct 143 ms 6204 KB Output is correct
54 Correct 148 ms 6252 KB Output is correct
55 Correct 292 ms 6700 KB Output is correct
56 Correct 323 ms 6572 KB Output is correct
57 Correct 127 ms 5628 KB Output is correct
58 Correct 138 ms 5932 KB Output is correct
59 Correct 140 ms 6168 KB Output is correct
60 Correct 143 ms 6224 KB Output is correct
61 Correct 147 ms 6104 KB Output is correct
62 Correct 133 ms 6064 KB Output is correct
63 Correct 138 ms 6208 KB Output is correct
64 Correct 294 ms 6100 KB Output is correct
65 Correct 301 ms 6116 KB Output is correct
66 Correct 300 ms 6012 KB Output is correct
67 Correct 295 ms 6156 KB Output is correct
68 Correct 735 ms 6160 KB Output is correct
69 Correct 816 ms 6160 KB Output is correct
70 Correct 834 ms 6220 KB Output is correct
71 Correct 804 ms 6232 KB Output is correct
72 Correct 943 ms 6236 KB Output is correct
73 Correct 876 ms 6160 KB Output is correct
74 Correct 940 ms 6232 KB Output is correct
75 Correct 995 ms 6648 KB Output is correct
76 Correct 941 ms 6652 KB Output is correct
77 Correct 1000 ms 6544 KB Output is correct
78 Correct 978 ms 6552 KB Output is correct
79 Correct 946 ms 6644 KB Output is correct
80 Correct 989 ms 6648 KB Output is correct
81 Correct 0 ms 336 KB Output is correct
82 Correct 3 ms 336 KB Output is correct
83 Correct 4 ms 336 KB Output is correct
84 Correct 152 ms 6220 KB Output is correct
85 Correct 143 ms 6248 KB Output is correct
86 Correct 141 ms 6232 KB Output is correct
87 Correct 290 ms 6544 KB Output is correct
88 Correct 298 ms 6612 KB Output is correct
89 Correct 155 ms 6184 KB Output is correct
90 Correct 150 ms 6204 KB Output is correct
91 Correct 142 ms 6220 KB Output is correct
92 Correct 289 ms 6548 KB Output is correct
93 Correct 313 ms 6572 KB Output is correct
94 Correct 3 ms 336 KB Output is correct
95 Correct 3 ms 336 KB Output is correct
96 Correct 3 ms 432 KB Output is correct
97 Correct 3 ms 336 KB Output is correct
98 Correct 3 ms 336 KB Output is correct
99 Correct 2 ms 336 KB Output is correct
100 Correct 3 ms 336 KB Output is correct
101 Correct 2 ms 336 KB Output is correct
102 Correct 3 ms 336 KB Output is correct
103 Correct 4 ms 336 KB Output is correct
104 Correct 855 ms 5940 KB Output is correct
105 Correct 940 ms 6108 KB Output is correct
106 Correct 924 ms 5824 KB Output is correct
107 Correct 1065 ms 6052 KB Output is correct
108 Correct 1008 ms 5456 KB Output is correct
109 Correct 1002 ms 6052 KB Output is correct
110 Correct 984 ms 6224 KB Output is correct
111 Correct 1089 ms 6224 KB Output is correct
112 Correct 1044 ms 6108 KB Output is correct
113 Correct 1139 ms 5816 KB Output is correct
114 Correct 922 ms 6348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4003 ms 7360 KB Time limit exceeded
2 Halted 0 ms 0 KB -