#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
// global
const int mxN = 2e5 + 1;
pii range[mxN];
int ans[mxN];
int n;
void init() {
// init
for (int i = 1; i < mxN; i++) ans[i] = -1;
}
void st5() {
}
void st4() {
}
int st3dp[2501][2501];
int st3(int &tar) {
if (range[tar].ff == 1 && range[tar].ss == n) {
return 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
st3dp[i][j] = 1e9;
}
}
queue<pii> q;
q.push({tar, tar});
st3dp[tar][tar] = 1;
int nl, nr;
while (!q.empty()) {
pii cur = q.front(); q.pop();
for (int i = range[cur.ff].ff; i < cur.ff; i++) {
if (range[cur.ff].ff <= range[i].ff && range[i].ss <= range[cur.ss].ss) continue;
nl = (range[cur.ff].ff <= range[i].ff ? cur.ff : i);
nr = (range[i].ss <= range[cur.ss].ss ? cur.ss : i);
if (st3dp[nl][nr] > st3dp[cur.ff][cur.ss] + 1) {
st3dp[nl][nr] = st3dp[cur.ff][cur.ss] + 1;
if (range[nl].ff == 1 && range[nr].ss == n) {
return st3dp[nl][nr];
}
q.push({nl, nr});
}
}
for (int i = cur.ss + 1; i <= range[cur.ss].ss; i++) {
if (range[cur.ff].ff <= range[i].ff && range[i].ss <= range[cur.ss].ss) continue;
nl = (range[cur.ff].ff <= range[i].ff ? cur.ff : i);
nr = (range[i].ss <= range[cur.ss].ss ? cur.ss : i);
if (st3dp[nl][nr] > st3dp[cur.ff][cur.ss] + 1) {
st3dp[nl][nr] = st3dp[cur.ff][cur.ss] + 1;
if (range[nl].ff == 1 && range[nr].ss == n) {
return st3dp[nl][nr];
}
q.push({nl, nr});
}
}
}
return -1;
}
int st2dp[301][301];
void st2recur(int l, int r, int val) {
st2dp[l][r] = val;
int nl, nr;
for (int i = l; i <= r; i++) {
nl = min(l, range[i].ff);
nr = max(r, range[i].ss);
if (st2dp[nl][nr] > val + 1) {
st2recur(nl, nr, val + 1);
}
}
}
int st2(int &tar) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
st2dp[i][j] = 1e9;
}
}
st2recur(range[tar].ff, range[tar].ss, 1);
return (st2dp[1][n] == 1e9 ? -1 : st2dp[1][n]);
}
int st1() {
int ans = 1;
int cur = range[1].ss;
int ptr = 1;
while (cur < n) {
int maxi = cur;
while (ptr <= cur) {
maxi = max(maxi, range[ptr].ss);
ptr++;
}
if (maxi == cur) return -1;
ans++;
cur = maxi;
}
return ans;
}
void solve() {
// solve
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> range[i].ff >> range[i].ss;
}
int q;
cin >> q;
if (q == 1) {
int cur;
cin >> cur;
if (cur == 1) {
cout << st1() << endl;
} else if (n <= 300) {
cout << st2(cur) << endl;
} else if (n <= 2500) {
cout << st3(cur) << endl;
} else {
st5();
cout << ans[cur] << endl;
}
return;
}
if (n <= 2500) {
st4();
} else {
st5();
}
while (q--) {
int x;
cin >> x;
cout << ans[x] << endl;
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
init(); solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1772 KB |
Output is correct |
4 |
Correct |
36 ms |
7372 KB |
Output is correct |
5 |
Correct |
33 ms |
7372 KB |
Output is correct |
6 |
Correct |
34 ms |
7472 KB |
Output is correct |
7 |
Correct |
28 ms |
6612 KB |
Output is correct |
8 |
Correct |
23 ms |
5892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1892 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1892 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
6 ms |
2516 KB |
Output is correct |
12 |
Correct |
42 ms |
2516 KB |
Output is correct |
13 |
Correct |
4 ms |
2532 KB |
Output is correct |
14 |
Correct |
7 ms |
2576 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1892 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1892 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
6 ms |
2516 KB |
Output is correct |
12 |
Correct |
42 ms |
2516 KB |
Output is correct |
13 |
Correct |
4 ms |
2532 KB |
Output is correct |
14 |
Correct |
7 ms |
2576 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
16 |
Correct |
23 ms |
48016 KB |
Output is correct |
17 |
Correct |
125 ms |
50680 KB |
Output is correct |
18 |
Correct |
57 ms |
50888 KB |
Output is correct |
19 |
Correct |
57 ms |
49552 KB |
Output is correct |
20 |
Correct |
43 ms |
50900 KB |
Output is correct |
21 |
Correct |
32 ms |
50900 KB |
Output is correct |
22 |
Correct |
24 ms |
50900 KB |
Output is correct |
23 |
Correct |
22 ms |
50796 KB |
Output is correct |
24 |
Correct |
39 ms |
57272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1892 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1892 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
6 ms |
2516 KB |
Output is correct |
12 |
Correct |
42 ms |
2516 KB |
Output is correct |
13 |
Correct |
4 ms |
2532 KB |
Output is correct |
14 |
Correct |
7 ms |
2576 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
16 |
Correct |
23 ms |
48016 KB |
Output is correct |
17 |
Correct |
125 ms |
50680 KB |
Output is correct |
18 |
Correct |
57 ms |
50888 KB |
Output is correct |
19 |
Correct |
57 ms |
49552 KB |
Output is correct |
20 |
Correct |
43 ms |
50900 KB |
Output is correct |
21 |
Correct |
32 ms |
50900 KB |
Output is correct |
22 |
Correct |
24 ms |
50900 KB |
Output is correct |
23 |
Correct |
22 ms |
50796 KB |
Output is correct |
24 |
Correct |
39 ms |
57272 KB |
Output is correct |
25 |
Incorrect |
1 ms |
1876 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1772 KB |
Output is correct |
4 |
Correct |
36 ms |
7372 KB |
Output is correct |
5 |
Correct |
33 ms |
7372 KB |
Output is correct |
6 |
Correct |
34 ms |
7472 KB |
Output is correct |
7 |
Correct |
28 ms |
6612 KB |
Output is correct |
8 |
Correct |
23 ms |
5892 KB |
Output is correct |
9 |
Correct |
2 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1892 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1892 KB |
Output is correct |
17 |
Correct |
1 ms |
1876 KB |
Output is correct |
18 |
Correct |
1 ms |
1876 KB |
Output is correct |
19 |
Correct |
6 ms |
2516 KB |
Output is correct |
20 |
Correct |
42 ms |
2516 KB |
Output is correct |
21 |
Correct |
4 ms |
2532 KB |
Output is correct |
22 |
Correct |
7 ms |
2576 KB |
Output is correct |
23 |
Correct |
4 ms |
2516 KB |
Output is correct |
24 |
Correct |
23 ms |
48016 KB |
Output is correct |
25 |
Correct |
125 ms |
50680 KB |
Output is correct |
26 |
Correct |
57 ms |
50888 KB |
Output is correct |
27 |
Correct |
57 ms |
49552 KB |
Output is correct |
28 |
Correct |
43 ms |
50900 KB |
Output is correct |
29 |
Correct |
32 ms |
50900 KB |
Output is correct |
30 |
Correct |
24 ms |
50900 KB |
Output is correct |
31 |
Correct |
22 ms |
50796 KB |
Output is correct |
32 |
Correct |
39 ms |
57272 KB |
Output is correct |
33 |
Incorrect |
1 ms |
1876 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |