Submission #916164

#TimeUsernameProblemLanguageResultExecution timeMemory
916164KiaRezRainforest Jumps (APIO21_jumps)C++17
0 / 100
167 ms60880 KiB
/* IN THE NAME OF GOD */ #include <bits/stdc++.h> // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef long double ld; #define F first #define S second #define Mp make_pair #define pb push_back #define pf push_front #define size(x) ((ll)x.size()) #define all(x) (x).begin(),(x).end() #define kill(x) cout << x << '\n', exit(0); #define fuck(x) cout << "(" << #x << " , " << x << ")" << endl #define endl '\n' const int N = 2e5+23, lg = 18; int n, l[N], r[N], f[lg][N], f2[lg][N], spl[lg][N], spr[lg][N], h[N]; void init(int _n, vector<int> _h) { n=_n; for(int i=1; i<=n; i++) h[i] = _h[i-1]; h[0] = 1e9+1, h[n+1] = 1e9+2; for(int i=1; i<=n; i++) { l[i] = i-1; while(h[i] > h[l[i]]) l[i] = l[l[i]]; spl[0][i] = i; for(int j=1; j<lg; j++) { int x=spl[j-1][i], y=spl[j-1][max(0,i-(1<<(j-1)))]; spl[j][i] = (h[x]>h[y] ? x : y); } } for(int i=0; i<lg; i++) f2[i][n+1] = f[i][n+1] = spr[i][n+1] = n+1; for(int i=n; i>=1; i--) { r[i] = i+1; while(h[i] > h[r[i]]) r[i] = r[r[i]]; if(h[r[i]] > h[l[i]]) f[0][i] = r[i], f2[0][i] = l[i]; else f[0][i] = l[i], f2[0][i] = r[i]; spr[0][i] = i; for(int j=1; j<lg; j++) { int x=spr[j-1][i], y=spr[j-1][min(n+1,i+(1<<(j-1)))]; spr[j][i] = (h[x]>h[y] ? x : y); } } for(int i=1; i<lg; i++) { for(int j=1; j<=n; j++) { f[i][j] = f[i-1][f[i-1][j]]; f2[i][j] = f2[i-1][f2[i-1][j]]; } } } int minimum_jumps(int a, int b, int c, int d) { a++, b++, c++, d++; int mxv=n+2, mxu=b, tmp=c, ans=0; for(int i=lg-1; i>=0; i--) { if(tmp+(1<<i)-1 <= d) mxv = (h[mxv]>h[spr[i][tmp]] ? mxv : spr[i][tmp]), tmp += (1<<i); } tmp = b; for(int i=lg-1; i>=0; i--) { if(tmp-(1<<i)+1 >= a && h[spl[i][tmp]] < h[mxv]) { mxu = (h[mxu] > spl[i][tmp] ? mxu : spl[i][tmp]); tmp -= (1<<i); } } for(int i=lg-1; i>=0; i--) { if(h[f[i][mxu]] < h[mxv]) ans += (1<<i), mxu = f[i][mxu]; } for(int i=lg-1; i>=0; i--) { if(h[f2[i][mxu]] < h[mxv]) mxu = f2[i][mxu], ans += (1<<i); } int res = f2[0][mxu]; if(res>=c && res<=d) { return ans+1; } 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...