Submission #417345

#TimeUsernameProblemLanguageResultExecution timeMemory
417345CSQ31밀림 점프 (APIO21_jumps)C++14
0 / 100
259 ms40364 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; stack<pii>stk; for(int i=0;i<n;i++){ while(!stk.empty() && stk.top().fi < h[i])stk.pop(); if(!stk.empty())l[0][i+1] = stk.top().se+1; 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+1] = stk.top().se+1; else r[0][i+1] = n+1; stk.push({h[i],i}); } r[0][n+1] = n+1; H.insert(H.begin(),0); for(int i=1;i<=n;i++){ high[0][i] = (H[r[0][i]] > H[l[0][i]])?r[0][i]:l[0][i]; } for(int j=1;j<20;j++){ for(int i=1;i<=n;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 == b+1){ if(r[0][b] > d)return -1; return 1; } int mid = b+1; for(int j=19;j>=0;j--){ if(r[j][mid] < c){ mid = r[j][mid]; } } if(H[b] > H[mid])return r[0][b]<=d?1:-1; //anything higher this mid point can be possibly "unreachable" //otherwise we are safe to use high edges int cur = b; for(int j=19;j>=0;j--){ if(l[j][cur] >= a && H[l[j][cur]] < H[mid])cur = l[j][cur]; } //we want to start at cur if(l[0][cur] >= a && r[0][l[0][cur]] <= d)return 1; //if l[0][cur] reachable we take it,else we can never go left again int ans = 0; for(int j=19;j>=0;j--){ if(H[high[j][cur]] <= H[mid]){ ans+=(1<<j); cur = high[j][cur]; } } if(cur == mid){ if(r[0][mid] <= d)return ans+1; else return -1; }else{ //since we cant jump right without >= h[mid] //we check if jumping left is reachable if(l[0][cur] && r[0][l[0][cur]] <= d)return ans+2; } //we cant jump to the left anymore : ( for(int j=19;j>=0;j--){ if(r[j][cur] < c){ cur = r[j][cur]; ans+=(1<<j); } } if(r[0][cur] <= d && r[0][cur] >= c)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...