제출 #489115

#제출 시각아이디문제언어결과실행 시간메모리
489115cfalas밀림 점프 (APIO21_jumps)C++14
100 / 100
1674 ms104640 KiB
#include "jumps.h" #include<bits/stdc++.h> using namespace std; #define mp make_pair #define INF 10000000 #define MOD 1000000007 #define MID ((l+r)/2) #define HASHMOD 2305843009213693951 #define ll long long #define ull unsigned long long #define F first #define S second typedef pair<ll, ll> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef map<int, int> mii; #define EPS 1e-6 #define FOR(i,n) for(int i=0;i<((int)(n));i++) #define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++) #define FOA(v, a) for(auto v : a) int t, n; vi a, b; vi nl, pl; struct nodemax{ nodemax* L=NULL, *R=NULL; int val=-1; int valp=0; void update(int x, int v, int l=0, int r=n-1){ if(l==r && l==x){ val = v; valp = l; //cout<<l<<" "<<r<<" "<<val<<endl; } else if(l==r || l>x || r<x) return; else{ if(!L) L = new nodemax(); if(!R) R = new nodemax(); val = -1; L->update(x, v, l, MID), val = max(val, L->val); R->update(x,v, MID+1,r), val = max(val, R->val); if(L->val > R->val) valp = L->valp; else valp = R->valp; //cout<<l<<" "<<r<<" "<<val<<endl; } } int query(int rl, int rr, int l=0, int r=n-1){ if(rl<=l && r<=rr) return val; else if(rl>r || l>rr) return -1; else { if(!L) L = new nodemax(); if(!R) R = new nodemax(); return max(L->query(rl, rr, l, MID), R->query(rl,rr,MID+1,r)); } } ii queryp(int rl, int rr, int l=0, int r=n-1){ if(rl<=l && r<=rr) return {val, valp}; else if(rl>r || l>rr) return {-1, 0}; else { if(!L) L = new nodemax(); if(!R) R = new nodemax(); return max(L->queryp(rl, rr, l, MID), R->queryp(rl,rr,MID+1,r)); } } }; struct node{ node* L=NULL, *R=NULL; int val=n; void update(int x, int v, int l=0, int r=n-1){ if(l==r && l==x){ val = v; //cout<<l<<" "<<r<<" "<<val<<endl; } else if(l==r || l>x || r<x) return; else{ if(!L) L = new node(); if(!R) R = new node(); val = n; L->update(x, v, l, MID), val = min(val, L->val); R->update(x,v, MID+1,r), val = min(val, R->val); //cout<<l<<" "<<r<<" "<<val<<endl; } } int query(int rl, int rr, int l=0, int r=n-1){ if(rl<=l && r<=rr) return val; else if(rl>r || l>rr) return n; else { if(!L) L = new node(); if(!R) R = new node(); return min(L->query(rl, rr, l, MID), R->query(rl,rr,MID+1,r)); } } }; node seg; nodemax segm; nodemax all; int hi[201000][20]; int lo[201000][20]; int le[201000][20]; int ri[201000][20]; void init(int N, std::vector<int> H) { n = N; a.resize(n); nl.resize(n+1); pl.resize(n+1); FOR(i,n) a[i] = H[i], all.update(i, a[i]); FORi(i,1,n+1){ nl[n-i] = seg.query(a[n-i], n-1); seg.update(a[n-i]-1, n-i); } nl[n] = n; FOR(i,n){ pl[i] = segm.query(a[i], n-1); segm.update(a[i]-1, i); } pl[n] = n; FOR(i,n){ if(nl[i]!=n && pl[i]!=-1){ if(a[nl[i]]>a[pl[i]]) hi[i][0] = nl[i], lo[i][0] = pl[i]; else hi[i][0] = pl[i], lo[i][0] = nl[i]; } else{ hi[i][0] = n; lo[i][0] = n; if(nl[i]!=n) lo[i][0] = nl[i]; else if(pl[i]!=-1) lo[i][0] = pl[i]; } le[i][0] = pl[i]; ri[i][0] = nl[i]; if(le[i][0] == -1) le[i][0] = n; if(ri[i][0] == -1) ri[i][0] = n; } lo[n][0] = n; hi[n][0] = n; le[n][0] = n; ri[n][0] = n; //FOR(i,n) cout<<hi[i][0]<<" "; cout<<" | "; FOR(i,n) cout<<lo[i][0]<<" "; cout<<endl; FORi(j,1,20){ FOR(i,n+1){ hi[i][j] = hi[hi[i][j-1]][j-1]; lo[i][j] = lo[lo[i][j-1]][j-1]; le[i][j] = le[le[i][j-1]][j-1]; ri[i][j] = ri[ri[i][j-1]][j-1]; //cout<<hi[i][j]<<" "; } //cout<<endl; } //cout<<endl; } int minimum_jumps(int A, int B, int C, int D) { int ans=0; int s = B; int larg = all.query(C, D); int larg2 = all.query(B+1, C-1); //cout<<larg<<" "<<larg2<<endl; if(larg2>larg) return -1; if(a[s]>larg) return -1; for(int i=18;i>=0;i--){ if(le[s][i]!=n && le[s][i]>=A && a[le[s][i]]<=larg) s = le[s][i]; } //cout<<s<<endl; for(int i=18;i>=0;i--){ if(hi[s][i]!=n && (a[hi[s][i]]<larg2)) s = hi[s][i], ans+=(1<<i); if(s>=C) break; } //cout<<s<<endl; if(a[s]<larg2 && hi[s][0]!=n && (a[hi[s][0]]<=larg)) s = hi[s][0], ans++; //cout<<s<<endl; for(int i=18;i>=0;i--){ if(ri[s][i]!=n && (a[ri[s][i]]<larg2)) s = ri[s][i], ans+=(1<<i); if(s>=C) break; } //cout<<s<<endl; if(ri[s][0]!=n && a[s]<larg2 && (a[ri[s][0]]<=larg)) s = ri[s][0], ans++; //cout<<s<<endl; //cout<<s<<endl; if(s>=C && s<=D) return ans; if(ri[s][0]>=C && ri[s][0]<=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...