제출 #541968

#제출 시각아이디문제언어결과실행 시간메모리
541968aryan12밀림 점프 (APIO21_jumps)C++17
69 / 100
1089 ms48572 KiB
#include "jumps.h" #include <vector> using namespace std; #include <bits/stdc++.h> const int MAXN = 2e5 + 5; int DP_Min[19][MAXN], DP_Max[19][MAXN]; int seg[MAXN * 4]; vector<int> INPUT; vector<int> g[MAXN]; vector<int> omk(MAXN); int okN; void Build(int l, int r, int pos) { if(l == r) { seg[pos] = INPUT[l - 1]; return; } int mid = (l + r) >> 1; Build(l, mid, pos * 2); Build(mid + 1, r, pos * 2 + 1); seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]); } pair<int, int> Query(int l, int r, int pos, int ql, int qr, int qval) { if(ql > r || l > qr) { //cout << "l = " << l << ", r = " << r << ", there {-1, -1}\n"; return {0, 0}; } if(l == r) { if(seg[pos] >= qval) { //cout << "l = " << l << ", r = " << r << ", here {-1, -1}\n"; return {-1, -1}; } //cout << "l = " << l << ", r = " << r << ", {" << seg[pos] << ", " << seg[pos] << "}\n"; return {seg[pos], seg[pos]}; } if(ql <= l && r <= qr && seg[pos] < qval) { return {seg[pos], seg[pos]}; } int mid = (l + r) >> 1; pair<int, int> x = Query(mid + 1, r, pos * 2 + 1, ql, qr, qval); if(x.second == -1) { //cout << "l = " << l << ", r = " << r << ", aftermid {" << x.first << ", -1}\n"; return x; } pair<int, int> y = Query(l, mid, pos * 2, ql, qr, qval); if(y.second == -1) { //cout << "l = " << l << ", r = " << r << ", {" << max(y.first, x.first) << ", " << y.second << "}\n"; return {max(y.first, x.first), -1}; } //cout << "l = " << l << ", r = " << r << ", {" << max(y.first, x.first) << ", " << max(y.second, x.second) << "}\n"; return {max(y.first, x.first), max(y.second, x.second)}; } void init(int N, vector<int> H) { okN = N; vector<int> L(N), R(N); stack<int> q; for(int i = 0; i < N; i++) { omk[H[i]] = i; INPUT.push_back(H[i]); while(!q.empty() && H[q.top()] <= H[i]) { q.pop(); } if(q.empty()) { L[i] = -1; } else { L[i] = q.top(); } q.push(i); } Build(1, N, 1); while(!q.empty()) { q.pop(); } for(int i = N - 1; i >= 0; i--) { while(!q.empty() && H[q.top()] <= H[i]) { q.pop(); } if(q.empty()) { R[i] = N; } else { R[i] = q.top(); } q.push(i); } for(int i = 0; i < N; i++) { if(L[i] == -1 && R[i] == N) { DP_Min[0][i] = -1; DP_Max[0][i] = -1; } else if(L[i] == -1 || R[i] == N) { if(L[i] == -1) { DP_Min[0][i] = R[i]; DP_Max[0][i] = R[i]; } else { DP_Min[0][i] = L[i]; DP_Max[0][i] = L[i]; } } else { if(H[L[i]] < H[R[i]]) { DP_Min[0][i] = L[i]; DP_Max[0][i] = R[i]; } else { DP_Min[0][i] = R[i]; DP_Max[0][i] = L[i]; } //DP_Min[0][i] = min(L[i], R[i]); //DP_Max[0][i] = max(L[i], R[i]); } } for(int i = 1; i < 19; i++) { for(int j = 0; j < N; j++) { if(DP_Min[i - 1][j] == -1) { DP_Min[i][j] = -1; } else { DP_Min[i][j] = DP_Min[i - 1][DP_Min[i - 1][j]]; } if(DP_Max[i - 1][j] == -1) { DP_Max[i][j] = -1; } else { DP_Max[i][j] = DP_Max[i - 1][DP_Max[i - 1][j]]; } } } for(int i = 0; i < N; i++) { if(L[i] != -1) { g[i].push_back(L[i]); } if(R[i] != -1) { g[i].push_back(R[i]); } } } int minimum_jumps(int A, int B, int C, int D) { if(okN <= 2000) { queue<int> q; vector<int> dist(MAXN, 1e9); for(int i = A; i <= B; i++) { q.push(i); dist[i] = 0; } while(!q.empty()) { int node = q.front(); q.pop(); for(int i = 0; i < g[node].size(); i++) { if(dist[g[node][i]] == 1e9) { dist[g[node][i]] = dist[node] + 1; q.push(g[node][i]); } } } int ans = 1e9; for(int i = C; i <= D; i++) { ans = min(ans, dist[i]); } if(ans == 1e9) { return -1; } return ans; } A++; B++; //cout << "A = " << A << ", B = " << B << ", INPUT[C] = " << INPUT[C] << "\n"; int pos = Query(1, okN, 1, A, B, INPUT[C]).first; A = omk[pos]; B = omk[pos]; //cout << "A = " << A << ", B = " << B << "\n"; int ans = 0, cur = B; for(int i = 18; i >= 0; i--) { if(DP_Max[i][cur] != -1 && INPUT[DP_Max[i][cur]] <= INPUT[C]) { //cout << "prev cur = " << cur << "\n"; ans += (1 << i); cur = DP_Max[i][cur]; //cout << "new cur = " << cur << ", with added answer: " << (1 << i) << "\n"; } } for(int i = 18; i >= 0; i--) { if(DP_Min[i][cur] != -1 && INPUT[DP_Min[i][cur]] <= INPUT[C]) { ans += (1 << i); cur = DP_Min[i][cur]; } } if(cur != C) return -1; return ans; }

컴파일 시 표준 에러 (stderr) 메시지

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:155:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |       for(int i = 0; i < g[node].size(); i++) {
      |                      ~~^~~~~~~~~~~~~~~~
#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...