Submission #807137

#TimeUsernameProblemLanguageResultExecution timeMemory
807137rnl42Passport (JOI23_passport)C++14
16 / 100
757 ms1048576 KiB
#include <bits/stdc++.h> using namespace std; #define int long long string to_string(string s) { return s; } template <typename T> string to_string(T v) { bool first = true; string res = "["; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "]"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void dbg_out() { cout << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << to_string(H); dbg_out(T...); } #ifdef DEBUG #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif int N, Q; vector<int> L, R; vector<int> X; vector<vector<int>> dp; signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> N; L.resize(N), R.resize(N); vector<vector<vector<int>>> ibyLR(N, vector<vector<int>>(N)); for (int i = 0; i < N; i++) { cin >> L[i] >> R[i], L[i]--, R[i]--; ibyLR[L[i]][R[i]].push_back(i); } cin >> Q; X.resize(Q); for (int i = 0; i < Q; i++) { cin >> X[i], X[i]--; } dp.assign(N, vector<int>(N, 1e18)); dp[0][N-1] = 0; vector<vector<int>> optR(N+1, vector<int>(N+1, 1e18)); for (int r = 0; r < N; r++) { for (int l = r; l >= 0; l--) { if (R[l] <= r) { optR[l][r] = min(optR[l+1][r], L[l]); //dbg(l, r, optR[l][r]); } } } vector<int> optLR(N, 1e18); for (int l = 0; l < N; l++) { vector<int> opt(N+1, -1); vector<int> optLRcumul(N+1, 1e18); for (int r = l; r < N; r++) { if (L[r] >= l) { opt[r+1] = max(opt[r], R[r]); } } for (int r = l; r < N; r++) { optLRcumul[r+1] = min(optLRcumul[r], optLR[r]); } for (int r = N-1; r >= l; r--) { dp[l][r] = min(dp[l][r], optLRcumul[r+1]); if (opt[r+1] != -1) { dp[l][r] = min(dp[l][r], 1+dp[l][opt[r+1]]); } if (optR[l][r] != 1e18) { dp[l][r] = min(dp[l][r], 1+dp[optR[l][r]][r]); } for (int i : ibyLR[l][r]) { optLR[i] = min(optLR[i], 1+dp[l][r]); } } } for (int i = 0; i < Q; i++) { cout << (dp[X[i]][X[i]] >= 1e18 ? -1 : dp[X[i]][X[i]]) << '\n'; } }
#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...