#pragma GCC optimize("Ofast")
#pragma GCC optimize("O2,O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
//typedef tuple<int,int,int> tp;
const int N = 1e6 + 5;
//const int mod = 1e9 + 7;
const int oo = 1e8;
int n, L[N], R[N], h[N << 2], id[N], f[N << 2];
vector<pii> p[N << 2];
void build(int i,int l,int r){
if(l == r){
id[l] = i;
h[i] = 1;
return;
}
int mid = (l + r) / 2;
build(i << 1, l, mid);
build(i << 1|1, mid + 1, r);
p[i << 1].pb({i, 0});
p[i << 1|1].pb({i, 0});
}
void update(int i,int l,int r,int u,int v,int x){
if(l > r || l > v || r < u) return;
if(u <= l && r <= v){
p[i].pb({id[x], 1});
return;
}
int mid = (l + r) / 2;
update(i << 1, l, mid, u, v, x);
update(i << 1|1, mid + 1, r, u, v, x);
}
int d[N << 2][5];
void cal(int st,int j){
d[id[st]][j] = 0;
priority_queue<pii, vector<pii>, greater<pii>> q; q.push({0, id[st]});
while(!q.empty()){
auto [g, u] = q.top();
q.pop();
if(g > d[u][j]) continue;
for(auto [v, w] : p[u]) if(d[u][j] + w < d[v][j]){
d[v][j] = d[u][j] + w;
q.push({d[v][j], v});
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n;
build(1, 1, n);
for(int i = 1; i <= n; i ++){
cin >> L[i] >> R[i];
update(1, 1, n, L[i], R[i], i);
}
for(int i = 1; i <= 4 * n; i ++) d[i][0] = d[i][1] = oo;
cal(1, 0);
cal(n, 1);
priority_queue<pii, vector<pii>, greater<pii>> pq;
for(int i = 1; i <= 4 * n; i ++){
if(id[1] == i || id[n] == i) f[i] = d[i][1] + d[i][0];
else f[i] = d[i][1] + d[i][0] - h[i];
pq.push({f[i], i});
}
while(!pq.empty()){
auto [w, u] = pq.top();
pq.pop();
if(w > f[u]) continue;
for(auto [v, g] : p[u]) if(w + g < f[v]) pq.push({f[v] = w + g, v});
}
int q;
cin >> q;
while(q--){
int x; cin >> x;
cout << (f[id[x]] >= oo ? -1 : f[id[x]]) << "\n";
}
}
Compilation message
passport.cpp: In function 'int main()':
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(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103000 KB |
Output is correct |
2 |
Correct |
27 ms |
103004 KB |
Output is correct |
3 |
Correct |
27 ms |
103004 KB |
Output is correct |
4 |
Correct |
838 ms |
230420 KB |
Output is correct |
5 |
Correct |
397 ms |
191172 KB |
Output is correct |
6 |
Correct |
256 ms |
185588 KB |
Output is correct |
7 |
Correct |
272 ms |
177540 KB |
Output is correct |
8 |
Correct |
238 ms |
185752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
103004 KB |
Output is correct |
2 |
Correct |
27 ms |
103000 KB |
Output is correct |
3 |
Correct |
26 ms |
102912 KB |
Output is correct |
4 |
Correct |
28 ms |
103000 KB |
Output is correct |
5 |
Correct |
30 ms |
103124 KB |
Output is correct |
6 |
Correct |
27 ms |
103004 KB |
Output is correct |
7 |
Correct |
27 ms |
103060 KB |
Output is correct |
8 |
Correct |
27 ms |
103068 KB |
Output is correct |
9 |
Correct |
27 ms |
103000 KB |
Output is correct |
10 |
Correct |
28 ms |
102964 KB |
Output is correct |
11 |
Correct |
28 ms |
103004 KB |
Output is correct |
12 |
Correct |
29 ms |
103004 KB |
Output is correct |
13 |
Correct |
28 ms |
102992 KB |
Output is correct |
14 |
Correct |
28 ms |
103004 KB |
Output is correct |
15 |
Correct |
27 ms |
103000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
103004 KB |
Output is correct |
2 |
Correct |
27 ms |
103000 KB |
Output is correct |
3 |
Correct |
26 ms |
102912 KB |
Output is correct |
4 |
Correct |
28 ms |
103000 KB |
Output is correct |
5 |
Correct |
30 ms |
103124 KB |
Output is correct |
6 |
Correct |
27 ms |
103004 KB |
Output is correct |
7 |
Correct |
27 ms |
103060 KB |
Output is correct |
8 |
Correct |
27 ms |
103068 KB |
Output is correct |
9 |
Correct |
27 ms |
103000 KB |
Output is correct |
10 |
Correct |
28 ms |
102964 KB |
Output is correct |
11 |
Correct |
28 ms |
103004 KB |
Output is correct |
12 |
Correct |
29 ms |
103004 KB |
Output is correct |
13 |
Correct |
28 ms |
102992 KB |
Output is correct |
14 |
Correct |
28 ms |
103004 KB |
Output is correct |
15 |
Correct |
27 ms |
103000 KB |
Output is correct |
16 |
Correct |
32 ms |
108112 KB |
Output is correct |
17 |
Correct |
31 ms |
107860 KB |
Output is correct |
18 |
Correct |
32 ms |
108380 KB |
Output is correct |
19 |
Correct |
32 ms |
108380 KB |
Output is correct |
20 |
Correct |
30 ms |
107856 KB |
Output is correct |
21 |
Correct |
33 ms |
108120 KB |
Output is correct |
22 |
Correct |
30 ms |
107800 KB |
Output is correct |
23 |
Correct |
32 ms |
108120 KB |
Output is correct |
24 |
Correct |
31 ms |
108124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
103004 KB |
Output is correct |
2 |
Correct |
27 ms |
103000 KB |
Output is correct |
3 |
Correct |
26 ms |
102912 KB |
Output is correct |
4 |
Correct |
28 ms |
103000 KB |
Output is correct |
5 |
Correct |
30 ms |
103124 KB |
Output is correct |
6 |
Correct |
27 ms |
103004 KB |
Output is correct |
7 |
Correct |
27 ms |
103060 KB |
Output is correct |
8 |
Correct |
27 ms |
103068 KB |
Output is correct |
9 |
Correct |
27 ms |
103000 KB |
Output is correct |
10 |
Correct |
28 ms |
102964 KB |
Output is correct |
11 |
Correct |
28 ms |
103004 KB |
Output is correct |
12 |
Correct |
29 ms |
103004 KB |
Output is correct |
13 |
Correct |
28 ms |
102992 KB |
Output is correct |
14 |
Correct |
28 ms |
103004 KB |
Output is correct |
15 |
Correct |
27 ms |
103000 KB |
Output is correct |
16 |
Correct |
32 ms |
108112 KB |
Output is correct |
17 |
Correct |
31 ms |
107860 KB |
Output is correct |
18 |
Correct |
32 ms |
108380 KB |
Output is correct |
19 |
Correct |
32 ms |
108380 KB |
Output is correct |
20 |
Correct |
30 ms |
107856 KB |
Output is correct |
21 |
Correct |
33 ms |
108120 KB |
Output is correct |
22 |
Correct |
30 ms |
107800 KB |
Output is correct |
23 |
Correct |
32 ms |
108120 KB |
Output is correct |
24 |
Correct |
31 ms |
108124 KB |
Output is correct |
25 |
Correct |
27 ms |
103000 KB |
Output is correct |
26 |
Correct |
27 ms |
103004 KB |
Output is correct |
27 |
Correct |
32 ms |
108280 KB |
Output is correct |
28 |
Correct |
32 ms |
108052 KB |
Output is correct |
29 |
Correct |
31 ms |
107864 KB |
Output is correct |
30 |
Correct |
32 ms |
108004 KB |
Output is correct |
31 |
Correct |
31 ms |
108000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
103000 KB |
Output is correct |
2 |
Correct |
27 ms |
103004 KB |
Output is correct |
3 |
Correct |
27 ms |
103004 KB |
Output is correct |
4 |
Correct |
838 ms |
230420 KB |
Output is correct |
5 |
Correct |
397 ms |
191172 KB |
Output is correct |
6 |
Correct |
256 ms |
185588 KB |
Output is correct |
7 |
Correct |
272 ms |
177540 KB |
Output is correct |
8 |
Correct |
238 ms |
185752 KB |
Output is correct |
9 |
Correct |
27 ms |
103004 KB |
Output is correct |
10 |
Correct |
27 ms |
103000 KB |
Output is correct |
11 |
Correct |
26 ms |
102912 KB |
Output is correct |
12 |
Correct |
28 ms |
103000 KB |
Output is correct |
13 |
Correct |
30 ms |
103124 KB |
Output is correct |
14 |
Correct |
27 ms |
103004 KB |
Output is correct |
15 |
Correct |
27 ms |
103060 KB |
Output is correct |
16 |
Correct |
27 ms |
103068 KB |
Output is correct |
17 |
Correct |
27 ms |
103000 KB |
Output is correct |
18 |
Correct |
28 ms |
102964 KB |
Output is correct |
19 |
Correct |
28 ms |
103004 KB |
Output is correct |
20 |
Correct |
29 ms |
103004 KB |
Output is correct |
21 |
Correct |
28 ms |
102992 KB |
Output is correct |
22 |
Correct |
28 ms |
103004 KB |
Output is correct |
23 |
Correct |
27 ms |
103000 KB |
Output is correct |
24 |
Correct |
32 ms |
108112 KB |
Output is correct |
25 |
Correct |
31 ms |
107860 KB |
Output is correct |
26 |
Correct |
32 ms |
108380 KB |
Output is correct |
27 |
Correct |
32 ms |
108380 KB |
Output is correct |
28 |
Correct |
30 ms |
107856 KB |
Output is correct |
29 |
Correct |
33 ms |
108120 KB |
Output is correct |
30 |
Correct |
30 ms |
107800 KB |
Output is correct |
31 |
Correct |
32 ms |
108120 KB |
Output is correct |
32 |
Correct |
31 ms |
108124 KB |
Output is correct |
33 |
Correct |
27 ms |
103000 KB |
Output is correct |
34 |
Correct |
27 ms |
103004 KB |
Output is correct |
35 |
Correct |
32 ms |
108280 KB |
Output is correct |
36 |
Correct |
32 ms |
108052 KB |
Output is correct |
37 |
Correct |
31 ms |
107864 KB |
Output is correct |
38 |
Correct |
32 ms |
108004 KB |
Output is correct |
39 |
Correct |
31 ms |
108000 KB |
Output is correct |
40 |
Correct |
880 ms |
234712 KB |
Output is correct |
41 |
Correct |
441 ms |
197060 KB |
Output is correct |
42 |
Correct |
544 ms |
248664 KB |
Output is correct |
43 |
Correct |
544 ms |
248932 KB |
Output is correct |
44 |
Correct |
279 ms |
186536 KB |
Output is correct |
45 |
Correct |
372 ms |
200696 KB |
Output is correct |
46 |
Correct |
185 ms |
141640 KB |
Output is correct |
47 |
Correct |
546 ms |
202508 KB |
Output is correct |
48 |
Correct |
445 ms |
216856 KB |
Output is correct |