제출 #430800

#제출 시각아이디문제언어결과실행 시간메모리
430800Supersonic밀림 점프 (APIO21_jumps)C++14
100 / 100
1969 ms42804 KiB
#include "jumps.h" #include <bits/stdc++.h> using namespace std; int n;vector<int> v; #define SIZE 262144 typedef long long ll; vector<int> l;vector<int> r;vector<int> t;int y[200001];int pa; int jm[200001][21];int jr[200001][21]; ll tree[2*SIZE]; int m1=-1,m1i=-1,m2=-1; int p2[18]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072}; int g(int a,int p){ if(a==-1||a+p2[p]>=n)return -1; if(jm[a][p]>=0)return jm[a][p]; if(p==0){if(l[a]==-1)jm[a][p]=r[a];else if(r[a]==-1)jm[a][p]=l[a];if(v[l[a]]>v[r[a]])jm[a][p]=l[a];else jm[a][p]=r[a];return jm[a][p];} jm[a][p]=g(g(a,p-1),p-1);; return jm[a][p]; } int gr(int a,int p){ if(a==-1||a+p2[p]>=n)return -1; if(jr[a][p]>=0)return jr[a][p]; if(p==0){jr[a][p]=r[a];return jr[a][p];} jr[a][p]=gr(gr(a,p-1),p-1);; return jr[a][p]; } void u(int pos, int val) { pos += SIZE; tree[pos]=val; for (pos /= 2; pos >= 1; pos /= 2){ tree[pos] = max(tree[2*pos], tree[2*pos+1]); } } ll q(int a, int b) { a += SIZE; b += SIZE; ll s = 0; while (a <= b) { if (a%2 == 1) s = max(s, tree[a++]); if (b%2 == 0) s = max(s, tree[b--]); a /= 2; b /= 2; } return s; } void init(int N, std::vector<int> H) { //if(N==176)exit(1); n=N;v=H; l.push_back(-1);t.push_back(0); for(int i=0;i<n;i++){u(i,H[i]);y[H[i]]=i;for(int j=0;j<21;j++){jm[i][j]=-1;jr[i][j]=-1;}} for(int i=1;i<n;i++){ //for(auto k:t)cerr<<k<<' ';cerr<<endl; while(t.size()>0&&v[i]>v[t.back()])t.pop_back(); if(t.size()==0)l.push_back(-1); else l.push_back(t.back()); t.push_back(i); } reverse(v.begin(),v.end());t.clear(); r.push_back(-1);t.push_back(0); for(int i=1;i<n;i++){ while(t.size()>0&&v[i]>v[t.back()])t.pop_back(); if(t.size()==0)r.push_back(-1); else r.push_back(t.back()); t.push_back(i); } reverse(v.begin(),v.end());reverse(r.begin(),r.end());for(int i=0;i<n;i++)if(r[i]!=-1)r[i]=(n-1)-r[i]; //cerr<<l[1]<<endl; //cout<<"DBG"<<endl;int td;cin>>td;while(td--){int ta,tb;cin>>ta>>tb;cout<<g(ta,tb)<<endl;} } int mj(int a,int c,int d,int p){ //p=16; //pa++; //cerr<<a<<' '<<v[a]<<" | "; if((c<=l[a]&&l[a]<=d)||(c<=r[a]&&r[a]<=d))return 1; if(v[l[a]]>m2){ p=16; while(p>=0&&(gr(a,p)==-1||r[jr[a][p]]==-1||r[jr[a][p]]>=c))p--; if(p==-1){ if(r[a]!=-1&&r[a]<=d&&v[r[a]]<m2)return 1+mj(r[a],c,d,0); return -1*1e8; } return p2[p]+mj(jr[a][p],c,d,p); } while(p>=0&&(g(a,p)==-1||r[jm[a][p]]==-1||r[jm[a][p]]>=c))p--; if(p==-1){ if(r[a]!=-1&&l[a]!=-1&&v[r[a]]>v[l[a]]&&v[r[a]]<m2&&r[a]<=d)return 1+mj(r[a],c,d,0); if(l[a]!=-1&&l[a]<=d&&v[l[a]]<m2)return 1+mj(l[a],c,d,0); if(r[a]!=-1&&r[a]<=d&&v[r[a]]<m2)return 1+mj(r[a],c,d,0); return -1*1e8; } //cerr<<a<<' '<<p<<' '<<r[g(a,p)]<<endl; return p2[p]+mj(jm[a][p],c,d,p); } int minimum_jumps(int a, int b, int c, int d) { //cerr<<v[a]<<' '<<v[b]<<' '<<v[c]<<' '<<v[d]<<endl; m2=q(c,d);pa=0; int x=a-1; for (int e=p2[15];e>=1;e/=2) { while(q(x+e,b)>m2)x+=e; } x++;if(x>b)return -1; m1=q(x,b);m1i=y[m1]; int tj=max(mj(m1i,c,d,16),-1);//cerr<<pa<<' '; return tj; }
#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...