This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct event
{
int s;
int f;
int num;
};
bool cmp (event a, event b)
{
if (a.f==b.f) return a.s<=b.s;
return a.f<b.f;
}
int main() {
int n,q;
cin>>n>>q;
event arr[n+1];
int p[n+1];
for (int i=1; i<=n; i++)
cin>>arr[i].s>>arr[i].f;
for (int i=1; i<=n; i++)
arr[i].num=i;
sort (arr+1,arr+n+1,cmp);
for (int i=1; i<=n; i++)
p[arr[i].num]=i;
int ori=1,start[n+1];
start[1]=1;
for (int i=1; i<n; i++)
{
if (arr[i+1].s<=arr[i].f&&arr[i].f<=arr[i+1].f) start[i+1]=ori;
else {ori=i+1; start[i+1]=ori;}
}
while (q--)
{
int a,b;
cin>>a>>b;
a=p[a]; b=p[b];
if (start[a]==start[b]&&a<=b) cout<<abs(a-b);
else cout<<"impossible";
cout<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |