제출 #417433

#제출 시각아이디문제언어결과실행 시간메모리
417433CSQ31밀림 점프 (APIO21_jumps)C++14
100 / 100
1223 ms51340 KiB
#include "jumps.h" #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) typedef long long int ll; typedef pair<int,int> pii; typedef vector<vector<int>> vii; const int MAXN = 2e5+5; int l[20][MAXN],r[20][MAXN],high[20][MAXN]; vector<int>H; void init(int n, vector<int>h){ H = h; H.insert(H.begin(),1e9); H.pb(1e9); stack<pii>stk; for(int i=0;i<=n+1;i++){ while(!stk.empty() && stk.top().fi <= H[i])stk.pop(); if(!stk.empty())l[0][i] = stk.top().se; else l[0][i] = i; stk.push({H[i],i}); } while(!stk.empty())stk.pop(); for(int i=n+1;i>=0;i--){ while(!stk.empty() && stk.top().fi <= H[i])stk.pop(); if(!stk.empty())r[0][i] = stk.top().se; else r[0][i] = i; stk.push({H[i],i}); } for(int i=0;i<=n+1;i++){ high[0][i] = (H[r[0][i]] > H[l[0][i]])?r[0][i]:l[0][i]; } //cout<<high[0][1]<<'\n'; for(int j=1;j<20;j++){ for(int i=0;i<=n+1;i++){ l[j][i] = l[j-1][l[j-1][i]]; r[j][i] = r[j-1][r[j-1][i]]; high[j][i] = high[j-1][high[j-1][i]]; } } } // int minimum_jumps(int a,int b,int c,int d){ a++; b++; c++; d++; if(c-1 == b){ return r[0][b] <=d?1:-1; } int mid = b+1; //mid just means bigger element in (b,c),cuz we have to cross mid regardless for(int i=19;i>=0;i--)if(r[i][mid] < c)mid = r[i][mid]; if(r[0][mid] > d)return -1; if(H[b] > H[mid]){ return r[0][b] <= d?1:-1; } int cur = b; for(int i=19;i>=0;i--)if(l[i][cur] >= a && H[l[i][cur]] < H[mid])cur = l[i][cur]; //cur is where we should start from int ans= 0; //if(a == 1)cout<<l[0][cur]<<'\n'; if(l[0][cur] >= a){ if(r[0][l[0][cur]] <= d)return 1; //if this case fails it just means we can never ever again go left }else{ for(int i=19;i>=0;i--){ if(H[high[i][cur]] <= H[mid]){ cur = high[i][cur]; ans+=(1<<i); } } //if(a == 1)cout<<cur<<" "<<ans<<'\n'; if(cur == mid)return r[0][mid] <=d?ans+1:-1; else if(l[0][cur] && r[0][l[0][cur]] <= d)return ans+2; } for(int i=19;i>=0;i--){ if(r[i][cur] < c){ cur = r[i][cur]; ans+=(1<<i); } } cur = r[0][cur]; if(c<=cur && cur<=d)return ans+1; else return -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...