#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=1e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
struct node
{
ll val,id;
node()
{
val=inf,id=inf;
}
node operator+(const node&o)
{
node ans;
if(val<o.val) ans.val=val,ans.id=id;
else ans=o;
return ans;
}
}st[4*maxN];
ll n,q;
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
st[id].val=val;
st[id].id=l;
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
node get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return node();
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return get(i,j,id*2,l,mid)+get(i,j,id*2+1,mid+1,r);
}
struct qq
{
ll s,e,id;
bool operator<(const qq&o)
{
if(e==o.e) return s<o.s;
return e<o.e;
}
}a[maxN];
ll pos[maxN];
ll pre[maxN][19];
void solve()
{
cin >> n >> q;
for(int i=1;i<=n;i++)
{
cin >> a[i].s >> a[i].e;
a[i].id=i;
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
pos[a[i].id]=i;
ll low=1,high=i-1;
while(low<=high)
{
ll mid=low+high>>1;
if(a[mid].e<a[i].s) low=mid+1;
else high=mid-1;
}
auto val=get(low,i-1);
if(val.val==inf||val.val>=a[i].s) pre[i][0]=i;
else pre[i][0]=val.id;
update(i,a[i].s);
}
for(int j=1;j<=18;j++)
{
for(int i=1;i<=n;i++)
{
pre[i][j]=pre[pre[i][j-1]][j-1];
}
}
for(int i=1;i<=q;i++)
{
ll x,y;
cin >> x >> y;
x=pos[x];
y=pos[y];
if(x==y)
{
cout << 0<<'\n';
}
else if(a[x].e==a[y].e)
{
cout << 1<<'\n';
}
else if(x>y)
{
cout << "impossible\n";
}
else
{
ll ans=0;
for(int j=18;j>=0;j--)
{
if(a[pre[y][j]].s>a[x].e)
{
y=pre[y][j],ans+=1<<j;
}
}
if(a[pre[y][0]].s<=a[x].e)
{
ans+=2;
cout << ans << '\n';
}
else
{
cout << "impossible\n";
}
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
events.cpp: In function 'void update(ll, ll, ll, ll, ll)':
events.cpp:37:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | ll mid=l+r>>1;
| ~^~
events.cpp: In function 'node get(ll, ll, ll, ll, ll)':
events.cpp:46:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | ll mid=l+r>>1;
| ~^~
events.cpp: In function 'void solve()':
events.cpp:75:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | ll mid=low+high>>1;
| ~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
110 ms |
25156 KB |
Output is correct |
3 |
Correct |
108 ms |
25088 KB |
Output is correct |
4 |
Incorrect |
112 ms |
25160 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6740 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
25084 KB |
Output is correct |
2 |
Correct |
115 ms |
25132 KB |
Output is correct |
3 |
Incorrect |
114 ms |
25124 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
110 ms |
25156 KB |
Output is correct |
3 |
Correct |
108 ms |
25088 KB |
Output is correct |
4 |
Incorrect |
112 ms |
25160 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |