// #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;
} else if(events[b].first<=events[a].second) {
cout<<1<<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;
}
assert(events[b].first>events[a].second);
ll nxt=jmp[0][b];
if(nxt==n) {
cout<<"impossible"<<endl;
continue;
}
assert(events[nxt].first<=events[a].second);
// cout<<"a = "<<events[a].first<<' '<<events[a].second<<endl;
// cout<<"b = "<<events[nxt].first<<' '<<events[nxt].second<<endl;
ans+=2;
cout<<ans<<endl;
}
_Exit(0);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
289 ms |
54712 KB |
Output is correct |
3 |
Correct |
304 ms |
54684 KB |
Output is correct |
4 |
Correct |
352 ms |
54652 KB |
Output is correct |
5 |
Correct |
315 ms |
55580 KB |
Output is correct |
6 |
Correct |
331 ms |
56512 KB |
Output is correct |
7 |
Correct |
308 ms |
56504 KB |
Output is correct |
8 |
Correct |
353 ms |
64360 KB |
Output is correct |
9 |
Correct |
332 ms |
67500 KB |
Output is correct |
10 |
Correct |
338 ms |
58152 KB |
Output is correct |
11 |
Correct |
401 ms |
61380 KB |
Output is correct |
12 |
Correct |
245 ms |
57548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
1 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 |
3 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
3 ms |
852 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 |
852 KB |
Output is correct |
19 |
Correct |
152 ms |
3740 KB |
Output is correct |
20 |
Correct |
148 ms |
3556 KB |
Output is correct |
21 |
Correct |
160 ms |
3796 KB |
Output is correct |
22 |
Correct |
180 ms |
3928 KB |
Output is correct |
23 |
Correct |
138 ms |
4108 KB |
Output is correct |
24 |
Correct |
141 ms |
5204 KB |
Output is correct |
25 |
Correct |
164 ms |
4744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
1 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 |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
3 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
163 ms |
54172 KB |
Output is correct |
20 |
Correct |
123 ms |
54036 KB |
Output is correct |
21 |
Correct |
155 ms |
54016 KB |
Output is correct |
22 |
Correct |
146 ms |
56916 KB |
Output is correct |
23 |
Correct |
257 ms |
63276 KB |
Output is correct |
24 |
Correct |
227 ms |
63432 KB |
Output is correct |
25 |
Correct |
74 ms |
44884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
54728 KB |
Output is correct |
2 |
Correct |
297 ms |
54752 KB |
Output is correct |
3 |
Correct |
354 ms |
54676 KB |
Output is correct |
4 |
Correct |
353 ms |
64600 KB |
Output is correct |
5 |
Correct |
327 ms |
55080 KB |
Output is correct |
6 |
Correct |
426 ms |
64096 KB |
Output is correct |
7 |
Correct |
386 ms |
64004 KB |
Output is correct |
8 |
Correct |
398 ms |
64240 KB |
Output is correct |
9 |
Correct |
264 ms |
63336 KB |
Output is correct |
10 |
Correct |
408 ms |
63620 KB |
Output is correct |
11 |
Correct |
401 ms |
63516 KB |
Output is correct |
12 |
Correct |
405 ms |
63712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
289 ms |
54712 KB |
Output is correct |
3 |
Correct |
304 ms |
54684 KB |
Output is correct |
4 |
Correct |
352 ms |
54652 KB |
Output is correct |
5 |
Correct |
315 ms |
55580 KB |
Output is correct |
6 |
Correct |
331 ms |
56512 KB |
Output is correct |
7 |
Correct |
308 ms |
56504 KB |
Output is correct |
8 |
Correct |
353 ms |
64360 KB |
Output is correct |
9 |
Correct |
332 ms |
67500 KB |
Output is correct |
10 |
Correct |
338 ms |
58152 KB |
Output is correct |
11 |
Correct |
401 ms |
61380 KB |
Output is correct |
12 |
Correct |
245 ms |
57548 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
980 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
152 ms |
3740 KB |
Output is correct |
32 |
Correct |
148 ms |
3556 KB |
Output is correct |
33 |
Correct |
160 ms |
3796 KB |
Output is correct |
34 |
Correct |
180 ms |
3928 KB |
Output is correct |
35 |
Correct |
138 ms |
4108 KB |
Output is correct |
36 |
Correct |
141 ms |
5204 KB |
Output is correct |
37 |
Correct |
164 ms |
4744 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
852 KB |
Output is correct |
42 |
Correct |
2 ms |
980 KB |
Output is correct |
43 |
Correct |
3 ms |
852 KB |
Output is correct |
44 |
Correct |
2 ms |
1108 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
1 ms |
852 KB |
Output is correct |
47 |
Correct |
163 ms |
54172 KB |
Output is correct |
48 |
Correct |
123 ms |
54036 KB |
Output is correct |
49 |
Correct |
155 ms |
54016 KB |
Output is correct |
50 |
Correct |
146 ms |
56916 KB |
Output is correct |
51 |
Correct |
257 ms |
63276 KB |
Output is correct |
52 |
Correct |
227 ms |
63432 KB |
Output is correct |
53 |
Correct |
74 ms |
44884 KB |
Output is correct |
54 |
Correct |
294 ms |
54728 KB |
Output is correct |
55 |
Correct |
297 ms |
54752 KB |
Output is correct |
56 |
Correct |
354 ms |
54676 KB |
Output is correct |
57 |
Correct |
353 ms |
64600 KB |
Output is correct |
58 |
Correct |
327 ms |
55080 KB |
Output is correct |
59 |
Correct |
426 ms |
64096 KB |
Output is correct |
60 |
Correct |
386 ms |
64004 KB |
Output is correct |
61 |
Correct |
398 ms |
64240 KB |
Output is correct |
62 |
Correct |
264 ms |
63336 KB |
Output is correct |
63 |
Correct |
408 ms |
63620 KB |
Output is correct |
64 |
Correct |
401 ms |
63516 KB |
Output is correct |
65 |
Correct |
405 ms |
63712 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
277 ms |
57768 KB |
Output is correct |
68 |
Correct |
337 ms |
57820 KB |
Output is correct |
69 |
Correct |
359 ms |
57856 KB |
Output is correct |
70 |
Correct |
339 ms |
58720 KB |
Output is correct |
71 |
Correct |
318 ms |
59356 KB |
Output is correct |
72 |
Correct |
324 ms |
59624 KB |
Output is correct |
73 |
Correct |
330 ms |
67516 KB |
Output is correct |
74 |
Correct |
351 ms |
67432 KB |
Output is correct |
75 |
Correct |
359 ms |
58184 KB |
Output is correct |
76 |
Correct |
337 ms |
61328 KB |
Output is correct |
77 |
Correct |
277 ms |
57528 KB |
Output is correct |
78 |
Correct |
1 ms |
328 KB |
Output is correct |
79 |
Correct |
2 ms |
988 KB |
Output is correct |
80 |
Correct |
2 ms |
980 KB |
Output is correct |
81 |
Correct |
2 ms |
980 KB |
Output is correct |
82 |
Correct |
3 ms |
980 KB |
Output is correct |
83 |
Correct |
3 ms |
980 KB |
Output is correct |
84 |
Correct |
2 ms |
984 KB |
Output is correct |
85 |
Correct |
2 ms |
852 KB |
Output is correct |
86 |
Correct |
154 ms |
4724 KB |
Output is correct |
87 |
Correct |
154 ms |
4608 KB |
Output is correct |
88 |
Correct |
154 ms |
4836 KB |
Output is correct |
89 |
Correct |
144 ms |
4884 KB |
Output is correct |
90 |
Correct |
143 ms |
5112 KB |
Output is correct |
91 |
Correct |
161 ms |
5212 KB |
Output is correct |
92 |
Correct |
143 ms |
4756 KB |
Output is correct |
93 |
Correct |
133 ms |
56064 KB |
Output is correct |
94 |
Correct |
144 ms |
55264 KB |
Output is correct |
95 |
Correct |
180 ms |
55984 KB |
Output is correct |
96 |
Correct |
173 ms |
58948 KB |
Output is correct |
97 |
Correct |
229 ms |
65252 KB |
Output is correct |
98 |
Correct |
234 ms |
65272 KB |
Output is correct |
99 |
Correct |
69 ms |
46108 KB |
Output is correct |
100 |
Correct |
436 ms |
67228 KB |
Output is correct |
101 |
Correct |
414 ms |
67364 KB |
Output is correct |
102 |
Correct |
426 ms |
67288 KB |
Output is correct |
103 |
Correct |
368 ms |
66808 KB |
Output is correct |
104 |
Correct |
403 ms |
66600 KB |
Output is correct |
105 |
Correct |
443 ms |
66896 KB |
Output is correct |
106 |
Correct |
326 ms |
54508 KB |
Output is correct |
107 |
Correct |
337 ms |
57168 KB |
Output is correct |
108 |
Correct |
343 ms |
58036 KB |
Output is correct |
109 |
Correct |
321 ms |
57984 KB |
Output is correct |
110 |
Correct |
429 ms |
67176 KB |
Output is correct |
111 |
Correct |
465 ms |
67088 KB |
Output is correct |