#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;
struct SegmentTree{
int n;
vector<int> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 2 + 2, INF);
}
void update(int i, int v){
i += n; a[i] = v;
while(i > 1){
i >>= 1;
a[i] = min(a[i * 2], a[i * 2 + 1]);
}
}
int get(int l, int r){
l += n; r += n + 1;
int ans = INF;
while(l < r){
if (l & 1) minimize(ans, a[l++]);
if (r & 1) minimize(ans, a[--r]);
l >>= 1; r >>= 1;
}
return ans;
}
};
int n, q;
int dp[N][N];
int mi[N][N], ma[N][N];
vector<int> pos[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) pos[a[i].first][a[i].second].push_back(i);
for(int i = 1; i <= n; ++i){
int cur_mi = n+1, cur_ma = 0;
for(int j = i; j <= n; ++j){
minimize(cur_mi, a[j].first);
maximize(cur_ma, a[j].second);
mi[i][j] = cur_mi;
ma[i][j] = cur_ma;
}
}
SegmentTree st(n);
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; 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]), j);
pair<int, int> range2 = make_pair(i, max(j, ma[i][j]));
minimize(dp[i][j], 1 + min({dp[range1.first][range1.second], dp[range2.first][range2.second], st.get(i, j)}));
for(int p: pos[i][j]) st.update(p, dp[i][j]);
}
}
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();
// freopen("input.inp", "r", stdin);
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... |