#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[5*MAXN];
int ans;
int f[5*MAXN];
int ID[5*MAXN];
bool vis[5*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][5*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;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23720 KB |
Output is correct |
3 |
Correct |
13 ms |
24152 KB |
Output is correct |
4 |
Runtime error |
85 ms |
98396 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23972 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23928 KB |
Output is correct |
6 |
Correct |
9 ms |
23972 KB |
Output is correct |
7 |
Correct |
10 ms |
23744 KB |
Output is correct |
8 |
Correct |
10 ms |
23952 KB |
Output is correct |
9 |
Correct |
9 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23756 KB |
Output is correct |
11 |
Correct |
10 ms |
23852 KB |
Output is correct |
12 |
Correct |
12 ms |
23896 KB |
Output is correct |
13 |
Correct |
15 ms |
23860 KB |
Output is correct |
14 |
Correct |
10 ms |
23900 KB |
Output is correct |
15 |
Correct |
10 ms |
24076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23972 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23928 KB |
Output is correct |
6 |
Correct |
9 ms |
23972 KB |
Output is correct |
7 |
Correct |
10 ms |
23744 KB |
Output is correct |
8 |
Correct |
10 ms |
23952 KB |
Output is correct |
9 |
Correct |
9 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23756 KB |
Output is correct |
11 |
Correct |
10 ms |
23852 KB |
Output is correct |
12 |
Correct |
12 ms |
23896 KB |
Output is correct |
13 |
Correct |
15 ms |
23860 KB |
Output is correct |
14 |
Correct |
10 ms |
23900 KB |
Output is correct |
15 |
Correct |
10 ms |
24076 KB |
Output is correct |
16 |
Correct |
15 ms |
24920 KB |
Output is correct |
17 |
Correct |
14 ms |
24664 KB |
Output is correct |
18 |
Correct |
15 ms |
24924 KB |
Output is correct |
19 |
Correct |
14 ms |
24924 KB |
Output is correct |
20 |
Correct |
13 ms |
24668 KB |
Output is correct |
21 |
Correct |
13 ms |
24664 KB |
Output is correct |
22 |
Correct |
12 ms |
24668 KB |
Output is correct |
23 |
Correct |
16 ms |
24668 KB |
Output is correct |
24 |
Correct |
13 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23972 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23928 KB |
Output is correct |
6 |
Correct |
9 ms |
23972 KB |
Output is correct |
7 |
Correct |
10 ms |
23744 KB |
Output is correct |
8 |
Correct |
10 ms |
23952 KB |
Output is correct |
9 |
Correct |
9 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23756 KB |
Output is correct |
11 |
Correct |
10 ms |
23852 KB |
Output is correct |
12 |
Correct |
12 ms |
23896 KB |
Output is correct |
13 |
Correct |
15 ms |
23860 KB |
Output is correct |
14 |
Correct |
10 ms |
23900 KB |
Output is correct |
15 |
Correct |
10 ms |
24076 KB |
Output is correct |
16 |
Correct |
15 ms |
24920 KB |
Output is correct |
17 |
Correct |
14 ms |
24664 KB |
Output is correct |
18 |
Correct |
15 ms |
24924 KB |
Output is correct |
19 |
Correct |
14 ms |
24924 KB |
Output is correct |
20 |
Correct |
13 ms |
24668 KB |
Output is correct |
21 |
Correct |
13 ms |
24664 KB |
Output is correct |
22 |
Correct |
12 ms |
24668 KB |
Output is correct |
23 |
Correct |
16 ms |
24668 KB |
Output is correct |
24 |
Correct |
13 ms |
24924 KB |
Output is correct |
25 |
Correct |
10 ms |
23896 KB |
Output is correct |
26 |
Correct |
10 ms |
23900 KB |
Output is correct |
27 |
Correct |
15 ms |
24884 KB |
Output is correct |
28 |
Correct |
14 ms |
24668 KB |
Output is correct |
29 |
Correct |
16 ms |
24924 KB |
Output is correct |
30 |
Correct |
20 ms |
24668 KB |
Output is correct |
31 |
Correct |
19 ms |
24672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23720 KB |
Output is correct |
3 |
Correct |
13 ms |
24152 KB |
Output is correct |
4 |
Runtime error |
85 ms |
98396 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |