#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define str string
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define all(x) x.begin() , x.end()
#define setpr(x) cout << fixed << setprecision(x)
#define endl '\n'
const int inf = INT_MAX;
const ld eps = 1e-9 , pi = acos(-1.0);
const ll mod = 1e9 + 7; // 998244353;
const int dx[4]{1 , 0 , -1 , 0} , dy[4]{0 , 1 , 0 , -1};
vector<vector<int>> graf;
int n;
int bfs(int x){
vector<int> dist(n + 1 , -1) , qat(n + 1 , -1);
queue<int> q;
auto add = [&](int j , int i , int p){
if(dist[i] == -1){
dist[i] = p;
q.push(i);
qat[i] = j;
}
};
add(x , x , 0);
while(!q.empty()){
int u = q.front();
q.pop();
for(int v : graf[u]) if(u != v) add(u , v , dist[u] + 1);
}
if(dist[1] == -1 || dist[n] == -1) return -1;
set<int> st1;
int h1 = 1 , h2 = n;
while(h1 != x){
h1 = qat[h1];
st1.insert(h1);
}
while(h2 != x){
h2 = qat[h2];
st1.insert(h2);
}
return st1.size();
}
void solution(){
cin >> n;
vector<pair<int , int>> bu(n);
for(int i = 0 ; i < n ; i ++) cin >> bu[i].F >> bu[i].S;
graf.resize(n + 1);
for(int i = 0 ; i < n ; i ++){
for(int j = bu[i].F ; j <= bu[i].S ; j ++){
graf[i + 1].pb(j);
}
}
int q; cin >> q;
while(q --){
int x; cin >> x;
cout << bfs(x) << endl;
}
}
signed main(){
IOS;
int t = 1; // cin >> t;
while(t --) solution();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
795 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Incorrect |
0 ms |
352 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Incorrect |
0 ms |
352 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Incorrect |
0 ms |
352 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
795 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |