#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, q, L[200005], R[200005], cl[200005], cr[200005];
struct node{
int s, e, m, val;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
val = 0;
if(s != e)l = new node(s, m), r = new node(m+1, e);
}
void upd(int p, int v){
if(s == e){
val = v;
return;
}
if(p <= m)l->upd(p, v);
else r->upd(p, v);
val = min(l->val, r->val);
}
int qry(int a, int b){
if(s == a && b == e)return val;
else if(b <= m)return l->qry(a, b);
else if(a > m)return r->qry(a, b);
else return min(l->qry(a, m), r->qry(m+1, b));
}
}*root;
vector <pi> adj[600005];
int dist[600005];
int cnt;
struct node2{
int s, e, m, nd;
node2 *l, *r;
node2(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e){
nd = ++cnt;
l = new node2(s, m);
r = new node2(m+1, e);
adj[l->nd].push_back({nd, 0});
adj[r->nd].push_back({nd, 0});
}
else nd = s;
}
void upd(int a, int b, int u){
if(s == a && e == b){
adj[nd].push_back({u, 1});
return;
}
if(b <= m)l->upd(a, b, u);
else if(a > m)r->upd(a, b, u);
else l->upd(a, m, u), r->upd(m+1, b, u);
}
}*root2;
void solve(){
cin >> n;
for(int i=1;i<=n;i++)cin >> L[i] >> R[i];
/*
root = new node(1, n);
cl[1] = cr[n] = 1;
for(int i=2;i<=n;i++){
if(L[i] == i){
cl[i] = 1e18;
}
else cl[i] = root->qry(L[i], i - 1) + 1;
root->upd(i, cl[i]);
}
root = new node(1, n);
for(int i=n-1;i>=1;i--){
if(R[i] == i){
cr[i] = 1e18;
}
else cr[i] = root->qry(i + 1, R[i]) + 1;
root->upd(i, cr[i]);
}
*/
cnt = n;
root2 = new node2(1, n);
for(int i=1;i<=n;i++)root2->upd(L[i], R[i], i);
priority_queue <pi, vector <pi>, greater <pi> > pq;
for(int i=1;i<=cnt;i++)cl[i] = 1e18;
cl[1] = 0;
pq.push({0, 1});
while(!pq.empty()){
int x = pq.top().fi, y = pq.top().se;
pq.pop();
if(cl[y] < x)continue;
for(auto [i, j] : adj[y]){
if(cl[i] > x + j)cl[i] = x + j, pq.push({cl[i], i});
}
}
for(int i=1;i<=cnt;i++)cr[i] = 1e18;
cr[n] = 0;
pq.push({0, n});
while(!pq.empty()){
int x = pq.top().fi, y = pq.top().se;
pq.pop();
if(cr[y] < x)continue;
for(auto [i, j] : adj[y]){
if(cr[i] > x + j)cr[i] = x + j, pq.push({cr[i], i});
}
}
for(int i=1;i<=n;i++){
if(i == 1)dist[i] = cr[i];
else if(i == n)dist[i] = cl[i];
else dist[i] = cl[i] + cr[i] - 1;
//cout << cl[i] << ' ' << cr[i] << ' ' << dist[i] << '\n';
//if(L[i] == 1 && R[i] == n)dist[i] = 1;
//cout << dist[i] << ' ';
pq.push({dist[i], i});
}
for(int i=n+1;i<=cnt;i++)dist[i] = 1e18;
while(!pq.empty()){
int x = pq.top().fi, y = pq.top().se;
pq.pop();
if(dist[y] < x)continue;
for(auto [i, j] : adj[y]){
if(dist[i] > x + j)dist[i] = x + j, pq.push({dist[i], i});
}
}
//for(int i=1;i<=cnt;i++)cout << dist[i] << ' ';
//cout << '\n';
cin >> q;
while(q--){
int x; cin >> x;
if(dist[x] > 1e9)cout << -1 << '\n';
else cout << dist[x] << '\n';
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
passport.cpp:150:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
150 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14400 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
729 ms |
128296 KB |
Output is correct |
5 |
Incorrect |
342 ms |
83696 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14364 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
7 ms |
14548 KB |
Output is correct |
12 |
Correct |
6 ms |
14548 KB |
Output is correct |
13 |
Correct |
7 ms |
14552 KB |
Output is correct |
14 |
Correct |
7 ms |
14552 KB |
Output is correct |
15 |
Correct |
7 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14364 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
7 ms |
14548 KB |
Output is correct |
12 |
Correct |
6 ms |
14548 KB |
Output is correct |
13 |
Correct |
7 ms |
14552 KB |
Output is correct |
14 |
Correct |
7 ms |
14552 KB |
Output is correct |
15 |
Correct |
7 ms |
14548 KB |
Output is correct |
16 |
Correct |
12 ms |
15588 KB |
Output is correct |
17 |
Correct |
12 ms |
15460 KB |
Output is correct |
18 |
Correct |
13 ms |
15828 KB |
Output is correct |
19 |
Correct |
10 ms |
15828 KB |
Output is correct |
20 |
Correct |
11 ms |
15352 KB |
Output is correct |
21 |
Correct |
9 ms |
15444 KB |
Output is correct |
22 |
Correct |
8 ms |
15188 KB |
Output is correct |
23 |
Correct |
9 ms |
15460 KB |
Output is correct |
24 |
Correct |
9 ms |
15572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14364 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
6 ms |
14420 KB |
Output is correct |
5 |
Correct |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
7 ms |
14548 KB |
Output is correct |
12 |
Correct |
6 ms |
14548 KB |
Output is correct |
13 |
Correct |
7 ms |
14552 KB |
Output is correct |
14 |
Correct |
7 ms |
14552 KB |
Output is correct |
15 |
Correct |
7 ms |
14548 KB |
Output is correct |
16 |
Correct |
12 ms |
15588 KB |
Output is correct |
17 |
Correct |
12 ms |
15460 KB |
Output is correct |
18 |
Correct |
13 ms |
15828 KB |
Output is correct |
19 |
Correct |
10 ms |
15828 KB |
Output is correct |
20 |
Correct |
11 ms |
15352 KB |
Output is correct |
21 |
Correct |
9 ms |
15444 KB |
Output is correct |
22 |
Correct |
8 ms |
15188 KB |
Output is correct |
23 |
Correct |
9 ms |
15460 KB |
Output is correct |
24 |
Correct |
9 ms |
15572 KB |
Output is correct |
25 |
Correct |
7 ms |
14420 KB |
Output is correct |
26 |
Correct |
6 ms |
14420 KB |
Output is correct |
27 |
Correct |
11 ms |
15572 KB |
Output is correct |
28 |
Correct |
9 ms |
15444 KB |
Output is correct |
29 |
Correct |
10 ms |
15316 KB |
Output is correct |
30 |
Correct |
9 ms |
15444 KB |
Output is correct |
31 |
Correct |
10 ms |
15316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14400 KB |
Output is correct |
3 |
Correct |
6 ms |
14420 KB |
Output is correct |
4 |
Correct |
729 ms |
128296 KB |
Output is correct |
5 |
Incorrect |
342 ms |
83696 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |