제출 #691574

#제출 시각아이디문제언어결과실행 시간메모리
691574Dan4Life밀림 점프 (APIO21_jumps)C++17
0 / 100
4024 ms62116 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back const int maxn = (int)2e5+10; const int maxlg = 24; const int INF = (int)1e9; int n, h[maxn], pr[maxn], nx[maxn]; int high[maxlg][maxn], low[maxlg][maxn], movR[maxlg][maxn]; void calc(int jmp[][maxn]){ for(int i = 1; i < maxlg; i++) for(int j = 0; j < n; j++) if(jmp[i-1][j]!=-1) jmp[i][j] = jmp[i-1][jmp[i-1][j]]; } void init(int N, vector<int> a) { n = N; stack<pair<int,int>> S; memset(pr,-1,sizeof(pr)); memset(nx,-1,sizeof(nx)); memset(low,-1,sizeof(low)); memset(high,-1,sizeof(high)); memset(movR,-1,sizeof(movR)); for(int i = 0; i < (int)a.size(); i++) h[i]=a[i]; for(int i = 0; i < n; i++){ while(!S.empty() and S.top().fi<a[i]) S.pop(); if(!S.empty()) pr[i] = S.top().se; S.push({a[i],i}); } while(!S.empty()) S.pop(); for(int i = n-1; i>=0; i--){ while(!S.empty() and S.top().fi<a[i]) S.pop(); if(!S.empty()) nx[i] = S.top().se; S.push({a[i],i}); } while(!S.empty()) S.pop(); for(int i = 0; i < n; i++){ if(nx[i]!=-1) movR[0][i]=nx[i]; if(pr[i]!=-1 and nx[i]!=-1){ bool ok=0; if(a[pr[i]]<a[nx[i]]) swap(pr[i],nx[i]),ok=1; high[0][i] = pr[i]; low[0][i] = nx[i]; if(ok) swap(pr[i],nx[i]); } else{ if(pr[i]!=-1) low[0][i] = pr[i]; else low[0][i] = nx[i]; } } calc(high), calc(low), calc(movR); } int get_path(int *x, int y, int z, int jmp[][maxn], int range=maxn){ int tot = 0; range = min(range, y); for(int i = 20; i >= 0; i--) if(jmp[i][*x]!=-1 and jmp[i][*x]<range) if(nx[jmp[i][*x]]!=-1 and nx[jmp[i][*x]]<=z) *x = jmp[i][*x], tot+=(1<<i); return tot; } int minimum_jumps(int A, int B, int C, int D) { int x = A; for(int i = B; i >= A; i--){ if(nx[i]==-1 or nx[i]>D) continue; x=i; break; } for(int i = A; i <= B; i++) if(nx[i]!=-1 and nx[i]<=D) x=i; int num1 = get_path(&x,C,D,high); int num3 = get_path(&x,C,D,low); int num2 = get_path(&x,C,D,movR); return ((nx[x]>=C and nx[x]<=D)?num1+num2+num3+1:-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...