#include<bits/stdc++.h>
using namespace std;
typedef int ll;
#define x first
#define y second
#pragma optimize("O3")
vector<vector<ll>> G( 5005);
vector<ll> v[5005];
vector<pair<ll,ll>> input;
void cmc(ll n,ll ini)
{
v[ini].assign(n+1,1e9);
queue<ll> q;
q.push(ini);
v[ini][ini]=0;
map<ll,ll> mapa;
while(!q.empty())
{
ll a=q.front();q.pop();
for(auto i:G[a])
{
if(v[ini][i]==1e9)
{
v[ini][i]=v[ini][a]+1;
if(mapa[input[i].y]==0)
{
q.push(i);
mapa[input[i].y]=1;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll n,q;
cin>>n>>q;
input.push_back({0,0});
vector<pair<ll,ll>> cad;
for(int i=1; i<=n; i++)
{
ll a,b;
cin>>a>>b;
input.push_back({a,b});
cad.push_back({a,i});
cad.push_back({b,i});
}
sort(cad.begin(),cad.end());
set<ll> s;
for(int i=0; i<cad.size(); i++)
{
vector<ll> temp,temp2;
ll a=i;
while(i<cad.size()&&cad[a].x==cad[i].x){
temp.push_back(cad[i].y);
i++;
}
i--;
for(auto j:temp)
{
if(s.find(j)!=s.end())
temp2.push_back(j);
else s.insert(j);
}
for(auto j:temp2) for(auto k:s) G[j].push_back(k);
for(auto j:temp2) if(s.find(j)!=s.end()) s.erase(j);
}
for(int i=1; i<=n; i++)
cmc(n,i);
for(int i=0; i<q; i++)
{
ll a,b;
cin>>a>>b;
a=v[a][b];
if(a==1e9) cout<<"impossible"<<"\n";
else cout<<a<<"\n";
}
return 0;
}
Compilation message
events.cpp:6: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
6 | #pragma optimize("O3")
|
events.cpp: In function 'int main()':
events.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0; i<cad.size(); i++)
| ~^~~~~~~~~~~
events.cpp:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | while(i<cad.size()&&cad[a].x==cad[i].x){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Runtime error |
27 ms |
6088 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
42 ms |
4600 KB |
Output is correct |
4 |
Correct |
22 ms |
4700 KB |
Output is correct |
5 |
Correct |
45 ms |
4688 KB |
Output is correct |
6 |
Correct |
33 ms |
5468 KB |
Output is correct |
7 |
Correct |
166 ms |
6044 KB |
Output is correct |
8 |
Correct |
190 ms |
7304 KB |
Output is correct |
9 |
Correct |
15 ms |
8536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
42 ms |
4600 KB |
Output is correct |
4 |
Correct |
22 ms |
4700 KB |
Output is correct |
5 |
Correct |
45 ms |
4688 KB |
Output is correct |
6 |
Correct |
33 ms |
5468 KB |
Output is correct |
7 |
Correct |
166 ms |
6044 KB |
Output is correct |
8 |
Correct |
190 ms |
7304 KB |
Output is correct |
9 |
Correct |
15 ms |
8536 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
44 ms |
4692 KB |
Output is correct |
13 |
Correct |
22 ms |
4696 KB |
Output is correct |
14 |
Correct |
45 ms |
4484 KB |
Output is correct |
15 |
Correct |
32 ms |
5204 KB |
Output is correct |
16 |
Correct |
160 ms |
6224 KB |
Output is correct |
17 |
Correct |
176 ms |
7252 KB |
Output is correct |
18 |
Correct |
12 ms |
8536 KB |
Output is correct |
19 |
Execution timed out |
1602 ms |
43448 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
42 ms |
4600 KB |
Output is correct |
4 |
Correct |
22 ms |
4700 KB |
Output is correct |
5 |
Correct |
45 ms |
4688 KB |
Output is correct |
6 |
Correct |
33 ms |
5468 KB |
Output is correct |
7 |
Correct |
166 ms |
6044 KB |
Output is correct |
8 |
Correct |
190 ms |
7304 KB |
Output is correct |
9 |
Correct |
15 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
46 ms |
4560 KB |
Output is correct |
13 |
Correct |
22 ms |
4696 KB |
Output is correct |
14 |
Correct |
45 ms |
4688 KB |
Output is correct |
15 |
Correct |
33 ms |
5468 KB |
Output is correct |
16 |
Correct |
159 ms |
6200 KB |
Output is correct |
17 |
Correct |
171 ms |
7104 KB |
Output is correct |
18 |
Correct |
16 ms |
8540 KB |
Output is correct |
19 |
Runtime error |
26 ms |
6084 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
6084 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Runtime error |
27 ms |
6088 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |