#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (int x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e))?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node{
int s,e,m;
ii val=ii(1e9,-1);
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void update(int i,ii k){
val=min(val,k);
if (s==e) return;
if (i<=m) l->update(i,k);
else r->update(i,k);
}
ii query(int i,int j){
if (s==i && e==j) return val;
else if (j<=m) return l->query(i,j);
else if (m<i) return r->query(i,j);
else return min(l->query(i,m),r->query(m+1,j));
}
} *root=new node(0,200005);
int n,q;
ii range[100005];
int tkd[100005][18];
signed main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n>>q;
rep(x,1,n+1) cin>>range[x].fi>>range[x].se;
vector<int> uni;
rep(x,1,n+1) uni.pub(range[x].fi),uni.pub(range[x].se);
sort(all(uni));
rep(x,1,n+1){
range[x].fi=lb(all(uni),range[x].fi)-uni.begin();
range[x].se=lb(all(uni),range[x].se)-uni.begin();
}
rep(x,1,n+1) root->update(range[x].se,ii(range[x].fi,x));
vector<int> idx;
rep(x,1,n+1) idx.pub(x);
sort(all(idx),[](int i,int j){
return range[i].fi<range[j].fi;
});
memset(tkd,-1,sizeof(tkd));
for (auto it:idx){
tkd[it][0]=root->query(range[it].fi,range[it].se).se;
if (range[tkd[it][0]].fi==range[it].fi) tkd[it][0]=-1;
int curr=tkd[it][0];
//cout<<it<<" "<<tkd[it][0]<<endl;
rep(x,0,18){
if (curr==-1) break;
curr=tkd[it][x+1]=tkd[curr][x];
}
}
int a,b;
while (q--){
cin>>a>>b;
if (a==b) cout<<0<<endl;
else if (range[b].se<range[a].se) cout<<"impossible"<<endl;
else if (range[b].fi<=range[a].se && range[a].se<=range[b].se) cout<<1<<endl;
else{
int ans=0;
rep(x,18,0) if (tkd[b][x]!=-1 && range[a].se<range[tkd[b][x]].fi){
b=tkd[b][x];
ans+=(1<<x);
}
if (tkd[b][0]!=-1) cout<<ans+2<<endl;
else cout<<"impossible"<<endl;
}
}
}
Compilation message
events.cpp: In constructor 'node::node(long long int, long long int)':
events.cpp:29:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | s=_s,e=_e,m=s+e>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
39380 KB |
Output is correct |
2 |
Correct |
264 ms |
48108 KB |
Output is correct |
3 |
Correct |
250 ms |
47972 KB |
Output is correct |
4 |
Correct |
325 ms |
47880 KB |
Output is correct |
5 |
Correct |
244 ms |
48108 KB |
Output is correct |
6 |
Correct |
252 ms |
48056 KB |
Output is correct |
7 |
Correct |
251 ms |
48184 KB |
Output is correct |
8 |
Correct |
184 ms |
48568 KB |
Output is correct |
9 |
Correct |
219 ms |
48384 KB |
Output is correct |
10 |
Correct |
243 ms |
48344 KB |
Output is correct |
11 |
Correct |
264 ms |
48424 KB |
Output is correct |
12 |
Correct |
102 ms |
47688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
39380 KB |
Output is correct |
2 |
Correct |
30 ms |
39444 KB |
Output is correct |
3 |
Correct |
28 ms |
39512 KB |
Output is correct |
4 |
Correct |
28 ms |
39508 KB |
Output is correct |
5 |
Correct |
27 ms |
39508 KB |
Output is correct |
6 |
Correct |
25 ms |
39508 KB |
Output is correct |
7 |
Correct |
29 ms |
39432 KB |
Output is correct |
8 |
Correct |
28 ms |
39500 KB |
Output is correct |
9 |
Correct |
26 ms |
39472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
39380 KB |
Output is correct |
2 |
Correct |
30 ms |
39444 KB |
Output is correct |
3 |
Correct |
28 ms |
39512 KB |
Output is correct |
4 |
Correct |
28 ms |
39508 KB |
Output is correct |
5 |
Correct |
27 ms |
39508 KB |
Output is correct |
6 |
Correct |
25 ms |
39508 KB |
Output is correct |
7 |
Correct |
29 ms |
39432 KB |
Output is correct |
8 |
Correct |
28 ms |
39500 KB |
Output is correct |
9 |
Correct |
26 ms |
39472 KB |
Output is correct |
10 |
Correct |
33 ms |
39380 KB |
Output is correct |
11 |
Correct |
31 ms |
39380 KB |
Output is correct |
12 |
Correct |
26 ms |
39492 KB |
Output is correct |
13 |
Correct |
27 ms |
39468 KB |
Output is correct |
14 |
Correct |
28 ms |
39508 KB |
Output is correct |
15 |
Correct |
26 ms |
39468 KB |
Output is correct |
16 |
Correct |
32 ms |
39500 KB |
Output is correct |
17 |
Correct |
25 ms |
39412 KB |
Output is correct |
18 |
Correct |
25 ms |
39476 KB |
Output is correct |
19 |
Correct |
54 ms |
41100 KB |
Output is correct |
20 |
Correct |
58 ms |
41172 KB |
Output is correct |
21 |
Correct |
64 ms |
41400 KB |
Output is correct |
22 |
Correct |
60 ms |
41460 KB |
Output is correct |
23 |
Correct |
63 ms |
41324 KB |
Output is correct |
24 |
Correct |
67 ms |
41376 KB |
Output is correct |
25 |
Correct |
51 ms |
41060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
39380 KB |
Output is correct |
2 |
Correct |
30 ms |
39444 KB |
Output is correct |
3 |
Correct |
28 ms |
39512 KB |
Output is correct |
4 |
Correct |
28 ms |
39508 KB |
Output is correct |
5 |
Correct |
27 ms |
39508 KB |
Output is correct |
6 |
Correct |
25 ms |
39508 KB |
Output is correct |
7 |
Correct |
29 ms |
39432 KB |
Output is correct |
8 |
Correct |
28 ms |
39500 KB |
Output is correct |
9 |
Correct |
26 ms |
39472 KB |
Output is correct |
10 |
Correct |
26 ms |
39356 KB |
Output is correct |
11 |
Correct |
32 ms |
39420 KB |
Output is correct |
12 |
Correct |
26 ms |
39472 KB |
Output is correct |
13 |
Correct |
25 ms |
39500 KB |
Output is correct |
14 |
Correct |
25 ms |
39476 KB |
Output is correct |
15 |
Correct |
26 ms |
39500 KB |
Output is correct |
16 |
Correct |
24 ms |
39508 KB |
Output is correct |
17 |
Correct |
27 ms |
39468 KB |
Output is correct |
18 |
Correct |
30 ms |
39508 KB |
Output is correct |
19 |
Correct |
213 ms |
46312 KB |
Output is correct |
20 |
Correct |
204 ms |
45668 KB |
Output is correct |
21 |
Correct |
199 ms |
46076 KB |
Output is correct |
22 |
Correct |
205 ms |
46176 KB |
Output is correct |
23 |
Correct |
173 ms |
46252 KB |
Output is correct |
24 |
Correct |
258 ms |
46324 KB |
Output is correct |
25 |
Correct |
73 ms |
45580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
247 ms |
48052 KB |
Output is correct |
2 |
Correct |
262 ms |
48056 KB |
Output is correct |
3 |
Correct |
280 ms |
47852 KB |
Output is correct |
4 |
Correct |
189 ms |
48436 KB |
Output is correct |
5 |
Correct |
300 ms |
48252 KB |
Output is correct |
6 |
Correct |
295 ms |
48172 KB |
Output is correct |
7 |
Correct |
306 ms |
48060 KB |
Output is correct |
8 |
Correct |
242 ms |
48360 KB |
Output is correct |
9 |
Correct |
172 ms |
46164 KB |
Output is correct |
10 |
Correct |
260 ms |
47744 KB |
Output is correct |
11 |
Correct |
229 ms |
47532 KB |
Output is correct |
12 |
Correct |
228 ms |
47764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
39380 KB |
Output is correct |
2 |
Correct |
264 ms |
48108 KB |
Output is correct |
3 |
Correct |
250 ms |
47972 KB |
Output is correct |
4 |
Correct |
325 ms |
47880 KB |
Output is correct |
5 |
Correct |
244 ms |
48108 KB |
Output is correct |
6 |
Correct |
252 ms |
48056 KB |
Output is correct |
7 |
Correct |
251 ms |
48184 KB |
Output is correct |
8 |
Correct |
184 ms |
48568 KB |
Output is correct |
9 |
Correct |
219 ms |
48384 KB |
Output is correct |
10 |
Correct |
243 ms |
48344 KB |
Output is correct |
11 |
Correct |
264 ms |
48424 KB |
Output is correct |
12 |
Correct |
102 ms |
47688 KB |
Output is correct |
13 |
Correct |
25 ms |
39380 KB |
Output is correct |
14 |
Correct |
30 ms |
39444 KB |
Output is correct |
15 |
Correct |
28 ms |
39512 KB |
Output is correct |
16 |
Correct |
28 ms |
39508 KB |
Output is correct |
17 |
Correct |
27 ms |
39508 KB |
Output is correct |
18 |
Correct |
25 ms |
39508 KB |
Output is correct |
19 |
Correct |
29 ms |
39432 KB |
Output is correct |
20 |
Correct |
28 ms |
39500 KB |
Output is correct |
21 |
Correct |
26 ms |
39472 KB |
Output is correct |
22 |
Correct |
33 ms |
39380 KB |
Output is correct |
23 |
Correct |
31 ms |
39380 KB |
Output is correct |
24 |
Correct |
26 ms |
39492 KB |
Output is correct |
25 |
Correct |
27 ms |
39468 KB |
Output is correct |
26 |
Correct |
28 ms |
39508 KB |
Output is correct |
27 |
Correct |
26 ms |
39468 KB |
Output is correct |
28 |
Correct |
32 ms |
39500 KB |
Output is correct |
29 |
Correct |
25 ms |
39412 KB |
Output is correct |
30 |
Correct |
25 ms |
39476 KB |
Output is correct |
31 |
Correct |
54 ms |
41100 KB |
Output is correct |
32 |
Correct |
58 ms |
41172 KB |
Output is correct |
33 |
Correct |
64 ms |
41400 KB |
Output is correct |
34 |
Correct |
60 ms |
41460 KB |
Output is correct |
35 |
Correct |
63 ms |
41324 KB |
Output is correct |
36 |
Correct |
67 ms |
41376 KB |
Output is correct |
37 |
Correct |
51 ms |
41060 KB |
Output is correct |
38 |
Correct |
26 ms |
39356 KB |
Output is correct |
39 |
Correct |
32 ms |
39420 KB |
Output is correct |
40 |
Correct |
26 ms |
39472 KB |
Output is correct |
41 |
Correct |
25 ms |
39500 KB |
Output is correct |
42 |
Correct |
25 ms |
39476 KB |
Output is correct |
43 |
Correct |
26 ms |
39500 KB |
Output is correct |
44 |
Correct |
24 ms |
39508 KB |
Output is correct |
45 |
Correct |
27 ms |
39468 KB |
Output is correct |
46 |
Correct |
30 ms |
39508 KB |
Output is correct |
47 |
Correct |
213 ms |
46312 KB |
Output is correct |
48 |
Correct |
204 ms |
45668 KB |
Output is correct |
49 |
Correct |
199 ms |
46076 KB |
Output is correct |
50 |
Correct |
205 ms |
46176 KB |
Output is correct |
51 |
Correct |
173 ms |
46252 KB |
Output is correct |
52 |
Correct |
258 ms |
46324 KB |
Output is correct |
53 |
Correct |
73 ms |
45580 KB |
Output is correct |
54 |
Correct |
247 ms |
48052 KB |
Output is correct |
55 |
Correct |
262 ms |
48056 KB |
Output is correct |
56 |
Correct |
280 ms |
47852 KB |
Output is correct |
57 |
Correct |
189 ms |
48436 KB |
Output is correct |
58 |
Correct |
300 ms |
48252 KB |
Output is correct |
59 |
Correct |
295 ms |
48172 KB |
Output is correct |
60 |
Correct |
306 ms |
48060 KB |
Output is correct |
61 |
Correct |
242 ms |
48360 KB |
Output is correct |
62 |
Correct |
172 ms |
46164 KB |
Output is correct |
63 |
Correct |
260 ms |
47744 KB |
Output is correct |
64 |
Correct |
229 ms |
47532 KB |
Output is correct |
65 |
Correct |
228 ms |
47764 KB |
Output is correct |
66 |
Correct |
27 ms |
39412 KB |
Output is correct |
67 |
Correct |
224 ms |
48024 KB |
Output is correct |
68 |
Correct |
236 ms |
47944 KB |
Output is correct |
69 |
Correct |
297 ms |
47976 KB |
Output is correct |
70 |
Correct |
274 ms |
48064 KB |
Output is correct |
71 |
Correct |
231 ms |
48108 KB |
Output is correct |
72 |
Correct |
260 ms |
48176 KB |
Output is correct |
73 |
Correct |
185 ms |
48508 KB |
Output is correct |
74 |
Correct |
198 ms |
48580 KB |
Output is correct |
75 |
Correct |
257 ms |
48444 KB |
Output is correct |
76 |
Correct |
248 ms |
48412 KB |
Output is correct |
77 |
Correct |
105 ms |
47708 KB |
Output is correct |
78 |
Correct |
29 ms |
39500 KB |
Output is correct |
79 |
Correct |
26 ms |
39500 KB |
Output is correct |
80 |
Correct |
26 ms |
39448 KB |
Output is correct |
81 |
Correct |
31 ms |
39508 KB |
Output is correct |
82 |
Correct |
24 ms |
39520 KB |
Output is correct |
83 |
Correct |
24 ms |
39484 KB |
Output is correct |
84 |
Correct |
23 ms |
39424 KB |
Output is correct |
85 |
Correct |
25 ms |
39540 KB |
Output is correct |
86 |
Correct |
70 ms |
41156 KB |
Output is correct |
87 |
Correct |
53 ms |
41232 KB |
Output is correct |
88 |
Correct |
63 ms |
41464 KB |
Output is correct |
89 |
Correct |
58 ms |
41492 KB |
Output is correct |
90 |
Correct |
53 ms |
41340 KB |
Output is correct |
91 |
Correct |
56 ms |
41420 KB |
Output is correct |
92 |
Correct |
75 ms |
40976 KB |
Output is correct |
93 |
Correct |
222 ms |
46324 KB |
Output is correct |
94 |
Correct |
206 ms |
45516 KB |
Output is correct |
95 |
Correct |
206 ms |
46236 KB |
Output is correct |
96 |
Correct |
183 ms |
46340 KB |
Output is correct |
97 |
Correct |
171 ms |
46216 KB |
Output is correct |
98 |
Correct |
234 ms |
46312 KB |
Output is correct |
99 |
Correct |
73 ms |
45624 KB |
Output is correct |
100 |
Correct |
292 ms |
48112 KB |
Output is correct |
101 |
Correct |
314 ms |
48084 KB |
Output is correct |
102 |
Correct |
215 ms |
48184 KB |
Output is correct |
103 |
Correct |
274 ms |
47812 KB |
Output is correct |
104 |
Correct |
205 ms |
47424 KB |
Output is correct |
105 |
Correct |
246 ms |
47788 KB |
Output is correct |
106 |
Correct |
234 ms |
47472 KB |
Output is correct |
107 |
Correct |
321 ms |
47460 KB |
Output is correct |
108 |
Correct |
210 ms |
48104 KB |
Output is correct |
109 |
Correct |
221 ms |
48056 KB |
Output is correct |
110 |
Correct |
274 ms |
48056 KB |
Output is correct |
111 |
Correct |
265 ms |
48100 KB |
Output is correct |