Submission #1052105

#TimeUsernameProblemLanguageResultExecution timeMemory
1052105kachim2Rainforest Jumps (APIO21_jumps)C++17
4 / 100
576 ms19344 KiB
#include "jumps.h" #include<queue> #include <vector> #include <iostream> using namespace std; std::vector<int> H; vector<int> nxt; vector<int> prv; vector<int> d; int segtree[(1<<19)+7]; bool subtask1=1; int mquery(int a, int b){ a--; b++; a+=(1<<18)+1; b+=(1<<18)+1; int res = 0; while(b-a > 1){ if(a%2 == 0){ res = max(segtree[a+1], res); } if(b%2 == 1){ res = max(segtree[b-1], res); } a/=2; b/=2; } return res; } void init(int N, std::vector<int> nH) { for(int i = 0; i < N; i++){ if(nH[i]!=i+1) subtask1=0; } H = nH; nxt.resize(N); prv.resize(N); d.resize(N); { vector<int> kmon; for (int i = 0; i < N; i++) { if (kmon.empty()) { kmon.push_back(i); prv[i] = -1; continue; } while (H[kmon.back()] < H[i]) { kmon.pop_back(); if (kmon.empty()) { kmon.push_back(i); prv[i] = -1; goto nd; } } prv[i] = kmon.back(); kmon.push_back(i); nd:; } } { vector<int> kmon; for (int i = N-1; i>=0; i--) { if (kmon.empty()) { kmon.push_back(i); nxt[i] = -1; continue; } while (H[kmon.back()] < H[i]) { kmon.pop_back(); if (kmon.empty()) { kmon.push_back(i); nxt[i] = -1; goto nd2; } } nxt[i] = kmon.back(); kmon.push_back(i); nd2:; } } vector<vector<int>> graph(N); for(int i = 0; i < N; i++){ if(prv[i]!=-1) graph[prv[i]].push_back(i); if(nxt[i]!=-1) graph[nxt[i]].push_back(i); } int root = 0; for(int i = 1; i < N; i++){ if(H[i] > H[root]) root = i; } queue<pair<int, int>> q; vector<bool> visited(N, 0); q.push({root, 0}); while(!q.empty()){ auto x = q.front(); q.pop(); if(visited[x.first]) continue; visited[x.first] = 1; d[x.first] = x.second; for(auto i : graph[x.first]) q.push({i, x.second+1}); } for(int i = 0; i < N; i++){ segtree[i+(1<<18)+1] = H[i]; } for(int i = 1; i < 1<<18; i++){ segtree[i] = max(segtree[i*2], segtree[i*2+1]); } } int minimum_jumps(int A, int B, int C, int D) { if (subtask1) return C-B; if(A==B && C==D){ if(mquery(A, C-1) > H[C] || H[A] > H[C]) { return -1; } //for(auto i : d) cerr << i << ' '; return d[A]-d[C]; } int mcd = 0; for (int i = C; i <= D; i++) { mcd = max(mcd, H[i]); } int bs = 0; for (int i = B; i >= A; i--) { // cerr << H[i] << ' '; if (H[i] < mcd) bs = max(bs, i); else break; } // cerr << bs << '\n'; vector<bool> visited(H.size(), 0); queue<pair<int, int>> q; for(int i = A; i <=B; i++) q.push({i, 0}); while(!q.empty()){ auto x = q.front(); q.pop(); if(visited[x.first]) continue; visited[x.first] = 1; if(x.first >= C && x.first <= D) return x.second; if(nxt[x.first]!=-1) q.push({nxt[x.first], x.second+1}); if(prv[x.first]!=-1) q.push({prv[x.first], x.second+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...