#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
#define per(i,n) for(int i = n-1; i >=0; i--)
#define all(a) a.begin(), a.end()
#define pii pair<int,int>
using namespace std;
vector<pii>T(1e6,{1e15,-1});
pii update(int i, int l, int r, int ui, pii uv)
{
if(r<=ui||l>ui) return T[i];
if(l+1==r) return T[i]=min(T[i],uv);
return T[i] = min(update(2*i,l,(l+r)/2,ui,uv),update(2*i+1,(l+r)/2,r,ui,uv));
}
pii query(int i, int l, int r, int ql, int qr)
{
if(qr<=l||r<=ql) return {1e15,-1};
if(l>=ql&&r<=qr) return T[i];
return min(query(2*i,l,(l+r)/2,ql,qr),query(2*i+1,(l+r)/2,r,ql,qr));
}
struct ev {int s,e;};
bool path(ev a, ev b)
{
return b.s<=a.e&&b.e>=a.e;
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
int n,q;
cin>>n>>q;
vector<ev>evs(n);
for(auto& e: evs) cin>>e.s>>e.e;
vector<int>cords;
for(ev e : evs) {cords.push_back(e.s); cords.push_back(e.e);}
sort(all(cords));
cords.erase(unique(all(cords)), cords.end());
for(ev& e : evs)
{
e.s=lower_bound(all(cords),e.s)-cords.begin();
e.e=lower_bound(all(cords),e.e)-cords.begin();
}
rep(i,n) update(1,0,cords.size(), evs[i].e, {evs[i].s,i});
vector<vector<int>>up(n, vector<int>(20));
rep(i,n)
{
up[i][0]=query(1,0,cords.size(),evs[i].s,evs[i].e+1).second;
if(up[i][0]==-1) up[i][0]=i;
}
for(int j = 1; j < 20; j++) rep(i,n) up[i][j]=up[up[i][j-1]][j-1];
rep(cq,q)
{
int s,e;
cin>>s>>e;
s--; e--;
int sum = 0;
int v = e;
for(int j = 19; j>=0;j--) if(evs[up[v][j]].s>evs[s].e) {v=up[v][j]; sum+=1<<j;}
if(evs[v].s>evs[s].e) {sum++; v=up[v][0];}
if(!path(evs[s], evs[v])) cout<<"impossible\n";
else {
if(s!=v) sum++;
cout<<sum<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
175 ms |
39172 KB |
Output is correct |
3 |
Correct |
196 ms |
39300 KB |
Output is correct |
4 |
Correct |
252 ms |
39272 KB |
Output is correct |
5 |
Correct |
213 ms |
39332 KB |
Output is correct |
6 |
Correct |
206 ms |
39304 KB |
Output is correct |
7 |
Correct |
227 ms |
39340 KB |
Output is correct |
8 |
Correct |
192 ms |
39688 KB |
Output is correct |
9 |
Correct |
181 ms |
39704 KB |
Output is correct |
10 |
Correct |
226 ms |
39544 KB |
Output is correct |
11 |
Correct |
230 ms |
39592 KB |
Output is correct |
12 |
Correct |
124 ms |
39684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15936 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
16212 KB |
Output is correct |
4 |
Correct |
8 ms |
16204 KB |
Output is correct |
5 |
Correct |
8 ms |
16212 KB |
Output is correct |
6 |
Correct |
8 ms |
16112 KB |
Output is correct |
7 |
Correct |
8 ms |
16084 KB |
Output is correct |
8 |
Correct |
9 ms |
16232 KB |
Output is correct |
9 |
Correct |
8 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15936 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
16212 KB |
Output is correct |
4 |
Correct |
8 ms |
16204 KB |
Output is correct |
5 |
Correct |
8 ms |
16212 KB |
Output is correct |
6 |
Correct |
8 ms |
16112 KB |
Output is correct |
7 |
Correct |
8 ms |
16084 KB |
Output is correct |
8 |
Correct |
9 ms |
16232 KB |
Output is correct |
9 |
Correct |
8 ms |
16212 KB |
Output is correct |
10 |
Correct |
7 ms |
15864 KB |
Output is correct |
11 |
Correct |
8 ms |
15956 KB |
Output is correct |
12 |
Correct |
8 ms |
16212 KB |
Output is correct |
13 |
Correct |
9 ms |
16212 KB |
Output is correct |
14 |
Correct |
8 ms |
16212 KB |
Output is correct |
15 |
Correct |
8 ms |
16084 KB |
Output is correct |
16 |
Correct |
8 ms |
16212 KB |
Output is correct |
17 |
Correct |
8 ms |
16212 KB |
Output is correct |
18 |
Correct |
8 ms |
16212 KB |
Output is correct |
19 |
Correct |
42 ms |
17544 KB |
Output is correct |
20 |
Correct |
39 ms |
17520 KB |
Output is correct |
21 |
Correct |
43 ms |
17864 KB |
Output is correct |
22 |
Correct |
38 ms |
17908 KB |
Output is correct |
23 |
Correct |
37 ms |
17736 KB |
Output is correct |
24 |
Correct |
36 ms |
17748 KB |
Output is correct |
25 |
Correct |
37 ms |
17364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15936 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
16212 KB |
Output is correct |
4 |
Correct |
8 ms |
16204 KB |
Output is correct |
5 |
Correct |
8 ms |
16212 KB |
Output is correct |
6 |
Correct |
8 ms |
16112 KB |
Output is correct |
7 |
Correct |
8 ms |
16084 KB |
Output is correct |
8 |
Correct |
9 ms |
16232 KB |
Output is correct |
9 |
Correct |
8 ms |
16212 KB |
Output is correct |
10 |
Correct |
7 ms |
15956 KB |
Output is correct |
11 |
Correct |
8 ms |
15976 KB |
Output is correct |
12 |
Correct |
8 ms |
16212 KB |
Output is correct |
13 |
Correct |
8 ms |
16212 KB |
Output is correct |
14 |
Correct |
8 ms |
16212 KB |
Output is correct |
15 |
Correct |
8 ms |
16212 KB |
Output is correct |
16 |
Correct |
9 ms |
16212 KB |
Output is correct |
17 |
Correct |
8 ms |
16096 KB |
Output is correct |
18 |
Correct |
7 ms |
16212 KB |
Output is correct |
19 |
Correct |
150 ms |
38584 KB |
Output is correct |
20 |
Correct |
155 ms |
38576 KB |
Output is correct |
21 |
Correct |
156 ms |
38608 KB |
Output is correct |
22 |
Correct |
169 ms |
38568 KB |
Output is correct |
23 |
Correct |
174 ms |
38852 KB |
Output is correct |
24 |
Correct |
190 ms |
38652 KB |
Output is correct |
25 |
Correct |
64 ms |
38724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
39208 KB |
Output is correct |
2 |
Correct |
202 ms |
39208 KB |
Output is correct |
3 |
Correct |
255 ms |
39236 KB |
Output is correct |
4 |
Correct |
178 ms |
39724 KB |
Output is correct |
5 |
Correct |
234 ms |
39648 KB |
Output is correct |
6 |
Correct |
251 ms |
39336 KB |
Output is correct |
7 |
Correct |
254 ms |
39364 KB |
Output is correct |
8 |
Correct |
215 ms |
39540 KB |
Output is correct |
9 |
Correct |
170 ms |
38596 KB |
Output is correct |
10 |
Correct |
230 ms |
39200 KB |
Output is correct |
11 |
Correct |
206 ms |
38840 KB |
Output is correct |
12 |
Correct |
235 ms |
39064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
175 ms |
39172 KB |
Output is correct |
3 |
Correct |
196 ms |
39300 KB |
Output is correct |
4 |
Correct |
252 ms |
39272 KB |
Output is correct |
5 |
Correct |
213 ms |
39332 KB |
Output is correct |
6 |
Correct |
206 ms |
39304 KB |
Output is correct |
7 |
Correct |
227 ms |
39340 KB |
Output is correct |
8 |
Correct |
192 ms |
39688 KB |
Output is correct |
9 |
Correct |
181 ms |
39704 KB |
Output is correct |
10 |
Correct |
226 ms |
39544 KB |
Output is correct |
11 |
Correct |
230 ms |
39592 KB |
Output is correct |
12 |
Correct |
124 ms |
39684 KB |
Output is correct |
13 |
Correct |
9 ms |
15936 KB |
Output is correct |
14 |
Correct |
7 ms |
15956 KB |
Output is correct |
15 |
Correct |
8 ms |
16212 KB |
Output is correct |
16 |
Correct |
8 ms |
16204 KB |
Output is correct |
17 |
Correct |
8 ms |
16212 KB |
Output is correct |
18 |
Correct |
8 ms |
16112 KB |
Output is correct |
19 |
Correct |
8 ms |
16084 KB |
Output is correct |
20 |
Correct |
9 ms |
16232 KB |
Output is correct |
21 |
Correct |
8 ms |
16212 KB |
Output is correct |
22 |
Correct |
7 ms |
15864 KB |
Output is correct |
23 |
Correct |
8 ms |
15956 KB |
Output is correct |
24 |
Correct |
8 ms |
16212 KB |
Output is correct |
25 |
Correct |
9 ms |
16212 KB |
Output is correct |
26 |
Correct |
8 ms |
16212 KB |
Output is correct |
27 |
Correct |
8 ms |
16084 KB |
Output is correct |
28 |
Correct |
8 ms |
16212 KB |
Output is correct |
29 |
Correct |
8 ms |
16212 KB |
Output is correct |
30 |
Correct |
8 ms |
16212 KB |
Output is correct |
31 |
Correct |
42 ms |
17544 KB |
Output is correct |
32 |
Correct |
39 ms |
17520 KB |
Output is correct |
33 |
Correct |
43 ms |
17864 KB |
Output is correct |
34 |
Correct |
38 ms |
17908 KB |
Output is correct |
35 |
Correct |
37 ms |
17736 KB |
Output is correct |
36 |
Correct |
36 ms |
17748 KB |
Output is correct |
37 |
Correct |
37 ms |
17364 KB |
Output is correct |
38 |
Correct |
7 ms |
15956 KB |
Output is correct |
39 |
Correct |
8 ms |
15976 KB |
Output is correct |
40 |
Correct |
8 ms |
16212 KB |
Output is correct |
41 |
Correct |
8 ms |
16212 KB |
Output is correct |
42 |
Correct |
8 ms |
16212 KB |
Output is correct |
43 |
Correct |
8 ms |
16212 KB |
Output is correct |
44 |
Correct |
9 ms |
16212 KB |
Output is correct |
45 |
Correct |
8 ms |
16096 KB |
Output is correct |
46 |
Correct |
7 ms |
16212 KB |
Output is correct |
47 |
Correct |
150 ms |
38584 KB |
Output is correct |
48 |
Correct |
155 ms |
38576 KB |
Output is correct |
49 |
Correct |
156 ms |
38608 KB |
Output is correct |
50 |
Correct |
169 ms |
38568 KB |
Output is correct |
51 |
Correct |
174 ms |
38852 KB |
Output is correct |
52 |
Correct |
190 ms |
38652 KB |
Output is correct |
53 |
Correct |
64 ms |
38724 KB |
Output is correct |
54 |
Correct |
192 ms |
39208 KB |
Output is correct |
55 |
Correct |
202 ms |
39208 KB |
Output is correct |
56 |
Correct |
255 ms |
39236 KB |
Output is correct |
57 |
Correct |
178 ms |
39724 KB |
Output is correct |
58 |
Correct |
234 ms |
39648 KB |
Output is correct |
59 |
Correct |
251 ms |
39336 KB |
Output is correct |
60 |
Correct |
254 ms |
39364 KB |
Output is correct |
61 |
Correct |
215 ms |
39540 KB |
Output is correct |
62 |
Correct |
170 ms |
38596 KB |
Output is correct |
63 |
Correct |
230 ms |
39200 KB |
Output is correct |
64 |
Correct |
206 ms |
38840 KB |
Output is correct |
65 |
Correct |
235 ms |
39064 KB |
Output is correct |
66 |
Correct |
7 ms |
15956 KB |
Output is correct |
67 |
Correct |
195 ms |
39188 KB |
Output is correct |
68 |
Correct |
205 ms |
39324 KB |
Output is correct |
69 |
Correct |
264 ms |
39208 KB |
Output is correct |
70 |
Correct |
226 ms |
39432 KB |
Output is correct |
71 |
Correct |
242 ms |
39236 KB |
Output is correct |
72 |
Correct |
223 ms |
39344 KB |
Output is correct |
73 |
Correct |
178 ms |
39748 KB |
Output is correct |
74 |
Correct |
180 ms |
39864 KB |
Output is correct |
75 |
Correct |
229 ms |
39912 KB |
Output is correct |
76 |
Correct |
222 ms |
39808 KB |
Output is correct |
77 |
Correct |
145 ms |
39716 KB |
Output is correct |
78 |
Correct |
7 ms |
15956 KB |
Output is correct |
79 |
Correct |
8 ms |
16144 KB |
Output is correct |
80 |
Correct |
8 ms |
16212 KB |
Output is correct |
81 |
Correct |
9 ms |
16092 KB |
Output is correct |
82 |
Correct |
8 ms |
16140 KB |
Output is correct |
83 |
Correct |
11 ms |
16212 KB |
Output is correct |
84 |
Correct |
9 ms |
16100 KB |
Output is correct |
85 |
Correct |
8 ms |
16212 KB |
Output is correct |
86 |
Correct |
42 ms |
17540 KB |
Output is correct |
87 |
Correct |
43 ms |
17600 KB |
Output is correct |
88 |
Correct |
47 ms |
17876 KB |
Output is correct |
89 |
Correct |
40 ms |
17884 KB |
Output is correct |
90 |
Correct |
46 ms |
17716 KB |
Output is correct |
91 |
Correct |
37 ms |
17740 KB |
Output is correct |
92 |
Correct |
48 ms |
17356 KB |
Output is correct |
93 |
Correct |
155 ms |
38568 KB |
Output is correct |
94 |
Correct |
156 ms |
38704 KB |
Output is correct |
95 |
Correct |
166 ms |
38572 KB |
Output is correct |
96 |
Correct |
167 ms |
38664 KB |
Output is correct |
97 |
Correct |
175 ms |
38656 KB |
Output is correct |
98 |
Correct |
184 ms |
38584 KB |
Output is correct |
99 |
Correct |
68 ms |
38724 KB |
Output is correct |
100 |
Correct |
264 ms |
39340 KB |
Output is correct |
101 |
Correct |
263 ms |
39848 KB |
Output is correct |
102 |
Correct |
242 ms |
40652 KB |
Output is correct |
103 |
Correct |
231 ms |
40516 KB |
Output is correct |
104 |
Correct |
210 ms |
39464 KB |
Output is correct |
105 |
Correct |
237 ms |
39968 KB |
Output is correct |
106 |
Correct |
228 ms |
40964 KB |
Output is correct |
107 |
Correct |
275 ms |
39924 KB |
Output is correct |
108 |
Correct |
211 ms |
40152 KB |
Output is correct |
109 |
Correct |
214 ms |
39912 KB |
Output is correct |
110 |
Correct |
232 ms |
39844 KB |
Output is correct |
111 |
Correct |
239 ms |
39424 KB |
Output is correct |