#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int,int>
using namespace std;
const int N = 2e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
const string NAME = "solve";
int n,cnt;
int d[2][4*N];
vector<pii> adj[4*N];
int idx[4*N];
int q;
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
idx[id] = l;
return;
}
idx[id] = ++cnt;
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid+1,r);
adj[idx[id << 1]].push_back({idx[id],0});
adj[idx[id << 1 | 1]].push_back({idx[id],0});
}
void update(int root, int u, int v, int id = 1, int l = 1, int r = n) {
if(u <= l and r <= v) {
adj[idx[id]].push_back({root,1});
return;
}
int mid = l + r >> 1;
if(u <= mid) {
update(root,u,v,id << 1,l,mid);
}
if(mid+1 <= v) {
update(root,u,v,id << 1 | 1,mid+1,r);
}
}
priority_queue<pii,vector<pii>,greater<pii>> pq;
void dijkstra(int d[]) {
while(!pq.empty()) {
int u = pq.top().second;
int du = pq.top().first;
pq.pop();
if(du > d[u]) {
continue;
}
for(auto p:adj[u]) {
int v = p.first;
int w = p.second;
if(d[u] + w < d[v]) {
d[v] = d[u] + w;
pq.push({d[v],v});
}
}
}
}
signed main()
{
if (fopen((NAME + ".inp").c_str(), "r")) {
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cnt = n;
build();
for(int i = 1 ; i <= n ; i++) {
int l,r; cin >> l >> r;
update(i,l,r);
}
for(int j = 0 ; j < 2 ; j++) {
for(int i = 1 ; i <= cnt ; i++) {
d[j][i] = 1e8;
}
}
d[0][1] = 0;
pq.push({0,1});
dijkstra(d[0]);
d[1][n] = 0;
pq.push({0,n});
dijkstra(d[1]);
for(int i = 1 ; i <= cnt ; i++) {
d[0][i] = d[0][i] + d[1][i];
if(1 < i and i < n) {
d[0][i]--;
}
pq.push({d[0][i],i});
}
dijkstra(d[0]);
cin >> q;
while(q--) {
int x; cin >> x;
if(d[0][x] >= 1e8) {
d[0][x] = -1;
}
cout << d[0][x] << '\n';
}
return 0;
}
Compilation message
passport.cpp: In function 'void build(int, int, int)':
passport.cpp:25:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void update(int, int, int, int, int, int)':
passport.cpp:37:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'int main()':
passport.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen((NAME + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
595 ms |
74976 KB |
Output is correct |
5 |
Correct |
269 ms |
54880 KB |
Output is correct |
6 |
Correct |
149 ms |
51160 KB |
Output is correct |
7 |
Correct |
180 ms |
47164 KB |
Output is correct |
8 |
Correct |
141 ms |
55076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22872 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
23040 KB |
Output is correct |
4 |
Correct |
5 ms |
22972 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
5 ms |
22872 KB |
Output is correct |
10 |
Correct |
5 ms |
22876 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
5 ms |
22876 KB |
Output is correct |
13 |
Correct |
5 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22872 KB |
Output is correct |
15 |
Correct |
5 ms |
23092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22872 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
23040 KB |
Output is correct |
4 |
Correct |
5 ms |
22972 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
5 ms |
22872 KB |
Output is correct |
10 |
Correct |
5 ms |
22876 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
5 ms |
22876 KB |
Output is correct |
13 |
Correct |
5 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22872 KB |
Output is correct |
15 |
Correct |
5 ms |
23092 KB |
Output is correct |
16 |
Correct |
8 ms |
23388 KB |
Output is correct |
17 |
Correct |
7 ms |
23388 KB |
Output is correct |
18 |
Correct |
9 ms |
23644 KB |
Output is correct |
19 |
Correct |
8 ms |
23500 KB |
Output is correct |
20 |
Correct |
6 ms |
23408 KB |
Output is correct |
21 |
Correct |
7 ms |
23388 KB |
Output is correct |
22 |
Correct |
6 ms |
23388 KB |
Output is correct |
23 |
Correct |
7 ms |
23640 KB |
Output is correct |
24 |
Correct |
7 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22872 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
23040 KB |
Output is correct |
4 |
Correct |
5 ms |
22972 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
5 ms |
22876 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
5 ms |
22872 KB |
Output is correct |
10 |
Correct |
5 ms |
22876 KB |
Output is correct |
11 |
Correct |
5 ms |
22876 KB |
Output is correct |
12 |
Correct |
5 ms |
22876 KB |
Output is correct |
13 |
Correct |
5 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22872 KB |
Output is correct |
15 |
Correct |
5 ms |
23092 KB |
Output is correct |
16 |
Correct |
8 ms |
23388 KB |
Output is correct |
17 |
Correct |
7 ms |
23388 KB |
Output is correct |
18 |
Correct |
9 ms |
23644 KB |
Output is correct |
19 |
Correct |
8 ms |
23500 KB |
Output is correct |
20 |
Correct |
6 ms |
23408 KB |
Output is correct |
21 |
Correct |
7 ms |
23388 KB |
Output is correct |
22 |
Correct |
6 ms |
23388 KB |
Output is correct |
23 |
Correct |
7 ms |
23640 KB |
Output is correct |
24 |
Correct |
7 ms |
23388 KB |
Output is correct |
25 |
Correct |
5 ms |
22876 KB |
Output is correct |
26 |
Correct |
5 ms |
23040 KB |
Output is correct |
27 |
Correct |
8 ms |
23388 KB |
Output is correct |
28 |
Correct |
9 ms |
23388 KB |
Output is correct |
29 |
Correct |
8 ms |
23436 KB |
Output is correct |
30 |
Correct |
7 ms |
23388 KB |
Output is correct |
31 |
Correct |
8 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
5 ms |
22876 KB |
Output is correct |
4 |
Correct |
595 ms |
74976 KB |
Output is correct |
5 |
Correct |
269 ms |
54880 KB |
Output is correct |
6 |
Correct |
149 ms |
51160 KB |
Output is correct |
7 |
Correct |
180 ms |
47164 KB |
Output is correct |
8 |
Correct |
141 ms |
55076 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
5 ms |
23040 KB |
Output is correct |
12 |
Correct |
5 ms |
22972 KB |
Output is correct |
13 |
Correct |
5 ms |
22872 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
5 ms |
22872 KB |
Output is correct |
18 |
Correct |
5 ms |
22876 KB |
Output is correct |
19 |
Correct |
5 ms |
22876 KB |
Output is correct |
20 |
Correct |
5 ms |
22876 KB |
Output is correct |
21 |
Correct |
5 ms |
22876 KB |
Output is correct |
22 |
Correct |
5 ms |
22872 KB |
Output is correct |
23 |
Correct |
5 ms |
23092 KB |
Output is correct |
24 |
Correct |
8 ms |
23388 KB |
Output is correct |
25 |
Correct |
7 ms |
23388 KB |
Output is correct |
26 |
Correct |
9 ms |
23644 KB |
Output is correct |
27 |
Correct |
8 ms |
23500 KB |
Output is correct |
28 |
Correct |
6 ms |
23408 KB |
Output is correct |
29 |
Correct |
7 ms |
23388 KB |
Output is correct |
30 |
Correct |
6 ms |
23388 KB |
Output is correct |
31 |
Correct |
7 ms |
23640 KB |
Output is correct |
32 |
Correct |
7 ms |
23388 KB |
Output is correct |
33 |
Correct |
5 ms |
22876 KB |
Output is correct |
34 |
Correct |
5 ms |
23040 KB |
Output is correct |
35 |
Correct |
8 ms |
23388 KB |
Output is correct |
36 |
Correct |
9 ms |
23388 KB |
Output is correct |
37 |
Correct |
8 ms |
23436 KB |
Output is correct |
38 |
Correct |
7 ms |
23388 KB |
Output is correct |
39 |
Correct |
8 ms |
23388 KB |
Output is correct |
40 |
Correct |
600 ms |
76260 KB |
Output is correct |
41 |
Correct |
308 ms |
56448 KB |
Output is correct |
42 |
Correct |
380 ms |
83296 KB |
Output is correct |
43 |
Correct |
374 ms |
83484 KB |
Output is correct |
44 |
Correct |
187 ms |
51580 KB |
Output is correct |
45 |
Correct |
237 ms |
59420 KB |
Output is correct |
46 |
Correct |
109 ms |
35976 KB |
Output is correct |
47 |
Correct |
347 ms |
58888 KB |
Output is correct |
48 |
Correct |
322 ms |
66444 KB |
Output is correct |