// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = (b) - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)x.size()
#define pb push_back
#define mp make_pair
#define debug(x) cout<<#x<<" = "<<x<<endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll INF = 1'000'000'007;
struct Node {
ll lo,hi,val=INF,id=INF;
Node *l,*r;
Node(ll L,ll R) : lo(L),hi(R) {
if(hi-lo!=1) {
ll mid=(lo+hi)/2;
l=new Node(lo,mid);
r=new Node(mid,hi);
}
}
pll query(ll L,ll R) {
if(R<=lo||hi<=L) return {INF,INF};
if(L<=lo&&hi<=R) return {val,id};
return min(l->query(L,R),r->query(L,R));
}
void update(ll pos,ll x,ll nid) {
if(pos<lo||hi<=pos) return;
if(hi-lo==1) {
if(x<val) {
val=x;
id=nid;
}
return;
}
l->update(pos,x,nid);
r->update(pos,x,nid);
tie(val,id)=min(pll(l->val,l->id),pll(r->val,r->id));
}
};
ll n,q,jmp[20][100005];
vector<pair<pll,ll>> sorted_events;
vpll events;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin>>n>>q;
rep(i,0,n+1) jmp[0][i]=n;
set<ll> coords;
umap<ll,ll>compress;
rep(i,0,n) {
ll s,e;
cin>>s>>e;
events.emplace_back(s,e);
coords.insert(s);
coords.insert(e);
}
{
ll cnt=0;
trav(val,coords) compress[val]=cnt++;
rep(i,0,n) {
events[i].first=compress[events[i].first];
events[i].second=compress[events[i].second];
sorted_events.emplace_back(events[i],i);
}
}
sort(all(sorted_events));
Node segtree(0,2*n+1);
trav(event,sorted_events) {
ll s,e,id=event.second;
tie(s,e)=event.first;
pll best=segtree.query(s,e+1);
if(best.first!=INF) jmp[0][id]=best.second;
segtree.update(e,s,id);
}
rep(j,1,20) rep(i,0,n+1) jmp[j][i]=jmp[j-1][jmp[j-1][i]];
while(q--) {
ll a,b;
cin>>a>>b;
--a; --b;
if(events[a].second>events[b].second) {
cout<<"impossible"<<endl;
continue;
} else if(a==b) {
cout<<0<<endl;
continue;
}
ll ans=0;
per(j,0,20) {
ll nxt=jmp[j][b];
if(nxt==n||events[nxt].first<=events[a].second) continue;
b=nxt;
ans+=1<<j;
}
ll nxt=jmp[0][b];
if(nxt==n) {
cout<<"impossible"<<endl;
continue;
}
// cout<<"a = "<<events[a].first<<' '<<events[a].second<<endl;
// cout<<"b = "<<events[nxt].first<<' '<<events[nxt].second<<endl;
++ans;
if(events[a].second<events[b].first) ++ans;
cout<<ans<<endl;
}
_Exit(0);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
293 ms |
57744 KB |
Output is correct |
3 |
Correct |
293 ms |
57816 KB |
Output is correct |
4 |
Correct |
393 ms |
57820 KB |
Output is correct |
5 |
Correct |
310 ms |
58632 KB |
Output is correct |
6 |
Correct |
339 ms |
59312 KB |
Output is correct |
7 |
Correct |
314 ms |
59572 KB |
Output is correct |
8 |
Incorrect |
335 ms |
67492 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
2 ms |
976 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
984 KB |
Output is correct |
9 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
2 ms |
976 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
984 KB |
Output is correct |
9 |
Correct |
2 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
916 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
2 ms |
916 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
2 ms |
856 KB |
Output is correct |
19 |
Correct |
155 ms |
4656 KB |
Output is correct |
20 |
Correct |
145 ms |
4588 KB |
Output is correct |
21 |
Correct |
147 ms |
4756 KB |
Output is correct |
22 |
Correct |
151 ms |
4992 KB |
Output is correct |
23 |
Incorrect |
138 ms |
5156 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
2 ms |
976 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
984 KB |
Output is correct |
9 |
Correct |
2 ms |
852 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
912 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
2 ms |
984 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
131 ms |
56032 KB |
Output is correct |
20 |
Correct |
122 ms |
55200 KB |
Output is correct |
21 |
Correct |
156 ms |
55980 KB |
Output is correct |
22 |
Correct |
178 ms |
58900 KB |
Output is correct |
23 |
Correct |
228 ms |
65288 KB |
Output is correct |
24 |
Correct |
208 ms |
65252 KB |
Output is correct |
25 |
Correct |
84 ms |
46188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
262 ms |
57804 KB |
Output is correct |
2 |
Correct |
298 ms |
57772 KB |
Output is correct |
3 |
Correct |
359 ms |
57952 KB |
Output is correct |
4 |
Correct |
354 ms |
67496 KB |
Output is correct |
5 |
Correct |
349 ms |
58328 KB |
Output is correct |
6 |
Correct |
409 ms |
67096 KB |
Output is correct |
7 |
Correct |
469 ms |
67060 KB |
Output is correct |
8 |
Correct |
420 ms |
67300 KB |
Output is correct |
9 |
Correct |
214 ms |
65272 KB |
Output is correct |
10 |
Correct |
372 ms |
66820 KB |
Output is correct |
11 |
Correct |
374 ms |
66528 KB |
Output is correct |
12 |
Correct |
379 ms |
66740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
293 ms |
57744 KB |
Output is correct |
3 |
Correct |
293 ms |
57816 KB |
Output is correct |
4 |
Correct |
393 ms |
57820 KB |
Output is correct |
5 |
Correct |
310 ms |
58632 KB |
Output is correct |
6 |
Correct |
339 ms |
59312 KB |
Output is correct |
7 |
Correct |
314 ms |
59572 KB |
Output is correct |
8 |
Incorrect |
335 ms |
67492 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |