Submission #959477

#TimeUsernameProblemLanguageResultExecution timeMemory
959477KavelmydexRailway Trip (JOI17_railway_trip)C++17
20 / 100
2037 ms13444 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define pi pair<int,int> #define vi vector<int> #define rep(i,x,n) for(int i=x; i<n; ++i) #define For(i,n) rep(i,0,n) #define endl "\n" #define sp ' ' #define pb push_back #define f first #define s second #define sz size() #define all(x) (x).begin(),(x).end() const int N = 1e5+10, OO = 1e18, mod = 1e9+7, mx = 10; void tr(int a, int b){cout << a << sp << b << endl;} void cmx(int &a, int b){a = max(a,b);} void cmn(int &a, int b){a = min(a,b);} const int dx[]{0,0,-1,1}, dy[]{1,-1,0,0}; int a[N], L[N], R[N]; vi adj[N]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); // freopen("talent.in", "r", stdin); // freopen("talent.out", "w", stdout); int n,k,q; cin >> n >> k >> q; rep(i,1,n+1){ cin >> a[i]; } vi lst,rst; rep(i,1,n+1){ if(i==1) lst.pb(i), L[1]=-1; else { while(lst.sz && a[lst.back()] < a[i]) lst.pop_back(); L[i] = lst.sz ? lst.back() : -1; lst.pb(i); } } for(int i=n; i>=1; --i){ if(i==n) rst.pb(i), R[n]=-1; else { while(rst.sz && a[rst.back()] < a[i]) rst.pop_back(); R[i] = rst.sz ? rst.back() : -1; rst.pb(i); } } rep(i,1,n+1){ if(L[i] != -1) adj[i].pb(L[i]), adj[L[i]].pb(i); if(R[i] != -1) adj[i].pb(R[i]), adj[R[i]].pb(i); } while(q--){ int x,y; cin >> x >> y; queue <int> Q; Q.push(x); vi d(n+1,OO); d[x] = 0; while(Q.sz){ int c = Q.front(); Q.pop(); for(int i: adj[c]){ if(d[c]+1 < d[i]){ d[i] = d[c]+1; Q.push(i); } } } cout << d[y]-1 << endl; } return 0; } /* Just some notices : I believe you can do it ! You've done things that were harder ... Stay calm and focused =) */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...