#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int N=200'010;
const int LG=20;
struct event {
int s, e, idx;
};
event es[N];
struct Binary_Lifting {
int up[N][LG];
void add(int node, int par) {
up[node][0]=par;
for(int i = 1;i<LG;i++) {
up[node][i]=up[up[node][i-1]][i-1];
}
}
int get(int start, int num) {
if(es[start].e>=num)return 1;
int res=1;
for(int i = LG-1;i>=0;i--) {
if(es[up[start][i]].e<num) {
start=up[start][i];
res+=(1<<i);
}
}
if(es[up[start][0]].e>=num)return res+1;
return -1;
}
};
bool operator<(const event &a, const event &b) {
return a.e>b.e;
}
Binary_Lifting bl;
vector<int> child[N];
int out[N];
void dfs(int at) {
for(int to:child[at]){
bl.add(to,at);
dfs(to);
}
}
pair<int,int> T[4*N];
void update(int at, pair<int,int> val, int tl=0, int tr=N-1, int v=1) {
if(tl==tr) {
T[v]=max(T[v], val);
return;
}
int tm=(tl+tr)/2;
if(at<=tm) {
update(at, val, tl,tm,2*v);
}
else update(at, val, tm+1,tr,2*v+1);
T[v]=max(T[2*v],T[2*v+1]);
}
pair<int,int> query(int l, int r, int tl=0, int tr=N-1, int v=1) {
if(tr<l or r<tl)return {-1,-1};
if(l<=tl and tr<=r)return T[v];
int tm=(tl+tr)/2;
return max(query(l,r,tl,tm,2*v), query(l,r,tm+1,tr,2*v+1));
}
int main () {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
map<int,int> cc;
for(int i = 0;i<n;i++) {
cin >> es[i].e >> es[i].s;
es[i].s*=-1;
es[i].e*=-1;
cc[es[i].s]=1;
cc[es[i].e]=1;
es[i].idx=i;
}
vector<event> on[2*n], off[2*n];
{
int cnt=0;
for(auto &i:cc) {
i.second=cnt++;
}
for(int i = 0;i<n;i++) {
es[i].s=cc[es[i].s];
es[i].e=cc[es[i].e];
}
}
for(int i = 0;i<n;i++) {
update(es[i].s, {es[i].e, i});
}
for(int i = 0;i<n;i++) {
pair<int,int> c=query(es[i].s, es[i].e);
if(c.first>es[i].e){
child[c.second].push_back(i);
out[i]++;
}
}
for(int i = 0;i<n;i++) {
if(out[i]==0){
bl.add(i,i);
dfs(i);
}
}
while(q--) {
int s, e;
cin >> s >> e;
swap(s,e);
if(es[s-1].s>es[e-1].s){
cout << "impossible" << "\n";
continue;
}
if(s==e) {
cout << 0 << "\n";
continue;
}
int res=bl.get(s-1,es[e-1].s);
if(res>=0)cout << res << "\n";
else cout << "impossible\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
258 ms |
37000 KB |
Output is correct |
3 |
Correct |
265 ms |
39844 KB |
Output is correct |
4 |
Correct |
298 ms |
39828 KB |
Output is correct |
5 |
Correct |
271 ms |
39960 KB |
Output is correct |
6 |
Correct |
264 ms |
40212 KB |
Output is correct |
7 |
Correct |
231 ms |
40160 KB |
Output is correct |
8 |
Correct |
262 ms |
41152 KB |
Output is correct |
9 |
Correct |
221 ms |
41036 KB |
Output is correct |
10 |
Correct |
262 ms |
38948 KB |
Output is correct |
11 |
Correct |
252 ms |
40120 KB |
Output is correct |
12 |
Correct |
125 ms |
33708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5332 KB |
Output is correct |
13 |
Correct |
4 ms |
5380 KB |
Output is correct |
14 |
Correct |
4 ms |
5332 KB |
Output is correct |
15 |
Correct |
4 ms |
5332 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
36 ms |
7056 KB |
Output is correct |
20 |
Correct |
36 ms |
7116 KB |
Output is correct |
21 |
Correct |
37 ms |
7372 KB |
Output is correct |
22 |
Correct |
31 ms |
7196 KB |
Output is correct |
23 |
Correct |
36 ms |
7444 KB |
Output is correct |
24 |
Correct |
33 ms |
7324 KB |
Output is correct |
25 |
Correct |
36 ms |
7116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5332 KB |
Output is correct |
13 |
Correct |
4 ms |
5332 KB |
Output is correct |
14 |
Correct |
5 ms |
5332 KB |
Output is correct |
15 |
Correct |
4 ms |
5332 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
194 ms |
36044 KB |
Output is correct |
20 |
Correct |
184 ms |
37296 KB |
Output is correct |
21 |
Correct |
185 ms |
32136 KB |
Output is correct |
22 |
Correct |
240 ms |
37900 KB |
Output is correct |
23 |
Correct |
232 ms |
37660 KB |
Output is correct |
24 |
Correct |
289 ms |
42376 KB |
Output is correct |
25 |
Correct |
54 ms |
24652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
36788 KB |
Output is correct |
2 |
Correct |
239 ms |
39848 KB |
Output is correct |
3 |
Correct |
294 ms |
39852 KB |
Output is correct |
4 |
Correct |
227 ms |
41092 KB |
Output is correct |
5 |
Correct |
266 ms |
38952 KB |
Output is correct |
6 |
Correct |
317 ms |
44328 KB |
Output is correct |
7 |
Correct |
380 ms |
44280 KB |
Output is correct |
8 |
Correct |
293 ms |
42876 KB |
Output is correct |
9 |
Correct |
281 ms |
37704 KB |
Output is correct |
10 |
Correct |
318 ms |
43832 KB |
Output is correct |
11 |
Correct |
314 ms |
42564 KB |
Output is correct |
12 |
Correct |
304 ms |
43880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
258 ms |
37000 KB |
Output is correct |
3 |
Correct |
265 ms |
39844 KB |
Output is correct |
4 |
Correct |
298 ms |
39828 KB |
Output is correct |
5 |
Correct |
271 ms |
39960 KB |
Output is correct |
6 |
Correct |
264 ms |
40212 KB |
Output is correct |
7 |
Correct |
231 ms |
40160 KB |
Output is correct |
8 |
Correct |
262 ms |
41152 KB |
Output is correct |
9 |
Correct |
221 ms |
41036 KB |
Output is correct |
10 |
Correct |
262 ms |
38948 KB |
Output is correct |
11 |
Correct |
252 ms |
40120 KB |
Output is correct |
12 |
Correct |
125 ms |
33708 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5332 KB |
Output is correct |
16 |
Correct |
5 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5332 KB |
Output is correct |
20 |
Correct |
4 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5380 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5332 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
36 ms |
7056 KB |
Output is correct |
32 |
Correct |
36 ms |
7116 KB |
Output is correct |
33 |
Correct |
37 ms |
7372 KB |
Output is correct |
34 |
Correct |
31 ms |
7196 KB |
Output is correct |
35 |
Correct |
36 ms |
7444 KB |
Output is correct |
36 |
Correct |
33 ms |
7324 KB |
Output is correct |
37 |
Correct |
36 ms |
7116 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
4 ms |
5332 KB |
Output is correct |
41 |
Correct |
4 ms |
5332 KB |
Output is correct |
42 |
Correct |
5 ms |
5332 KB |
Output is correct |
43 |
Correct |
4 ms |
5332 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
4 ms |
5332 KB |
Output is correct |
46 |
Correct |
4 ms |
5204 KB |
Output is correct |
47 |
Correct |
194 ms |
36044 KB |
Output is correct |
48 |
Correct |
184 ms |
37296 KB |
Output is correct |
49 |
Correct |
185 ms |
32136 KB |
Output is correct |
50 |
Correct |
240 ms |
37900 KB |
Output is correct |
51 |
Correct |
232 ms |
37660 KB |
Output is correct |
52 |
Correct |
289 ms |
42376 KB |
Output is correct |
53 |
Correct |
54 ms |
24652 KB |
Output is correct |
54 |
Correct |
274 ms |
36788 KB |
Output is correct |
55 |
Correct |
239 ms |
39848 KB |
Output is correct |
56 |
Correct |
294 ms |
39852 KB |
Output is correct |
57 |
Correct |
227 ms |
41092 KB |
Output is correct |
58 |
Correct |
266 ms |
38952 KB |
Output is correct |
59 |
Correct |
317 ms |
44328 KB |
Output is correct |
60 |
Correct |
380 ms |
44280 KB |
Output is correct |
61 |
Correct |
293 ms |
42876 KB |
Output is correct |
62 |
Correct |
281 ms |
37704 KB |
Output is correct |
63 |
Correct |
318 ms |
43832 KB |
Output is correct |
64 |
Correct |
314 ms |
42564 KB |
Output is correct |
65 |
Correct |
304 ms |
43880 KB |
Output is correct |
66 |
Correct |
3 ms |
5076 KB |
Output is correct |
67 |
Correct |
227 ms |
39896 KB |
Output is correct |
68 |
Correct |
213 ms |
39756 KB |
Output is correct |
69 |
Correct |
244 ms |
39732 KB |
Output is correct |
70 |
Correct |
242 ms |
39980 KB |
Output is correct |
71 |
Correct |
242 ms |
40228 KB |
Output is correct |
72 |
Correct |
261 ms |
40172 KB |
Output is correct |
73 |
Correct |
229 ms |
41120 KB |
Output is correct |
74 |
Correct |
227 ms |
41156 KB |
Output is correct |
75 |
Correct |
254 ms |
38952 KB |
Output is correct |
76 |
Correct |
270 ms |
40248 KB |
Output is correct |
77 |
Correct |
128 ms |
33740 KB |
Output is correct |
78 |
Correct |
3 ms |
5076 KB |
Output is correct |
79 |
Correct |
5 ms |
5332 KB |
Output is correct |
80 |
Correct |
5 ms |
5332 KB |
Output is correct |
81 |
Correct |
4 ms |
5292 KB |
Output is correct |
82 |
Correct |
6 ms |
5332 KB |
Output is correct |
83 |
Correct |
5 ms |
5332 KB |
Output is correct |
84 |
Correct |
5 ms |
5304 KB |
Output is correct |
85 |
Correct |
3 ms |
5204 KB |
Output is correct |
86 |
Correct |
36 ms |
7984 KB |
Output is correct |
87 |
Correct |
34 ms |
8124 KB |
Output is correct |
88 |
Correct |
40 ms |
8404 KB |
Output is correct |
89 |
Correct |
44 ms |
8220 KB |
Output is correct |
90 |
Correct |
33 ms |
8348 KB |
Output is correct |
91 |
Correct |
30 ms |
8320 KB |
Output is correct |
92 |
Correct |
32 ms |
8136 KB |
Output is correct |
93 |
Correct |
209 ms |
38048 KB |
Output is correct |
94 |
Correct |
193 ms |
37368 KB |
Output is correct |
95 |
Correct |
188 ms |
33008 KB |
Output is correct |
96 |
Correct |
227 ms |
37832 KB |
Output is correct |
97 |
Correct |
229 ms |
37764 KB |
Output is correct |
98 |
Correct |
272 ms |
42316 KB |
Output is correct |
99 |
Correct |
55 ms |
24628 KB |
Output is correct |
100 |
Correct |
401 ms |
44264 KB |
Output is correct |
101 |
Correct |
311 ms |
44284 KB |
Output is correct |
102 |
Correct |
367 ms |
42836 KB |
Output is correct |
103 |
Correct |
317 ms |
44028 KB |
Output is correct |
104 |
Correct |
374 ms |
42528 KB |
Output is correct |
105 |
Correct |
319 ms |
43904 KB |
Output is correct |
106 |
Correct |
219 ms |
36352 KB |
Output is correct |
107 |
Correct |
291 ms |
39184 KB |
Output is correct |
108 |
Correct |
218 ms |
34960 KB |
Output is correct |
109 |
Correct |
205 ms |
35020 KB |
Output is correct |
110 |
Correct |
363 ms |
41636 KB |
Output is correct |
111 |
Correct |
324 ms |
41472 KB |
Output is correct |