#include <bits/stdc++.h>
#define MAX 200005
#define MOD 998244353
#define INF (ll)(1e18)
#define inf (1987654321)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef complex<long double> cpx;
constexpr long double PI = acos(-1);
int tt, n, m, k, q;
int L[MAX], R[MAX], in[MAX];
int dist1[MAX << 2], dist2[MAX << 2], dist[MAX << 2];
vector<pii> graph[MAX << 2];
vector<int> nodes;
void initGraph(int str, int ed, int node) {
dist1[node] = dist2[node] = n + 2;
if(str == ed) { in[str] = node; return; }
graph[node << 1].push_back({node, 0});
graph[node << 1 | 1].push_back({node, 0});
int mid = str + ed >> 1;
initGraph(str, mid, node << 1);
initGraph(mid + 1, ed, node << 1 | 1);
}
void getRange(int str, int ed, int left, int right, int node) {
if(str > right || ed < left) return;
if(left <= str && ed <= right) {
nodes.push_back(node);
return;
}
int mid = str + ed >> 1;
getRange(str, mid, left, right, node << 1);
getRange(mid + 1, ed, left, right, node << 1 | 1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n; initGraph(1, n, 1);
for(int i = 1; i <= n; ++i) cin >> L[i] >> R[i];
for(int i = 1; i <= n; ++i) {
nodes.clear(); getRange(1, n, L[i], R[i], 1);
for(auto v : nodes) graph[v].push_back({in[i], 1});
}
deque<pii> dq;
dq.push_back({in[1], 0});
while(!dq.empty()) {
int cur = dq.front().first;
int d = dq.front().second;
dq.pop_front();
if(dist1[cur] != n + 2) continue;
dist1[cur] = d;
for(auto [v, w] : graph[cur]) {
if(w == 0) dq.push_front({v, d});
else dq.push_back({v, d + 1});
}
}
dq.push_back({in[n], 0});
while(!dq.empty()) {
int cur = dq.front().first;
int d = dq.front().second;
dq.pop_front();
if(dist2[cur] != n + 2) continue;
dist2[cur] = d;
for(auto [v, w] : graph[cur]) {
if(w == 0) dq.push_front({v, d});
else dq.push_back({v, d + 1});
}
}
for(int i = 1; i <= 4 * n; ++i) dist[i] = dist1[i] + dist2[i];
for(int i = 2; i < n; ++i) dist[in[i]]--;
priority_queue<pii> pq;
for(int i = 1; i <= 4 * n; ++i) pq.push({-dist[i], i});
while(!pq.empty()) {
int d = -pq.top().first;
int cur = pq.top().second;
pq.pop();
if(dist[cur] < d) continue;
for(auto [v, w] : graph[cur]) {
if(dist[v] > dist[cur] + w) {
dist[v] = dist[cur] + w;
pq.push({-dist[v], v});
}
}
}
cin >> q;
while(q--) {
int x; cin >> x;
if(dist[in[x]] > n) cout << -1 << '\n';
else cout << dist[in[x]] << '\n';
}
return 0;
}
Compilation message
passport.cpp: In function 'void initGraph(int, int, int)':
passport.cpp:28:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int mid = str + ed >> 1;
| ~~~~^~~~
passport.cpp: In function 'void getRange(int, int, int, int, int)':
passport.cpp:39:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid = str + ed >> 1;
| ~~~~^~~~
passport.cpp: In function 'int main()':
passport.cpp:66:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [v, w] : graph[cur]) {
| ^
passport.cpp:81:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | for(auto [v, w] : graph[cur]) {
| ^
passport.cpp:98:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
98 | for(auto [v, w] : graph[cur]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27224 KB |
Output is correct |
4 |
Correct |
563 ms |
96952 KB |
Output is correct |
5 |
Correct |
289 ms |
60776 KB |
Output is correct |
6 |
Correct |
194 ms |
56512 KB |
Output is correct |
7 |
Correct |
168 ms |
56504 KB |
Output is correct |
8 |
Correct |
189 ms |
67496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27224 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
5 ms |
27224 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27480 KB |
Output is correct |
9 |
Correct |
5 ms |
27224 KB |
Output is correct |
10 |
Correct |
5 ms |
27228 KB |
Output is correct |
11 |
Correct |
5 ms |
27224 KB |
Output is correct |
12 |
Correct |
5 ms |
27228 KB |
Output is correct |
13 |
Correct |
5 ms |
27224 KB |
Output is correct |
14 |
Correct |
5 ms |
27224 KB |
Output is correct |
15 |
Correct |
5 ms |
27224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27224 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
5 ms |
27224 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27480 KB |
Output is correct |
9 |
Correct |
5 ms |
27224 KB |
Output is correct |
10 |
Correct |
5 ms |
27228 KB |
Output is correct |
11 |
Correct |
5 ms |
27224 KB |
Output is correct |
12 |
Correct |
5 ms |
27228 KB |
Output is correct |
13 |
Correct |
5 ms |
27224 KB |
Output is correct |
14 |
Correct |
5 ms |
27224 KB |
Output is correct |
15 |
Correct |
5 ms |
27224 KB |
Output is correct |
16 |
Correct |
9 ms |
27964 KB |
Output is correct |
17 |
Correct |
8 ms |
27736 KB |
Output is correct |
18 |
Correct |
9 ms |
27992 KB |
Output is correct |
19 |
Correct |
9 ms |
27992 KB |
Output is correct |
20 |
Correct |
8 ms |
27736 KB |
Output is correct |
21 |
Correct |
8 ms |
27736 KB |
Output is correct |
22 |
Correct |
7 ms |
27764 KB |
Output is correct |
23 |
Correct |
8 ms |
27736 KB |
Output is correct |
24 |
Correct |
8 ms |
27736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27224 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
5 ms |
27224 KB |
Output is correct |
7 |
Correct |
6 ms |
27224 KB |
Output is correct |
8 |
Correct |
6 ms |
27480 KB |
Output is correct |
9 |
Correct |
5 ms |
27224 KB |
Output is correct |
10 |
Correct |
5 ms |
27228 KB |
Output is correct |
11 |
Correct |
5 ms |
27224 KB |
Output is correct |
12 |
Correct |
5 ms |
27228 KB |
Output is correct |
13 |
Correct |
5 ms |
27224 KB |
Output is correct |
14 |
Correct |
5 ms |
27224 KB |
Output is correct |
15 |
Correct |
5 ms |
27224 KB |
Output is correct |
16 |
Correct |
9 ms |
27964 KB |
Output is correct |
17 |
Correct |
8 ms |
27736 KB |
Output is correct |
18 |
Correct |
9 ms |
27992 KB |
Output is correct |
19 |
Correct |
9 ms |
27992 KB |
Output is correct |
20 |
Correct |
8 ms |
27736 KB |
Output is correct |
21 |
Correct |
8 ms |
27736 KB |
Output is correct |
22 |
Correct |
7 ms |
27764 KB |
Output is correct |
23 |
Correct |
8 ms |
27736 KB |
Output is correct |
24 |
Correct |
8 ms |
27736 KB |
Output is correct |
25 |
Correct |
5 ms |
27228 KB |
Output is correct |
26 |
Correct |
5 ms |
27224 KB |
Output is correct |
27 |
Correct |
9 ms |
27996 KB |
Output is correct |
28 |
Correct |
9 ms |
27736 KB |
Output is correct |
29 |
Correct |
7 ms |
27736 KB |
Output is correct |
30 |
Correct |
8 ms |
27736 KB |
Output is correct |
31 |
Correct |
8 ms |
27736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27224 KB |
Output is correct |
4 |
Correct |
563 ms |
96952 KB |
Output is correct |
5 |
Correct |
289 ms |
60776 KB |
Output is correct |
6 |
Correct |
194 ms |
56512 KB |
Output is correct |
7 |
Correct |
168 ms |
56504 KB |
Output is correct |
8 |
Correct |
189 ms |
67496 KB |
Output is correct |
9 |
Correct |
5 ms |
27224 KB |
Output is correct |
10 |
Correct |
5 ms |
27224 KB |
Output is correct |
11 |
Correct |
5 ms |
27224 KB |
Output is correct |
12 |
Correct |
5 ms |
27228 KB |
Output is correct |
13 |
Correct |
5 ms |
27228 KB |
Output is correct |
14 |
Correct |
5 ms |
27224 KB |
Output is correct |
15 |
Correct |
6 ms |
27224 KB |
Output is correct |
16 |
Correct |
6 ms |
27480 KB |
Output is correct |
17 |
Correct |
5 ms |
27224 KB |
Output is correct |
18 |
Correct |
5 ms |
27228 KB |
Output is correct |
19 |
Correct |
5 ms |
27224 KB |
Output is correct |
20 |
Correct |
5 ms |
27228 KB |
Output is correct |
21 |
Correct |
5 ms |
27224 KB |
Output is correct |
22 |
Correct |
5 ms |
27224 KB |
Output is correct |
23 |
Correct |
5 ms |
27224 KB |
Output is correct |
24 |
Correct |
9 ms |
27964 KB |
Output is correct |
25 |
Correct |
8 ms |
27736 KB |
Output is correct |
26 |
Correct |
9 ms |
27992 KB |
Output is correct |
27 |
Correct |
9 ms |
27992 KB |
Output is correct |
28 |
Correct |
8 ms |
27736 KB |
Output is correct |
29 |
Correct |
8 ms |
27736 KB |
Output is correct |
30 |
Correct |
7 ms |
27764 KB |
Output is correct |
31 |
Correct |
8 ms |
27736 KB |
Output is correct |
32 |
Correct |
8 ms |
27736 KB |
Output is correct |
33 |
Correct |
5 ms |
27228 KB |
Output is correct |
34 |
Correct |
5 ms |
27224 KB |
Output is correct |
35 |
Correct |
9 ms |
27996 KB |
Output is correct |
36 |
Correct |
9 ms |
27736 KB |
Output is correct |
37 |
Correct |
7 ms |
27736 KB |
Output is correct |
38 |
Correct |
8 ms |
27736 KB |
Output is correct |
39 |
Correct |
8 ms |
27736 KB |
Output is correct |
40 |
Correct |
609 ms |
100032 KB |
Output is correct |
41 |
Correct |
338 ms |
64976 KB |
Output is correct |
42 |
Correct |
456 ms |
92620 KB |
Output is correct |
43 |
Correct |
430 ms |
99140 KB |
Output is correct |
44 |
Correct |
231 ms |
59260 KB |
Output is correct |
45 |
Correct |
317 ms |
67120 KB |
Output is correct |
46 |
Correct |
112 ms |
46156 KB |
Output is correct |
47 |
Correct |
337 ms |
74368 KB |
Output is correct |
48 |
Correct |
321 ms |
77772 KB |
Output is correct |