#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) 3e5 + 7;
int n, q;
int l[N], r[N];
int d[4][N];
int vis[N * 4];
vector<int> g, t[N * 4];
void upd(int l, int r, int id, int v = 1, int tl = 1, int tr = n) {
if(tr < l || r < tl || vis[v]) return;
if(tl >= l && tr <= r) {
t[v].push_back(id);
return;
}
int tm = (tl+tr)>>1;
upd(l, r, id, v<<1, tl, tm);
upd(l, r, id, v<<1|1, tm+1, tr);
}
void get(int p, int v = 1, int tl = 1, int tr = n) {
for (auto to : t[v])
g.push_back(to);
t[v].clear();
if(tl == tr) return;
int tm = (tl+tr) >> 1;
if(p <= tm) {
get(p, v<<1, tl, tm);
} else {
get(p, v<<1 | 1, tm + 1, tr);
}
}
void init() {
memset(vis, 0, sizeof vis);
for (int v = 1; v <= n; ++v) {
upd(l[v], r[v], v);
}
}
void djk(int id, int st) {
for (int v = 1; v <= n; ++v) {
d[id][v] = 1e9;
}
d[id][st] = 0;
set<pair<int,int>> s;
s.insert({1, st});
//cout << "start: " << st << endl;
while(sz(s)) {
int v = s.begin()->second;
s.erase(s.begin());
get(v);
for (int to : g) {
if(d[id][to] > d[id][v] + 1) {
s.erase({d[id][to], to});
d[id][to] = d[id][v] + 1;
s.insert({d[id][to], to});
}
}
g.clear();
}
}
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];
}
init();
djk(0, 1);
init();
djk(1, n);
init();
set<pair<int,int>> s;
for (int v = 1; v <= n; ++v) {
// cout << "here: " << v << ' ' << d[0][v] << ' ' << d[1][v] << '\n';
if(v == 1) {
d[2][v] = d[1][v];
} else if(v == n) {
d[2][v] = d[0][v];
} else {
d[2][v] = d[1][v] + d[0][v] - 1;
}
if(d[2][v] < 1e9) {
s.insert({d[2][v], v});
}
}
while(sz(s)) {
while(sz(s)) {
int v = s.begin()->second;
s.erase(s.begin());
get(v);
for (int to : g) {
if(d[2][to] > d[2][v] + 1) {
s.erase({d[2][to], to});
d[2][to] = d[2][v] + 1;
s.insert({d[2][to], to});
}
}
g.clear();
}
}
cin >> q;
for(; q; --q) {
int x;
cin >> x;
int res = d[2][x];
if(res >= 1e9) res = -1;
cout << res << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
33236 KB |
Output is correct |
2 |
Correct |
15 ms |
33236 KB |
Output is correct |
3 |
Correct |
17 ms |
33232 KB |
Output is correct |
4 |
Correct |
725 ms |
69008 KB |
Output is correct |
5 |
Correct |
341 ms |
59276 KB |
Output is correct |
6 |
Correct |
223 ms |
59336 KB |
Output is correct |
7 |
Correct |
217 ms |
49348 KB |
Output is correct |
8 |
Correct |
165 ms |
52652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33236 KB |
Output is correct |
2 |
Correct |
14 ms |
33156 KB |
Output is correct |
3 |
Correct |
17 ms |
33116 KB |
Output is correct |
4 |
Correct |
17 ms |
33244 KB |
Output is correct |
5 |
Correct |
14 ms |
33236 KB |
Output is correct |
6 |
Correct |
14 ms |
33188 KB |
Output is correct |
7 |
Correct |
14 ms |
33236 KB |
Output is correct |
8 |
Correct |
14 ms |
33236 KB |
Output is correct |
9 |
Correct |
15 ms |
33236 KB |
Output is correct |
10 |
Correct |
15 ms |
33192 KB |
Output is correct |
11 |
Correct |
15 ms |
33220 KB |
Output is correct |
12 |
Correct |
14 ms |
33192 KB |
Output is correct |
13 |
Correct |
15 ms |
33272 KB |
Output is correct |
14 |
Correct |
15 ms |
33236 KB |
Output is correct |
15 |
Correct |
14 ms |
33264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33236 KB |
Output is correct |
2 |
Correct |
14 ms |
33156 KB |
Output is correct |
3 |
Correct |
17 ms |
33116 KB |
Output is correct |
4 |
Correct |
17 ms |
33244 KB |
Output is correct |
5 |
Correct |
14 ms |
33236 KB |
Output is correct |
6 |
Correct |
14 ms |
33188 KB |
Output is correct |
7 |
Correct |
14 ms |
33236 KB |
Output is correct |
8 |
Correct |
14 ms |
33236 KB |
Output is correct |
9 |
Correct |
15 ms |
33236 KB |
Output is correct |
10 |
Correct |
15 ms |
33192 KB |
Output is correct |
11 |
Correct |
15 ms |
33220 KB |
Output is correct |
12 |
Correct |
14 ms |
33192 KB |
Output is correct |
13 |
Correct |
15 ms |
33272 KB |
Output is correct |
14 |
Correct |
15 ms |
33236 KB |
Output is correct |
15 |
Correct |
14 ms |
33264 KB |
Output is correct |
16 |
Correct |
20 ms |
33492 KB |
Output is correct |
17 |
Correct |
17 ms |
33492 KB |
Output is correct |
18 |
Correct |
21 ms |
33616 KB |
Output is correct |
19 |
Correct |
21 ms |
33640 KB |
Output is correct |
20 |
Correct |
19 ms |
33452 KB |
Output is correct |
21 |
Correct |
17 ms |
33492 KB |
Output is correct |
22 |
Correct |
19 ms |
33364 KB |
Output is correct |
23 |
Correct |
17 ms |
33480 KB |
Output is correct |
24 |
Correct |
17 ms |
33660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33236 KB |
Output is correct |
2 |
Correct |
14 ms |
33156 KB |
Output is correct |
3 |
Correct |
17 ms |
33116 KB |
Output is correct |
4 |
Correct |
17 ms |
33244 KB |
Output is correct |
5 |
Correct |
14 ms |
33236 KB |
Output is correct |
6 |
Correct |
14 ms |
33188 KB |
Output is correct |
7 |
Correct |
14 ms |
33236 KB |
Output is correct |
8 |
Correct |
14 ms |
33236 KB |
Output is correct |
9 |
Correct |
15 ms |
33236 KB |
Output is correct |
10 |
Correct |
15 ms |
33192 KB |
Output is correct |
11 |
Correct |
15 ms |
33220 KB |
Output is correct |
12 |
Correct |
14 ms |
33192 KB |
Output is correct |
13 |
Correct |
15 ms |
33272 KB |
Output is correct |
14 |
Correct |
15 ms |
33236 KB |
Output is correct |
15 |
Correct |
14 ms |
33264 KB |
Output is correct |
16 |
Correct |
20 ms |
33492 KB |
Output is correct |
17 |
Correct |
17 ms |
33492 KB |
Output is correct |
18 |
Correct |
21 ms |
33616 KB |
Output is correct |
19 |
Correct |
21 ms |
33640 KB |
Output is correct |
20 |
Correct |
19 ms |
33452 KB |
Output is correct |
21 |
Correct |
17 ms |
33492 KB |
Output is correct |
22 |
Correct |
19 ms |
33364 KB |
Output is correct |
23 |
Correct |
17 ms |
33480 KB |
Output is correct |
24 |
Correct |
17 ms |
33660 KB |
Output is correct |
25 |
Correct |
20 ms |
33228 KB |
Output is correct |
26 |
Correct |
15 ms |
33180 KB |
Output is correct |
27 |
Correct |
20 ms |
33620 KB |
Output is correct |
28 |
Correct |
19 ms |
33492 KB |
Output is correct |
29 |
Correct |
17 ms |
33512 KB |
Output is correct |
30 |
Correct |
23 ms |
33516 KB |
Output is correct |
31 |
Correct |
17 ms |
33572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
33236 KB |
Output is correct |
2 |
Correct |
15 ms |
33236 KB |
Output is correct |
3 |
Correct |
17 ms |
33232 KB |
Output is correct |
4 |
Correct |
725 ms |
69008 KB |
Output is correct |
5 |
Correct |
341 ms |
59276 KB |
Output is correct |
6 |
Correct |
223 ms |
59336 KB |
Output is correct |
7 |
Correct |
217 ms |
49348 KB |
Output is correct |
8 |
Correct |
165 ms |
52652 KB |
Output is correct |
9 |
Correct |
14 ms |
33236 KB |
Output is correct |
10 |
Correct |
14 ms |
33156 KB |
Output is correct |
11 |
Correct |
17 ms |
33116 KB |
Output is correct |
12 |
Correct |
17 ms |
33244 KB |
Output is correct |
13 |
Correct |
14 ms |
33236 KB |
Output is correct |
14 |
Correct |
14 ms |
33188 KB |
Output is correct |
15 |
Correct |
14 ms |
33236 KB |
Output is correct |
16 |
Correct |
14 ms |
33236 KB |
Output is correct |
17 |
Correct |
15 ms |
33236 KB |
Output is correct |
18 |
Correct |
15 ms |
33192 KB |
Output is correct |
19 |
Correct |
15 ms |
33220 KB |
Output is correct |
20 |
Correct |
14 ms |
33192 KB |
Output is correct |
21 |
Correct |
15 ms |
33272 KB |
Output is correct |
22 |
Correct |
15 ms |
33236 KB |
Output is correct |
23 |
Correct |
14 ms |
33264 KB |
Output is correct |
24 |
Correct |
20 ms |
33492 KB |
Output is correct |
25 |
Correct |
17 ms |
33492 KB |
Output is correct |
26 |
Correct |
21 ms |
33616 KB |
Output is correct |
27 |
Correct |
21 ms |
33640 KB |
Output is correct |
28 |
Correct |
19 ms |
33452 KB |
Output is correct |
29 |
Correct |
17 ms |
33492 KB |
Output is correct |
30 |
Correct |
19 ms |
33364 KB |
Output is correct |
31 |
Correct |
17 ms |
33480 KB |
Output is correct |
32 |
Correct |
17 ms |
33660 KB |
Output is correct |
33 |
Correct |
20 ms |
33228 KB |
Output is correct |
34 |
Correct |
15 ms |
33180 KB |
Output is correct |
35 |
Correct |
20 ms |
33620 KB |
Output is correct |
36 |
Correct |
19 ms |
33492 KB |
Output is correct |
37 |
Correct |
17 ms |
33512 KB |
Output is correct |
38 |
Correct |
23 ms |
33516 KB |
Output is correct |
39 |
Correct |
17 ms |
33572 KB |
Output is correct |
40 |
Correct |
868 ms |
70760 KB |
Output is correct |
41 |
Correct |
417 ms |
61896 KB |
Output is correct |
42 |
Correct |
543 ms |
71772 KB |
Output is correct |
43 |
Correct |
513 ms |
71392 KB |
Output is correct |
44 |
Correct |
283 ms |
62044 KB |
Output is correct |
45 |
Correct |
292 ms |
58192 KB |
Output is correct |
46 |
Correct |
149 ms |
43332 KB |
Output is correct |
47 |
Correct |
426 ms |
63232 KB |
Output is correct |
48 |
Correct |
456 ms |
78328 KB |
Output is correct |