Submission #983660

#TimeUsernameProblemLanguageResultExecution timeMemory
983660shjeongRainforest Jumps (APIO21_jumps)C++17
4 / 100
1380 ms227860 KiB
#include "jumps.h" #include <iostream> #include <cmath> #include <algorithm> #include <numeric> #include <cstring> #include <vector> #include <string> #include <climits> #include <map> #include <set> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <list> using namespace std; typedef long long ll; ll n; struct segtree{ vector<pair<ll,ll>> tree; segtree(): tree(808080){} void init(ll node, ll s, ll e, vector<ll> &v){ if(s==e)tree[node] = {v[s],s}; else{ ll mid = s+e>>1; init(node<<1,s,mid,v); init(node<<1|1,mid+1,e,v); tree[node] = max(tree[node<<1],tree[node<<1|1]); } } pair<ll,ll> query(ll node, ll s, ll e, ll l, ll r){ if(l>r)return {-1,-1}; if(e<l or r<s)return {0,0}; if(l<=s and e<=r)return tree[node]; ll mid = s+e>>1; return max(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r)); } pair<ll,ll> Q(ll l, ll r){ return query(1,1,n,l,r); } } seg; vector<ll> v, Rv; ll L[202020][22]; ll R[202020][22]; ll M[202020][22]; ll RL[202020][22]; ll RR[202020][22]; ll RM[202020][22]; void init(int N, std::vector<int> H) { n=N; v.push_back(0); Rv=v; for(int i = 1 ; i <= n ; i++)v.push_back(H[i-1]), Rv.push_back(H[n-i]); seg.init(1,1,n,v); stack<ll> st; for(int i = 1 ; i <= n ; i++){ while(st.size() and v[st.top()] < v[i]){ R[st.top()][0] = i; st.pop(); } st.push(i); } while(st.size())st.pop(); for(int i = n ; i >= 1 ; i--){ while(st.size() and v[st.top()] < v[i]){ L[st.top()][0] = i; st.pop(); } st.push(i); } for(int i = 1 ; i <= n ; i++){ M[i][0] = (v[L[i][0]] > v[R[i][0]] ? L[i][0] : R[i][0]); } for(int i = 1 ; i <= n ; i++){ RL[i][0] = L[n-i+1][0]; RR[i][0] = R[n-i+1][0]; RM[i][0] = M[n-i+1][0]; } for(int j = 1 ; j <= 20 ; j++){ for(int i = 1 ; i <= n ; i++){ L[i][j] = L[L[i][j-1]][j-1]; R[i][j] = R[R[i][j-1]][j-1]; M[i][j] = M[M[i][j-1]][j-1]; } for(int i = 1 ; i <= n ; i++){ RL[i][j] = L[n-i+1][j]; RR[i][j] = R[n-i+1][j]; RM[i][j] = M[n-i+1][j]; } } } ll AB, BC, CD; ll c; ll solve_terminal(ll A, ll B, ll C){ ll D=C; ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first; ll ret = 0; ll cur = B; for(int i = 20 ; i >= 0 ; i--){ if(L[cur][i]<=0)continue; if(L[cur][i]>=A and v[L[cur][i]] < CD)cur = L[cur][i]; } for(int i = 20 ; i >= 0 ; i--){ if(M[cur][i]<=0)continue; if(v[M[cur][i]]<CD and M[cur][i]<C)cur = M[cur][i], ret += (1<<i); } for(int i = 20 ; i >= 0 ; i--){ if(R[cur][i]<=0)continue; if(R[cur][i]<C)cur = R[cur][i], ret += (1<<i); } ret++; cur = R[cur][0]; if(cur<C or cur>D)ret = 1e9; return ret; } ll solve_terminal_reverse(ll A, ll B, ll C){ ll D=C; ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first; ll ret = 0; ll cur = B; for(int i = 20 ; i >= 0 ; i--){ if(RL[cur][i]<=0)continue; if(RL[cur][i]>=A and Rv[RL[cur][i]] < CD)cur = RL[cur][i]; } for(int i = 20 ; i >= 0 ; i--){ if(RM[cur][i]<=0)continue; if(Rv[RM[cur][i]]<CD and RM[cur][i]<C)cur = RM[cur][i], ret += (1<<i); } for(int i = 20 ; i >= 0 ; i--){ if(RR[cur][i]<=0)continue; if(RR[cur][i]<C)cur = RR[cur][i], ret += (1<<i); } ret++; cur = RR[cur][0]; if(cur<C or cur>D)ret = 1e9; return ret; } int minimum_jumps(int A, int B, int C, int D) { A++; B++; C++; D++; ll cur = B; AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first; ll ret = 1e9; if(AB>BC){ ll cur = B; for(int i = 20 ; i >= 0 ; i--){ if(L[cur][i]<=0)continue; if(v[L[cur][i]]<BC)cur = L[cur][i]; } if(v[cur]<BC)cur = L[cur][0]; cur = R[cur][0]; if(C<=cur and cur<=D)return 1; } if(BC>0){ ll pos = seg.Q(B+1,C-1).second; ret = min(ret, solve_terminal(A,B,pos)+1); } if(seg.Q(1,A-1).first > BC){ ll cur = A-1; for(int i = 20 ; i >= 0 ; i--){ if(L[cur][i]<=0)continue; if(v[L[cur][i]]<BC)cur = L[cur][i]; } if(v[cur]<BC)cur = L[cur][0]; ret = min(ret, solve_terminal_reverse(n-B+1, n-A+1, n-cur+1)); } if(ret==1e9)ret = -1; return ret; }

Compilation message (stderr)

jumps.cpp: In member function 'void segtree::init(ll, ll, ll, std::vector<long long int>&)':
jumps.cpp:26:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |             ll mid = s+e>>1;
      |                      ~^~
jumps.cpp: In member function 'std::pair<long long int, long long int> segtree::query(ll, ll, ll, ll, ll)':
jumps.cpp:35:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |         ll mid = s+e>>1;
      |                  ~^~
jumps.cpp: In function 'll solve_terminal(ll, ll, ll)':
jumps.cpp:93:8: warning: unused variable 'AB' [-Wunused-variable]
   93 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |        ^~
jumps.cpp:93:31: warning: unused variable 'BC' [-Wunused-variable]
   93 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |                               ^~
jumps.cpp: In function 'll solve_terminal_reverse(ll, ll, ll)':
jumps.cpp:114:8: warning: unused variable 'AB' [-Wunused-variable]
  114 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |        ^~
jumps.cpp:114:31: warning: unused variable 'BC' [-Wunused-variable]
  114 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |                               ^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:135:8: warning: unused variable 'cur' [-Wunused-variable]
  135 |     ll cur = B;
      |        ^~~
#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...