#include <cstdio>
#include <random>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <chrono>
#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
// вправо, вниз, влево, вверх
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
// ход конем
//int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
//int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
mt19937_64 rnd64(chrono::system_clock::now().time_since_epoch().count());
int rndInteger (int l, int r) {
return uniform_int_distribution<int> (l, r)(rnd);
}
const int MOD = (int) 998244353;
const int N = (int) 1e6 + 7;
int n, q, l[N], r[N];
int ans[N], x[N];
int solve(int L, int R, bool rev = false) {
if(rev) {
for (int i = 1; i <= n; ++i) {
l[i] = n - l[i] + 1;
r[i] = n - r[i] + 1;
swap(l[i], r[i]);
}
}
int res = 1;
while(R < n) {
int max_r = R;
for (int i = L; i <= R; ++i)
max_r = max(max_r, r[i]);
if(max_r == R) {
return 1e9;
}
++res;
R = max_r;
}
if(L > 1) {
L = n;
for (int i = 1; i <= n; ++i)
if(r[i] == n)
L = min(L, l[i]);
while(L > 1) {
int min_l = L;
for (int i = L; i <= n; ++i)
min_l = max(min_l, l[i]);
if(min_l == L)
return 1e9;
cout << "change: " << L << " -> " << min_l << endl;
++res;
L = min_l;
}
}
if(rev) {
for (int i = 1; i <= n; ++i) {
l[i] = n - l[i] + 1;
r[i] = n - r[i] + 1;
swap(l[i], r[i]);
}
}
return res;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> l[i] >> r[i];
cin >> q;
for (int i = 1; i <= q; ++i) {
cin >> x[i];
ans[i] = 1e9;
ans[i] = min(ans[i], solve(l[x[i]], r[x[i]]));
ans[i] = min(ans[i], solve(n - r[x[i]] + 1, n - l[x[i]] + 1, 1));
}
for (int i = 1; i <= q; ++i) {
if(ans[i] > n) ans[i] = -1;
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |