#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"
const double PI=3.141592653589;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
void solve()
{
int n;
cin >> n;
vector<pair<int,int>>range(n+1);
for(int i = 1;i<=n;++i){
int l,r;
cin >> l >> r;
range[i] = {l,r};
}
int Q;
cin >> Q;
while(Q--){
int x;
cin >> x;
vector<vector<int>>dist(n+1, vector<int>(n+1,1e18));
vector<vector<int>>visited(n+1, vector<int>(n+1,0));
priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;
dist[range[x].first][range[x].second] = 1;
pq.push({1,range[x].first,range[x].second});
while(!pq.empty()){
int l = pq.top()[1] ,r = pq.top()[2];
pq.pop();
if(visited[l][r])continue;
visited[l][r] = 1;
for(int i = l;i<=r;++i){
int k = min(l,range[i].first), j = max(r,range[i].second);
int curr_dist = dist[k][j], new_dist = dist[l][r]+1;
if(new_dist < curr_dist){
pq.push({new_dist,k,j});
dist[k][j] = new_dist;
}
}
}
int res = dist[1][n];
if(res>=1e18)res = -1;
cout << res << endl;
}
}
/*
5
1 1
2 3
1 5
3 4
5 5
1
2
*/
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T=1;
for(int i = 1;i<=T;++i)
{
// cout << "Case #" << i << ": ";
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Runtime error |
425 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
1684 KB |
Output is correct |
12 |
Correct |
4 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
5 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
1684 KB |
Output is correct |
12 |
Correct |
4 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
5 ms |
1748 KB |
Output is correct |
16 |
Correct |
1668 ms |
97040 KB |
Output is correct |
17 |
Correct |
1227 ms |
97616 KB |
Output is correct |
18 |
Correct |
59 ms |
98376 KB |
Output is correct |
19 |
Correct |
76 ms |
93384 KB |
Output is correct |
20 |
Execution timed out |
2079 ms |
98484 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
1684 KB |
Output is correct |
12 |
Correct |
4 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1620 KB |
Output is correct |
15 |
Correct |
5 ms |
1748 KB |
Output is correct |
16 |
Correct |
1668 ms |
97040 KB |
Output is correct |
17 |
Correct |
1227 ms |
97616 KB |
Output is correct |
18 |
Correct |
59 ms |
98376 KB |
Output is correct |
19 |
Correct |
76 ms |
93384 KB |
Output is correct |
20 |
Execution timed out |
2079 ms |
98484 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Runtime error |
425 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |