제출 #515192

#제출 시각아이디문제언어결과실행 시간메모리
515192minhcool밀림 점프 (APIO21_jumps)C++17
100 / 100
1430 ms68508 KiB
#include<bits/stdc++.h> using namespace std; //#define int long long #define fi first #define se second #define pb push_back #define mp make_pair #define foru(i, l, r) for(int i = l; i <= r; i++) #define ford(i, r, l) for(int i = r; i >= l; i--) typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef pair<ii, ii> iiii; const int N = 3e5 + 5; const int oo = 1e9 + 7, mod = 1e9 + 7; int n, h[N], lg2[N]; int nxtl[N], nxtr[N]; int small_jump[N][20], large_jump[N][20]; ii mx[N][20]; void init(int _n, vector<int> _H){ n = _n; for(int i = 1; i <= n; i++) h[i] = _H[i - 1]; h[0] = h[n + 1] = oo; stack<int> stk; stk.push(0); for(int i = 1; i <= n; i++) lg2[i] = log2(i); for(int i = 1; i <= n; i++){ while(h[stk.top()] < h[i]) stk.pop(); nxtl[i] = stk.top(); stk.push(i); } //return; while(!stk.empty()) stk.pop(); stk.push(n + 1); for(int i = n; i >= 1; i--){ while(h[stk.top()] < h[i]) stk.pop(); nxtr[i] = stk.top(); stk.push(i); } //return; for(int i = 1; i <= n; i++){ small_jump[i][0] = (h[nxtl[i]] < h[nxtr[i]] ? nxtl[i] : nxtr[i]); large_jump[i][0] = (h[nxtl[i]] > h[nxtr[i]] ? nxtl[i] : nxtr[i]); } for(int i = 1; i <= n; i++) mx[i][0] = {h[i], i}; // cout << "OKAY\n"; for(int i = 1; i <= 19; i++){ for(int j = 1; (j + (1LL << i) - 1) <= n; j++){ mx[j][i] = max(mx[j][i - 1], mx[j + (1LL << (i - 1))][i - 1]); //cout << i << " " << j << " " << mx[j][i].fi << "\n"; } } for(int i = 1; i <= 19; i++){ for(int j = 1; j <= n; j++){ small_jump[j][i] = small_jump[small_jump[j][i - 1]][i - 1]; large_jump[j][i] = large_jump[large_jump[j][i - 1]][i - 1]; } } } ii maxi(int l, int r){ if(l > r) return {-oo, -oo}; //cout << r - l + 1 << " " << lg2[r - l + 1] << "\n"; int k = lg2[r - l + 1]; //cout << mx[l][k].fi << " " << mx[r - (1LL << k) + 1][k].fi << "\n"; return max(mx[l][k], mx[r - (1LL << k) + 1][k]); } int cal(int a, int c){ int answer = 0; for(int i = 19; i >= 0; i--){ if(h[large_jump[a][i]] <= h[c]){ answer += (1LL << i); a = large_jump[a][i]; } } for(int i = 19; i >= 0; i--){ if(h[small_jump[a][i]] <= h[c]){ answer += (1LL << i); a = small_jump[a][i]; } } if(a != c) return oo; else return answer; } int cal2(int a, int b, int c){ //assert(c == d); if(h[b] > h[c]){ return oo; } int l = a, r = b; while(l < r){ int mid = (l + r) >> 1; if(maxi(mid, b).fi > h[c]) l = mid + 1; else r = mid; } //cout << l << " " << b << " " << c << "\n"; int pos = maxi(l, b).se; return cal(pos, c); } int minimum_jumps(int a, int b, int c, int d){ //return -1; a++, b++, c++, d++; //cout << "OK " << a << " " << b << " " << c << " "<< d << "\n"; if(b == (c - 1)){ if(h[b] > maxi(c, d).fi) return -1; else return 1; } ii temp1 = maxi(a, b), temp2 = maxi(b + 1, c - 1), temp3 = maxi(c, d); //cout << temp1.fi << " " << temp2.fi << " " << temp3.fi << "\n"; //cout << b + 1 << " " << c - 1 << " " << temp2.fi << " " << temp2.se << "\n"; if(min(temp1.fi, temp3.fi) > temp2.fi){ //assert(0); int l = a, r = b; while(l < r){ int mid = (l + r + 1) >> 1; if(maxi(mid, b).fi < temp2.fi) r = mid - 1; else l = mid; } //cout << l << "\n"; if(temp3.fi > h[l]) return 1; else a = l + 1; } if(temp3.fi < temp2.fi || a > b) return -1; int answer = oo; answer = min(answer, cal2(a, b, temp2.se) + 1); //cout << answer << "\n"; if(c == d){ //assert(answer >= cal2(a, b, c)); } if(a && maxi(1, a - 1).fi > temp2.fi){ int l = 1, r = a - 1; while(l < r){ int mid = (l + r + 1) >> 1; if(maxi(mid, a - 1).fi < temp2.fi) r = mid - 1; else l = mid; } if(h[l] < temp3.fi) answer = min(answer, cal2(a, b, l) + 1); } if(c == d){ //cout << answer << " " << cal2(a, b, c) << "\n"; assert(answer >= cal2(a, b, c)); } return (answer == oo ? -1 : answer); } /* int _n; vector<int> _h; void process(){ cin >> _n; for(int i = 1; i <= _n; i++){ int x; cin >> x; _h.pb(x); } init(_n, _h); int q; cin >> q; while(q--){ int a, b, c, d; cin >> a >> b >> c >> d; cout << minimum_jumps(a, b, c, d) << "\n"; } } signed main(){ ios_base::sync_with_stdio(0); //freopen("jumps.inp", "r", stdin); //freopen("jumps.out", "w", stdout); process(); } */ /* 7 3 2 1 6 4 5 7 1 4 4 6 6 */
#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...