#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 20, m = 450, INF = 1e9;
pair<int,int> Lcost[maxn];
bool cmpl(int a, int b) {
return Lcost[a].second < Lcost[b].second;
}
pair<int,int> Rcost[maxn];
bool cmpr(int a, int b) {
return Rcost[a].second < Rcost[b].second;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
pair<int,int> a[n+1];
vector<pair<int,int>> Lpos[m], Lpos1[m];
vector<pair<int,int>> lpos[n+1], lpos1[n+1];
for (int i=1;i<=n;i++) {
cin >> a[i].first >> a[i].second;
Lpos1[a[i].first/m].push_back({a[i].second, i});
lpos1[a[i].first].push_back({a[i].second, i});
}
for (int i=0;i<m;i++) sort(Lpos1[i].begin(), Lpos1[i].end());
for (int i=0;i<=n;i++) sort(lpos1[i].begin(), lpos1[i].end());
for (int i=0;i<m;i++) Lpos[i] = Lpos1[i];
for (int i=1;i<=n;i++) lpos[i] = lpos1[i];
bool vis[n+1];
for (int i=1;i<=n;i++) vis[i] = false;
vis[n] = true;
vector<int> from = {n}, to;
int rcost[n+1];
for (int i=1;i<=n;i++) rcost[i] = INF;
for (int cost=-1; from.size()>0; cost++) {
for (int u:from) rcost[u] = max(cost, (int)0);
for (int u:from) {
for (int L=0;(L+1)*m-1<=u;L++) {
while (Lpos[L].size()>0 && Lpos[L].back().first>=u) {
int v = Lpos[L].back().second;
if (!vis[v]) to.push_back(v);
vis[v] = true;
Lpos[L].pop_back();
}
}
int l = u/m*m;
for (;l<=u;l++) {
// if (lpos[l].size()>0) cout << "test " << lpos[l].back().first << endl;
while (lpos[l].size()>0 && lpos[l].back().first>=u) {
int v = lpos[l].back().second;
// if (!vis[v]) cout << cost << " " << u << " " << v << endl;
if (!vis[v]) to.push_back(v);
vis[v] = true;
lpos[l].pop_back();
}
}
}
from.clear();
swap(to, from);
}
for (int i=0;i<m;i++) Lpos[i] = Lpos1[i];
for (int i=1;i<=n;i++) lpos[i] = lpos1[i];
for (int i=1;i<=n;i++) vis[i] = false;
vis[1] = true;
from = {1};
for (int i=1;i<=n;i++) Lcost[i] = {INF, INF};
Lcost[1].second = rcost[1] + 1;
for (int cost=0; from.size()>0; cost++) {
sort(from.begin(), from.end(), cmpl);
for (int u:from) Lcost[u].first = cost;
for (int u:from) {
for (int L=0;(L+1)*m-1<=u;L++) {
while (Lpos[L].size()>0 && Lpos[L].back().first>=u) {
int v = Lpos[L].back().second;
if (!vis[v]) {
to.push_back(v);
Lcost[v].second = min(rcost[v], Lcost[u].second);
}
vis[v] = true;
Lpos[L].pop_back();
}
}
int l = u/m*m;
for (;l<=u;l++) {
while (lpos[l].size()>0 && lpos[l].back().first>=u) {
int v = lpos[l].back().second;
if (!vis[v]) {
to.push_back(v);
Lcost[v].second = min(rcost[v], Lcost[u].second);
}
vis[v] = true;
lpos[l].pop_back();
}
}
}
from.clear();
swap(to, from);
}
//2nd set of ans
for (int i=0;i<m;i++) Lpos[i] = Lpos1[i];
for (int i=1;i<=n;i++) lpos[i] = lpos1[i];
for (int i=1;i<=n;i++) vis[i] = false;
vis[1] = true;
from = {1};
to.clear();
int lcost[n+1];
for (int i=1;i<=n;i++) lcost[i] = INF;
for (int cost=-1; from.size()>0; cost++) {
for (int u:from) lcost[u] = max(cost, (int)0);
for (int u:from) {
for (int L=0;(L+1)*m-1<=u;L++) {
while (Lpos[L].size()>0 && Lpos[L].back().first>=u) {
int v = Lpos[L].back().second;
if (!vis[v]) to.push_back(v);
vis[v] = true;
Lpos[L].pop_back();
}
}
int l = u/m*m;
for (;l<=u;l++) {
while (lpos[l].size()>0 && lpos[l].back().first>=u) {
int v = lpos[l].back().second;
if (!vis[v]) to.push_back(v);
vis[v] = true;
lpos[l].pop_back();
}
}
}
from.clear();
swap(to, from);
}
for (int i=0;i<m;i++) Lpos[i] = Lpos1[i];
for (int i=1;i<=n;i++) lpos[i] = lpos1[i];
for (int i=1;i<=n;i++) vis[i] = false;
vis[n] = true;
from = {n};
for (int i=1;i<=n;i++) Rcost[i] = {INF, INF};
Rcost[1].second = lcost[1] + 1;
for (int cost=0; from.size()>0; cost++) {
sort(from.begin(), from.end(), cmpr);
for (int u:from) Rcost[u].first = cost;
for (int u:from) {
for (int L=0;(L+1)*m-1<=u;L++) {
while (Lpos[L].size()>0 && Lpos[L].back().first>=u) {
int v = Lpos[L].back().second;
if (!vis[v]) {
to.push_back(v);
Rcost[v].second = min(lcost[v], Rcost[u].second);
}
vis[v] = true;
Lpos[L].pop_back();
}
}
int l = u/m*m;
for (;l<=u;l++) {
while (lpos[l].size()>0 && lpos[l].back().first>=u) {
int v = lpos[l].back().second;
if (!vis[v]) {
to.push_back(v);
Rcost[v].second = min(lcost[v], Rcost[u].second);
}
vis[v] = true;
lpos[l].pop_back();
}
}
}
from.clear();
swap(to, from);
}
int q;
cin >> q;
for (int i=0;i<q;i++) {
int pos;
cin >> pos;
int ans = min(Lcost[pos].first + Lcost[pos].second, Rcost[pos].first + Rcost[pos].second);
if (ans >= INF) cout << "-1\n";
else cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
528 ms |
40876 KB |
Output is correct |
5 |
Correct |
527 ms |
39372 KB |
Output is correct |
6 |
Correct |
519 ms |
41812 KB |
Output is correct |
7 |
Correct |
360 ms |
37800 KB |
Output is correct |
8 |
Correct |
146 ms |
30636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2928 KB |
Output is correct |
17 |
Correct |
4 ms |
2908 KB |
Output is correct |
18 |
Correct |
3 ms |
2908 KB |
Output is correct |
19 |
Correct |
3 ms |
2908 KB |
Output is correct |
20 |
Correct |
4 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
4 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2928 KB |
Output is correct |
17 |
Correct |
4 ms |
2908 KB |
Output is correct |
18 |
Correct |
3 ms |
2908 KB |
Output is correct |
19 |
Correct |
3 ms |
2908 KB |
Output is correct |
20 |
Correct |
4 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
4 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2800 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
4 ms |
2900 KB |
Output is correct |
28 |
Correct |
4 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
5 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
528 ms |
40876 KB |
Output is correct |
5 |
Correct |
527 ms |
39372 KB |
Output is correct |
6 |
Correct |
519 ms |
41812 KB |
Output is correct |
7 |
Correct |
360 ms |
37800 KB |
Output is correct |
8 |
Correct |
146 ms |
30636 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2928 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
4 ms |
2908 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
4 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2800 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
4 ms |
2900 KB |
Output is correct |
36 |
Correct |
4 ms |
2908 KB |
Output is correct |
37 |
Correct |
4 ms |
2908 KB |
Output is correct |
38 |
Correct |
3 ms |
2908 KB |
Output is correct |
39 |
Correct |
5 ms |
2908 KB |
Output is correct |
40 |
Correct |
587 ms |
45360 KB |
Output is correct |
41 |
Correct |
570 ms |
44520 KB |
Output is correct |
42 |
Correct |
467 ms |
43604 KB |
Output is correct |
43 |
Correct |
453 ms |
44608 KB |
Output is correct |
44 |
Correct |
522 ms |
46928 KB |
Output is correct |
45 |
Correct |
429 ms |
45908 KB |
Output is correct |
46 |
Correct |
129 ms |
19020 KB |
Output is correct |
47 |
Correct |
498 ms |
45232 KB |
Output is correct |
48 |
Correct |
288 ms |
45380 KB |
Output is correct |