Submission #951497

#TimeUsernameProblemLanguageResultExecution timeMemory
951497hengliaoRainforest Jumps (APIO21_jumps)C++17
Compilation error
0 ms0 KiB
#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;

const ll mxN=2e5+5;
const ll inf=1e18;

ll n;
vll h;
vll adj[mxN];
ll r[mxN];
ll l[mxN];
vll dis;
bool cool;
ll up[mxN][20];
ll up2[mxN][20];
ll bg[mxN];
ll sm[mxN];

int rand(int a, int b){
    return a+rand()%(b-a+1);
}

void init(int tn, vector<int> th){
    n=tn;
    h.resize(n);
    cool=1;
    for(ll i=0;i<n;i++){
        h[i]=th[i];
        if(h[i]!=i+1){
            cool=0;
        }
    }
    vector<ll> stk;
    for(ll i=0;i<n;i++){
        if(!stk.empty()){
            ll lef=0, rig=stk.size()-1;
            ll tep=inf;
            while(lef<=rig){
                ll mid=(lef+rig)/2;
                if(stk[mid]>h[i]){
                    tep=min(tep, stk[mid]);
                    lef=mid+1;
                }
                else{
                    rig=mid-1;
                }
            }
            l[h[i]]=tep;
        }
        else{
            l[h[i]]=inf;
        }
        while(!stk.empty() && stk.back()<h[i]){
            stk.pop_back();
        }
        stk.pb(h[i]);
    }
    stk.clear();
    for(ll i=n-1;i>=0;i--){
        if(!stk.empty()){
            ll lef=0, rig=stk.size()-1;
            ll tep=inf;
            while(lef<=rig){
                ll mid=(lef+rig)/2;
                if(stk[mid]>h[i]){
                    tep=min(tep, stk[mid]);
                    lef=mid+1;
                }
                else{
                    rig=mid-1;
                }
            }
            r[h[i]]=tep;
        }
        else{
            r[h[i]]=inf;
        }
        while(!stk.empty() && stk.back()<h[i]){
            stk.pop_back();
        }
        stk.pb(h[i]);
    }
    for(ll i=1;i<=n;i++){
        if(l[i]!=inf){
            adj[i].pb(l[i]);
        }
        if(r[i]!=inf){
            adj[i].pb(r[i]);
        }
    }
    for(ll i=1;i<=n;i++){
        if(l[i]==inf && r[i]==inf){
            bg[i]=inf;
            sm[i]=inf;
        }
        else if(l[i]==inf){
            bg[i]=r[i];
            sm[i]=inf;
        }
        else if(r[i]==inf){
            bg[i]=l[i];
            sm[i]=inf;
        }
        else{
            bg[i]=max(l[i], r[i]);
            sm[i]=min(l[i], r[i]);
        }
        up[i][0]=bg[i];
        up2[i][0]=sm[i];
    }
    for(ll i=1;i<20;i++){
        for(ll j=1;j<=n;j++){
            if(up[j][i-1]!=inf) up[j][i]=up[up[j][i-1]][i-1];
            else up[j][i]=inf;
        }
    }
    for(ll i=1;i<20;i++){
        for(ll j=1;j<=n;j++){
            if(up2[j][i-1]!=inf) up2[j][i]=up2[up2[j][i-1]][i-1];
            else up2[j][i]=inf;
        }
    }
    //cout<<"initializing\n";
    //for(ll i=1;i<=n;i++){
        //cout<<i<<' '<<l[i]<<' '<<r[i]<<'\n';
    //}
}

int minimum_jumps(int a, int b, int c, int d){
    if(cool) return c-b;
    if(c==d){
        a=h[a];
        c=h[c];
        for(ll i=a;i<=b;i++){
            if(h[i]<c){
                a=max(a, h[i]);
            }
        }
        ll ans=0;
        for(ll i=19;i>=0;i--){
            if(up[a][i]<=c){
                ans+=(1LL<<i);
                a=up[a][i];
            }
        }
        for(ll i=19;i>=0;i--){
            if(up2[a][i]<=c){
                ans+=(1LL<<i);
                a=up2[a][i];
            }
        }
        if(a!=c){
            return -1;
        }
        else{
            return (int) ans;
        }
    } 
    dis=vll(n+1, inf);
    queue<pll> q;
    for(ll i=a;i<=b;i++){
        q.push({h[i], 0});
    }
    while(!q.empty()){
        pll cur=q.front();
        q.pop();
        if(dis[cur.F]!=inf){
            continue;
        }
        dis[cur.F]=cur.S;
        for(auto it:adj[cur.F]){
            q.push({it, cur.S+1});
        }
    }
    ll re=inf;
    for(ll i=c;i<=d;i++){
        re=min(re, dis[h[i]]);
        //cout<<h[i]<<' '<<dis[h[i]]<<'\n';
    }
    if(re==inf) return -1;
    else return (int) re;
}

/*int minimum_jumps(int a, int b, int c, int d){
    if(cool) return c-b;
    dis=vll(n+1, inf);
    queue<pll> q;
    for(ll i=a;i<=b;i++){
        q.push({h[i], 0});
    }
    while(!q.empty()){
        pll cur=q.front();
        q.pop();
        if(dis[cur.F]!=inf){
            continue;
        }
        dis[cur.F]=cur.S;
        for(auto it:adj[cur.F]){
            q.push({it, cur.S+1});
        }
    }
    ll re=inf;
    for(ll i=c;i<=d;i++){
        re=min(re, dis[h[i]]);
        //cout<<h[i]<<' '<<dis[h[i]]<<'\n';
    }
    if(re==inf) return -1;
    else return (int) re;
}*/

/*int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, q;
    n=10;
    q=100;
    vector<int> h(n);
    for(ll i=0;i<n;i++){
        h[i]=i+1;
    }
        random_shuffle(h.begin(), h.end());
        init(n, h);
        for(ll k=0;k<q;k++){
            int a, b, c, d;
            a=rand(0, n-2);
            b=a;
            c=rand(b+1, n-1);
            d=c;
            cout<<a<<' '<<c<<'\n';
            if(minimum_jumps(a, b, c, d)!=minimum_jumps2(a, b, c, d)){
                cout<<"found wrong\n";
                cout<<a<<' '<<c<<'\n';
                for(ll j=0;j<n;j++){
                    cout<<h[j]<<' ';
                }
                cout<<'\n';
                cout<<minimum_jumps(a, b, c, d)<<' '<<
                    minimum_jumps2(a, b, c, d)<<'\n';
            }
        }
    return 0;
}*/

Compilation message (stderr)

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:143:30: error: no matching function for call to 'max(int&, __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type&)'
  143 |                 a=max(a, h[i]);
      |                              ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from jumps.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
jumps.cpp:143:30: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'})
  143 |                 a=max(a, h[i]);
      |                              ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from jumps.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  300 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
jumps.cpp:143:30: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'})
  143 |                 a=max(a, h[i]);
      |                              ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from jumps.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3480 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note:   template argument deduction/substitution failed:
jumps.cpp:143:30: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  143 |                 a=max(a, h[i]);
      |                              ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from jumps.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3486 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note:   template argument deduction/substitution failed:
jumps.cpp:143:30: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  143 |                 a=max(a, h[i]);
      |                              ^