Submission #784564

#TimeUsernameProblemLanguageResultExecution timeMemory
784564MohamedAliSaidanePassport (JOI23_passport)C++14
16 / 100
2089 ms18264 KiB
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<int,int> pii; #define ff first #define ss second #define pb push_back #define all(x) (x).begin(),(x).end() const int nax = 2e5 + 4; int n, q; vi L, R, X; int sp[nax][20], LG[nax]; void build() { for(int i =0 ; i < n;i++) sp[i][0] = R[i]; //cout << "here" << endl; for(int j = 1; j < 20; j++) { //cout << j << endl; for(int i = 0; i + (1 << (j - 1)) < n;i ++) sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]); } } int RMQ(int l, int r) { int j = LG[r - l + 1]; return max(sp[l][j], sp[r- (1 << j) + 1][j]); } int dp[305][305]; int f(int l, int r) { if(dp[l][r] != -1) return dp[l][r]; if(l == 0 && r == n - 1) return dp[l][r] = 0; int ans = n + 2; for(int j = l; j <= r; j++) { if(L[j] < l || R[j] > r) ans = min(ans, f(min(L[j], l), max(R[j], r)) + 1); } return dp[l][r] = ans; } void solve() { cin >> n; memset(dp, -1, sizeof(dp)); L.resize(n); R.resize(n); for(int i =0; i < n; i++) { cin >> L[i] >> R[i]; L[i]--; R[i]--; } build(); //cout << "here" << endl; cin >> q; while(q--) { int X; cin >> X; X--; int l = L[X], r = R[X]; int ans = f(l, r) + 1; if(ans >= n + 1) cout << "-1\n"; else cout << ans << '\n'; } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); LG[1] = 0; for(int i =2; i < nax; i++) LG[i] = LG[i/2] + 1; int tt = 1; while(tt--) solve(); }
#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...