#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(abs(a), abs(b));}
ll lcm(ll a, ll b){return abs(a) / gcd(a, b) * abs(b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 2525, INF = 1e9 + 69;
int n, q;
int dp[N][N];
pair<int, int> mi[N][N], ma[N][N];
template<class T1, class T2>
ostream& operator << (ostream &out, pair<T1, T2> x){
return out << "(" << x.first << "," << x.second << ")";
}
void solve(){
int n; cin >> n;
vector<pair<int, int>> a(n+1);
for(int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second;
for(int i = 1; i <= n; ++i){
pair<int, int> cur_mi = make_pair(n+1,n+1), cur_ma = make_pair(0, 0);
for(int j = i; j <= n; ++j){
if (a[j].first < cur_mi.first) cur_mi = a[j];
else if (a[j].first == cur_mi.first) maximize(cur_mi.second, a[j].second);
if (a[j].second > cur_ma.second) cur_ma = a[j];
else if (a[j].second == cur_ma.second) minimize(cur_ma.first, a[j].first);
mi[i][j] = cur_mi;
ma[i][j] = cur_ma;
}
}
for(int i = 1; i <= n; ++i) for(int j = i; j <= n; ++j) dp[i][j] = INF;
dp[1][n] = 0;
for(int len = n-1; len >= 1; --len){
for(int i = 1; i + len - 1 <= n; ++i){
int j = i + len - 1;
pair<int, int> range1 = make_pair(min(i, mi[i][j].first), max(j, mi[i][j].second));
pair<int, int> range2 = make_pair(min(i, ma[i][j].first), max(j, ma[i][j].second));
minimize(dp[i][j], 1 + min(dp[range1.first][range1.second], dp[range2.first][range2.second]));
}
}
int q; cin >> q;
while(q--){
int x; cin >> x;
if (dp[x][x] == INF) cout << -1 << "\n";
else cout << dp[x][x] << "\n";
}
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |