#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define int int64_t
const int INF = 1e18+69;
const int MAXN = 2e5+3;
int n, q, x;
vector<pair<int,int>> e[4*MAXN];
int ans;
int f[4*MAXN];
int ID[MAXN];
bool vis[4*MAXN];
void STbuild(int idx=1, int l=1, int r=n){
if(l==r){
ID[l] = idx;
return;
}
int mid = l+r>>1;
e[idx].push_back({idx<<1, 0});
e[idx].push_back({idx<<1|1, 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=n){
if(v<l || r<u)return;
if(u<=l && r<=v){
// if(idx==ID[z])return;
e[ID[z]].push_back({idx, 1});
return;
}
int mid = l+r>>1;
STunion(z,u,v,idx<<1,l,mid);
STunion(z,u,v,idx<<1|1,mid+1,r);
}
// class Compare{
// public: bool operator()(const int &a, const int &b){
// return f[a] > f[b];
// }};
// priority_queue<int, vector<int>, Compare> pq;
queue<int> pq;
void solve(){
for(int i=1; i<=4*n; ++i) f[i] = INF, vis[i] = 0;
pq.push(ID[x]);
f[ID[x]] = 0;
vis[ID[x]] = 1;
while(!pq.empty()){
int u = pq.front(); pq.pop();
// cout << u << ' ';
for(pair<int,int> &z : e[u]){
int v = z.first,
w = z.second;
if(vis[v])continue;
pq.push(v);
vis[v] = 1;
f[v] = min(f[v],f[u]+w);
}
}
// cout << '\n';
ans = 0;
for(int i=1; i<=n; ++i)ans=max(ans,f[ID[i]]);
cout << (ans>=INF ? -1 : ans) << '\n';
}
signed main()
{
cin.tie(0) -> sync_with_stdio(0);
cin >> n;
STbuild();
for(int l, r, i=1; i<=n; ++i){
cin >> l >> r;
STunion(i, l, r);
}
cin >> q;
while(q--){
cin >> x;
solve();
}
return 0;
}
Compilation message
passport.cpp: In function 'void STbuild(int64_t, int64_t, int64_t)':
passport.cpp:27:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int mid = l+r>>1;
| ~^~
passport.cpp: In function 'void STunion(int64_t, int64_t, int64_t, int64_t, int64_t, int64_t)':
passport.cpp:41:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19036 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Incorrect |
221 ms |
102640 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19032 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
10 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
9 ms |
19268 KB |
Output is correct |
7 |
Incorrect |
11 ms |
19032 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19032 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
10 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
9 ms |
19268 KB |
Output is correct |
7 |
Incorrect |
11 ms |
19032 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19032 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
10 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
9 ms |
19268 KB |
Output is correct |
7 |
Incorrect |
11 ms |
19032 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19036 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Incorrect |
221 ms |
102640 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |