제출 #1345667

#제출 시각아이디문제언어결과실행 시간메모리
1345667tsengang밀림 점프 (APIO21_jumps)C++20
0 / 100
4025 ms15416 KiB
#include <bits/stdc++.h>
#define ll int
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ertunt return
#define rall(x) x.rbegin(),x.rend()
using namespace std;

const ll N = 200005;
vector<ll>adj[N];
ll n;
ll ngl[N], ngr[N];
struct segtree{
    ll n;
    vector<ll>st;
    segtree(){ }
    segtree(ll _n){ n=_n; st.assign(4*n,0); }
    void build(ll v,ll l,ll r){
        if(l==r){ st[v]=1; ertunt; }
        ll m=(l+r)>>1;
        build(v*2,l,m);
        build(v*2+1,m+1,r);
        st[v]=st[v*2]+st[v*2+1];
    }
    void remove(ll v,ll l,ll r,ll p){
        if(l==r){ st[v]=0; ertunt; }
        ll m=(l+r)>>1;
        if(p<=m) remove(v*2,l,m,p);
        else remove(v*2+1,m+1,r,p);
        st[v]=st[v*2]+st[v*2+1];
    }
    ll getAny(ll v,ll l,ll r,ll L,ll R){
        if(R<l || r<L || st[v]==0) ertunt -1;
        if(l==r) ertunt l;
        ll m=(l+r)>>1;
        ll x=getAny(v*2,l,m,L,R);
        if(x!=-1) ertunt x;
        ertunt getAny(v*2+1,m+1,r,L,R);
    }
};
segtree *gang;
vector<ll> H_vec;
void init(int _n, vector<int> h){
    n=_n;
    H_vec=h;
    stack<ll> st;
    for(ll i=0;i<n;i++){
        while(!st.empty() && h[st.top()]<h[i]) st.pop();
        ngl[i] = st.empty()?-1:st.top();
        st.push(i);
    }
    while(!st.empty()) st.pop();
    for(ll i=n-1;i>=0;i--){
        while(!st.empty() && h[st.top()]<h[i]) st.pop();
        ngr[i] = st.empty()?-1:st.top();
        st.push(i);
    }
    for(ll i=0;i<n;i++){
        adj[i].clear();
        if(ngl[i]!=-1) adj[ngl[i]].pb(i);
        if(ngr[i]!=-1) adj[ngr[i]].pb(i);
    }
    gang=new segtree(n);
    gang->build(1,0,n-1);
}

int minimum_jumps(int A,int B,int C,int D){
    vector<ll> dist(n,-1);
    queue<ll> q;
    for(ll i=C;i<=D;i++){
        dist[i]=0;
        q.push(i);
        gang->remove(1,0,n-1,i);
    }
    while(!q.empty()){
        ll u=q.front(); q.pop();
        if(u>=A && u<=B) ertunt dist[u];
        for(ll v: adj[u]){
            if(dist[v]==-1){
                dist[v]=dist[u]+1;
                gang->remove(1,0,n-1,v);
                q.push(v);
            }
        }
        while(true){
            ll x=gang->getAny(1,0,n-1,A,B);
            if(x==-1) break;
            dist[x]=dist[u]+1;
            gang->remove(1,0,n-1,x);
            q.push(x);
        }
    }
    ertunt -1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...