#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9+69;
const int MAXN = 2e5+3;
int nArr, q, x;
int numNode;
vector<pair<int,int>> e[6*MAXN];
int ans;
int f[6*MAXN];
int ID[6*MAXN];
bool vis[6*MAXN];
void STbuild(int idx=1, int l=1, int r=nArr){
if(l==r){
e[l].emplace_back(ID[idx], 0);
return;
}
int mid = l+r>>1;
e[ID[idx<<1]].emplace_back(ID[idx], 0);
e[ID[idx<<1|1]].emplace_back(ID[idx], 0);
STbuild(idx<<1, l, mid);
STbuild(idx<<1|1, mid+1, r);
}
void STunion(int z, int u, int v, int idx=1, int l=1, int r=nArr){
if(v<l || r<u)return;
if(u<=l && r<=v){
e[ID[idx]].push_back({z, 0});
return;
}
int mid = l+r>>1;
STunion(z,u,v,idx<<1,l,mid);
STunion(z,u,v,idx<<1|1,mid+1,r);
}
int S[2][6*MAXN];
void dijsktra(int x, int f[]){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, x);
for(int i=1; i<=numNode; ++i) f[i] = INF;
f[x] = 0;
while(!pq.empty()){
pair<int,int> z = pq.top(); pq.pop();
int du = z.first,
u = z.second;
if(du>f[u])continue;
for(pair<int,int> &z : e[u]){
int v = z.first,
w = z.second;
if(f[u]+w < f[v]){
f[v] = f[u]+w;
pq.emplace(f[v], v);
}
}
cout << '\n';
}
}
signed main()
{
cin.tie(0) -> sync_with_stdio(0);
cin >> nArr;
numNode = nArr;
for(int i=1; i<=4*nArr; ++i) ID[i] = ++numNode;
STbuild();
for(int l, r, i=1; i<=nArr; ++i){
cin >> l >> r;
STunion(++numNode, l, r);
e[numNode].emplace_back(i, 1);
}
dijsktra(1, S[0]);
dijsktra(nArr, S[1]);
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
for(int i=1; i<=numNode; ++i){
f[i] = S[0][i] + S[1][i];
pq.emplace(f[i], i);
}
// for(int i=1; i<=numNode; ++i) cout << S[0][i] << ' '; cout << '\n';
// for(int i=1; i<=numNode; ++i) cout << S[1][i] << ' '; cout << '\n';
// for(int i=1; i<=numNode; ++i) cout << f[i] << ' '; cout << '\n';
while(!pq.empty()){
pair<int,int> z = pq.top(); pq.pop();
int du = z.first,
u = z.second;
if(du>f[u])continue;
// cout << u << '\n';
for(pair<int,int> &z : e[u]){
int v = z.first,
w = z.second;
if(f[u]+w < f[v]){
f[v] = f[u]+w;
pq.emplace(f[v], v);
}
}
}
// for(int i=1; i<=numNode; ++i) cout << f[i] << ' '; cout << '\n';
cin >> q;
while(q--){
cin >> x;
cout << (f[x]>=INF ? -1 : f[x]) << '\n';
}
return 0;
}
Compilation message
passport.cpp: In function 'void STbuild(int, int, int)':
passport.cpp:26:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'void STunion(int, int, int, int, int, int)':
passport.cpp:39:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid = l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28508 KB |
Output is correct |
2 |
Correct |
12 ms |
28628 KB |
Output is correct |
3 |
Correct |
13 ms |
28508 KB |
Output is correct |
4 |
Correct |
947 ms |
115772 KB |
Output is correct |
5 |
Correct |
566 ms |
96704 KB |
Output is correct |
6 |
Correct |
318 ms |
92088 KB |
Output is correct |
7 |
Correct |
403 ms |
90676 KB |
Output is correct |
8 |
Correct |
224 ms |
84596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28504 KB |
Output is correct |
2 |
Correct |
12 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28508 KB |
Output is correct |
4 |
Correct |
12 ms |
28504 KB |
Output is correct |
5 |
Correct |
11 ms |
28516 KB |
Output is correct |
6 |
Correct |
11 ms |
28508 KB |
Output is correct |
7 |
Correct |
12 ms |
28436 KB |
Output is correct |
8 |
Correct |
12 ms |
28592 KB |
Output is correct |
9 |
Correct |
12 ms |
28508 KB |
Output is correct |
10 |
Correct |
12 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28716 KB |
Output is correct |
12 |
Correct |
14 ms |
28764 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
15 ms |
28760 KB |
Output is correct |
15 |
Correct |
13 ms |
28764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28504 KB |
Output is correct |
2 |
Correct |
12 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28508 KB |
Output is correct |
4 |
Correct |
12 ms |
28504 KB |
Output is correct |
5 |
Correct |
11 ms |
28516 KB |
Output is correct |
6 |
Correct |
11 ms |
28508 KB |
Output is correct |
7 |
Correct |
12 ms |
28436 KB |
Output is correct |
8 |
Correct |
12 ms |
28592 KB |
Output is correct |
9 |
Correct |
12 ms |
28508 KB |
Output is correct |
10 |
Correct |
12 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28716 KB |
Output is correct |
12 |
Correct |
14 ms |
28764 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
15 ms |
28760 KB |
Output is correct |
15 |
Correct |
13 ms |
28764 KB |
Output is correct |
16 |
Correct |
17 ms |
29532 KB |
Output is correct |
17 |
Correct |
17 ms |
29528 KB |
Output is correct |
18 |
Correct |
18 ms |
29788 KB |
Output is correct |
19 |
Correct |
17 ms |
29532 KB |
Output is correct |
20 |
Correct |
16 ms |
29276 KB |
Output is correct |
21 |
Correct |
16 ms |
29532 KB |
Output is correct |
22 |
Correct |
15 ms |
29272 KB |
Output is correct |
23 |
Correct |
20 ms |
29420 KB |
Output is correct |
24 |
Correct |
15 ms |
29604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28504 KB |
Output is correct |
2 |
Correct |
12 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28508 KB |
Output is correct |
4 |
Correct |
12 ms |
28504 KB |
Output is correct |
5 |
Correct |
11 ms |
28516 KB |
Output is correct |
6 |
Correct |
11 ms |
28508 KB |
Output is correct |
7 |
Correct |
12 ms |
28436 KB |
Output is correct |
8 |
Correct |
12 ms |
28592 KB |
Output is correct |
9 |
Correct |
12 ms |
28508 KB |
Output is correct |
10 |
Correct |
12 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28716 KB |
Output is correct |
12 |
Correct |
14 ms |
28764 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
15 ms |
28760 KB |
Output is correct |
15 |
Correct |
13 ms |
28764 KB |
Output is correct |
16 |
Correct |
17 ms |
29532 KB |
Output is correct |
17 |
Correct |
17 ms |
29528 KB |
Output is correct |
18 |
Correct |
18 ms |
29788 KB |
Output is correct |
19 |
Correct |
17 ms |
29532 KB |
Output is correct |
20 |
Correct |
16 ms |
29276 KB |
Output is correct |
21 |
Correct |
16 ms |
29532 KB |
Output is correct |
22 |
Correct |
15 ms |
29272 KB |
Output is correct |
23 |
Correct |
20 ms |
29420 KB |
Output is correct |
24 |
Correct |
15 ms |
29604 KB |
Output is correct |
25 |
Correct |
13 ms |
28508 KB |
Output is correct |
26 |
Correct |
14 ms |
28644 KB |
Output is correct |
27 |
Correct |
18 ms |
29528 KB |
Output is correct |
28 |
Correct |
19 ms |
29532 KB |
Output is correct |
29 |
Correct |
16 ms |
29432 KB |
Output is correct |
30 |
Correct |
18 ms |
29416 KB |
Output is correct |
31 |
Correct |
18 ms |
29532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
28508 KB |
Output is correct |
2 |
Correct |
12 ms |
28628 KB |
Output is correct |
3 |
Correct |
13 ms |
28508 KB |
Output is correct |
4 |
Correct |
947 ms |
115772 KB |
Output is correct |
5 |
Correct |
566 ms |
96704 KB |
Output is correct |
6 |
Correct |
318 ms |
92088 KB |
Output is correct |
7 |
Correct |
403 ms |
90676 KB |
Output is correct |
8 |
Correct |
224 ms |
84596 KB |
Output is correct |
9 |
Correct |
12 ms |
28504 KB |
Output is correct |
10 |
Correct |
12 ms |
28508 KB |
Output is correct |
11 |
Correct |
13 ms |
28508 KB |
Output is correct |
12 |
Correct |
12 ms |
28504 KB |
Output is correct |
13 |
Correct |
11 ms |
28516 KB |
Output is correct |
14 |
Correct |
11 ms |
28508 KB |
Output is correct |
15 |
Correct |
12 ms |
28436 KB |
Output is correct |
16 |
Correct |
12 ms |
28592 KB |
Output is correct |
17 |
Correct |
12 ms |
28508 KB |
Output is correct |
18 |
Correct |
12 ms |
28508 KB |
Output is correct |
19 |
Correct |
12 ms |
28716 KB |
Output is correct |
20 |
Correct |
14 ms |
28764 KB |
Output is correct |
21 |
Correct |
13 ms |
28764 KB |
Output is correct |
22 |
Correct |
15 ms |
28760 KB |
Output is correct |
23 |
Correct |
13 ms |
28764 KB |
Output is correct |
24 |
Correct |
17 ms |
29532 KB |
Output is correct |
25 |
Correct |
17 ms |
29528 KB |
Output is correct |
26 |
Correct |
18 ms |
29788 KB |
Output is correct |
27 |
Correct |
17 ms |
29532 KB |
Output is correct |
28 |
Correct |
16 ms |
29276 KB |
Output is correct |
29 |
Correct |
16 ms |
29532 KB |
Output is correct |
30 |
Correct |
15 ms |
29272 KB |
Output is correct |
31 |
Correct |
20 ms |
29420 KB |
Output is correct |
32 |
Correct |
15 ms |
29604 KB |
Output is correct |
33 |
Correct |
13 ms |
28508 KB |
Output is correct |
34 |
Correct |
14 ms |
28644 KB |
Output is correct |
35 |
Correct |
18 ms |
29528 KB |
Output is correct |
36 |
Correct |
19 ms |
29532 KB |
Output is correct |
37 |
Correct |
16 ms |
29432 KB |
Output is correct |
38 |
Correct |
18 ms |
29416 KB |
Output is correct |
39 |
Correct |
18 ms |
29532 KB |
Output is correct |
40 |
Correct |
999 ms |
116208 KB |
Output is correct |
41 |
Correct |
674 ms |
97240 KB |
Output is correct |
42 |
Correct |
728 ms |
121620 KB |
Output is correct |
43 |
Correct |
629 ms |
121756 KB |
Output is correct |
44 |
Correct |
326 ms |
92132 KB |
Output is correct |
45 |
Correct |
465 ms |
98524 KB |
Output is correct |
46 |
Correct |
198 ms |
52164 KB |
Output is correct |
47 |
Correct |
675 ms |
99740 KB |
Output is correct |
48 |
Correct |
498 ms |
107092 KB |
Output is correct |