#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
int n, q;
int pw2[20];
pair<int,int> a[100005];
int spa[100005][20];
set<int> re;
vector<int> re2;
map<int, int> en, de;
struct node
{
pair<int, int> mn = {2e9, -1};
node *l = NULL, *r = NULL;
} *root;
node* build(int l, int r)
{
node *ptr = new node();
if(l == r) return ptr;
int mid = (l + r) / 2;
ptr->l = build(l, mid);
ptr->r = build(mid + 1, r);
return ptr;
}
void up(node *ptr, int l, int r, pair<int, int> val, int po)
{
ptr->mn = min(ptr->mn, val);
if(l == r) return ;
int mid = (l + r) / 2;
if(po <= mid) up(ptr->l, l, mid, val, po);
else up(ptr->r, mid + 1, r, val, po);
}
pair<int, int> que(node *ptr, int l, int r, int ll, int rr)
{
if(ll <= l && r <= rr) return ptr->mn;
if(r < ll || rr < l) return {2e9, -1};
int mid = (l + r) / 2;
return min(que(ptr->l, l, mid, ll, rr), que(ptr->r, mid + 1, r, ll, rr));
}
int main()
{
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
pw2[0] = 1;
for(int i = 1; i < 20; i++)
pw2[i] = pw2[i - 1] * 2;
scanf("%d %d", &n, &q);
for(int i = 0; i < n; i++)
{
scanf("%d %d", &a[i].f, &a[i].s);
re.insert(a[i].f);
re.insert(a[i].s);
}
for(auto val: re)
{
en[val] = sz(re2);
de[sz(re2)] = val;
re2.pb(val);
}
root = build(0, sz(re2));
for(int i = 0; i < n; i++)
up(root, 0, sz(re2), {a[i].f, i}, en[a[i].s]);
for(int i = 0; i < n; i++)
{
pair<int, int> ret = que(root, 0, sz(re2), en[a[i].f], en[a[i].s]);
if(ret.s == i || ret.f == a[i].f)
spa[i][0] = -1;
else
spa[i][0] = ret.s;
// printf("p: %d %d\n",i, spa[i][0]);
}
for(int j = 1; j < 20; j++)
for(int i = 0; i < n; i++)
if(spa[i][j - 1] != -1)
spa[i][j] = spa[spa[i][j - 1]][j - 1];
else
spa[i][j] = -1;
for(int i = 0; i < q; i++)
{
int s, t;
scanf("%d %d", &s, &t);
s--, t--;
if(s == t)
printf("0\n");
else if(a[t].f <= a[s].s && a[s].s <= a[t].s)
printf("1\n");
else if(a[s].s > a[t].s)
printf("impossible\n");
else
{
int ans = 0, cur = t;
for(int j = 19; j >= 0; j--)
{
if(spa[cur][j] != -1 && a[s].s < a[spa[cur][j]].f)
{
ans += pw2[j];
cur = spa[cur][j];
}
}
if(spa[cur][0] != -1 && a[spa[cur][0]].f <= a[s].s)
printf("%d\n", ans + 2);
else
printf("impossible\n");
}
}
return 0;
}
/*
8 5
1 2
3 4
1 5
6 7
5 10
10 20
15 20
999999999 1000000000
1 6
1 7
2 4
3 3
5 8
*/
Compilation message
events.cpp: In function 'int main()':
events.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
events.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d %d", &a[i].f, &a[i].s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d %d", &s, &t);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
333 ms |
30536 KB |
Output is correct |
3 |
Correct |
398 ms |
30420 KB |
Output is correct |
4 |
Correct |
401 ms |
30424 KB |
Output is correct |
5 |
Correct |
411 ms |
32580 KB |
Output is correct |
6 |
Correct |
412 ms |
37304 KB |
Output is correct |
7 |
Correct |
440 ms |
38032 KB |
Output is correct |
8 |
Correct |
452 ms |
54464 KB |
Output is correct |
9 |
Correct |
483 ms |
54424 KB |
Output is correct |
10 |
Correct |
368 ms |
33712 KB |
Output is correct |
11 |
Correct |
429 ms |
41852 KB |
Output is correct |
12 |
Correct |
179 ms |
32992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
668 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
41 ms |
3488 KB |
Output is correct |
20 |
Correct |
42 ms |
3232 KB |
Output is correct |
21 |
Correct |
43 ms |
3524 KB |
Output is correct |
22 |
Correct |
31 ms |
3612 KB |
Output is correct |
23 |
Correct |
39 ms |
4424 KB |
Output is correct |
24 |
Correct |
33 ms |
4372 KB |
Output is correct |
25 |
Correct |
46 ms |
4116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
624 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
341 ms |
30204 KB |
Output is correct |
20 |
Correct |
297 ms |
30708 KB |
Output is correct |
21 |
Correct |
324 ms |
31332 KB |
Output is correct |
22 |
Correct |
383 ms |
38920 KB |
Output is correct |
23 |
Correct |
544 ms |
52164 KB |
Output is correct |
24 |
Correct |
590 ms |
52300 KB |
Output is correct |
25 |
Correct |
32 ms |
10060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
30416 KB |
Output is correct |
2 |
Correct |
375 ms |
30436 KB |
Output is correct |
3 |
Correct |
433 ms |
30420 KB |
Output is correct |
4 |
Correct |
521 ms |
51788 KB |
Output is correct |
5 |
Correct |
414 ms |
30856 KB |
Output is correct |
6 |
Correct |
623 ms |
51472 KB |
Output is correct |
7 |
Correct |
641 ms |
51508 KB |
Output is correct |
8 |
Correct |
609 ms |
51616 KB |
Output is correct |
9 |
Correct |
670 ms |
50728 KB |
Output is correct |
10 |
Correct |
764 ms |
51152 KB |
Output is correct |
11 |
Correct |
737 ms |
51068 KB |
Output is correct |
12 |
Correct |
869 ms |
51348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
333 ms |
30536 KB |
Output is correct |
3 |
Correct |
398 ms |
30420 KB |
Output is correct |
4 |
Correct |
401 ms |
30424 KB |
Output is correct |
5 |
Correct |
411 ms |
32580 KB |
Output is correct |
6 |
Correct |
412 ms |
37304 KB |
Output is correct |
7 |
Correct |
440 ms |
38032 KB |
Output is correct |
8 |
Correct |
452 ms |
54464 KB |
Output is correct |
9 |
Correct |
483 ms |
54424 KB |
Output is correct |
10 |
Correct |
368 ms |
33712 KB |
Output is correct |
11 |
Correct |
429 ms |
41852 KB |
Output is correct |
12 |
Correct |
179 ms |
32992 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
668 KB |
Output is correct |
26 |
Correct |
3 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
724 KB |
Output is correct |
29 |
Correct |
4 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
41 ms |
3488 KB |
Output is correct |
32 |
Correct |
42 ms |
3232 KB |
Output is correct |
33 |
Correct |
43 ms |
3524 KB |
Output is correct |
34 |
Correct |
31 ms |
3612 KB |
Output is correct |
35 |
Correct |
39 ms |
4424 KB |
Output is correct |
36 |
Correct |
33 ms |
4372 KB |
Output is correct |
37 |
Correct |
46 ms |
4116 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
624 KB |
Output is correct |
41 |
Correct |
2 ms |
632 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
324 KB |
Output is correct |
47 |
Correct |
341 ms |
30204 KB |
Output is correct |
48 |
Correct |
297 ms |
30708 KB |
Output is correct |
49 |
Correct |
324 ms |
31332 KB |
Output is correct |
50 |
Correct |
383 ms |
38920 KB |
Output is correct |
51 |
Correct |
544 ms |
52164 KB |
Output is correct |
52 |
Correct |
590 ms |
52300 KB |
Output is correct |
53 |
Correct |
32 ms |
10060 KB |
Output is correct |
54 |
Correct |
363 ms |
30416 KB |
Output is correct |
55 |
Correct |
375 ms |
30436 KB |
Output is correct |
56 |
Correct |
433 ms |
30420 KB |
Output is correct |
57 |
Correct |
521 ms |
51788 KB |
Output is correct |
58 |
Correct |
414 ms |
30856 KB |
Output is correct |
59 |
Correct |
623 ms |
51472 KB |
Output is correct |
60 |
Correct |
641 ms |
51508 KB |
Output is correct |
61 |
Correct |
609 ms |
51616 KB |
Output is correct |
62 |
Correct |
670 ms |
50728 KB |
Output is correct |
63 |
Correct |
764 ms |
51152 KB |
Output is correct |
64 |
Correct |
737 ms |
51068 KB |
Output is correct |
65 |
Correct |
869 ms |
51348 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
444 ms |
33200 KB |
Output is correct |
68 |
Correct |
445 ms |
33188 KB |
Output is correct |
69 |
Correct |
458 ms |
33252 KB |
Output is correct |
70 |
Correct |
501 ms |
35356 KB |
Output is correct |
71 |
Correct |
493 ms |
37296 KB |
Output is correct |
72 |
Correct |
468 ms |
37908 KB |
Output is correct |
73 |
Correct |
566 ms |
54400 KB |
Output is correct |
74 |
Correct |
499 ms |
54444 KB |
Output is correct |
75 |
Correct |
382 ms |
33616 KB |
Output is correct |
76 |
Correct |
462 ms |
41888 KB |
Output is correct |
77 |
Correct |
188 ms |
33028 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
624 KB |
Output is correct |
80 |
Correct |
2 ms |
616 KB |
Output is correct |
81 |
Correct |
2 ms |
596 KB |
Output is correct |
82 |
Correct |
2 ms |
596 KB |
Output is correct |
83 |
Correct |
2 ms |
724 KB |
Output is correct |
84 |
Correct |
3 ms |
728 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
43 ms |
3480 KB |
Output is correct |
87 |
Correct |
41 ms |
3276 KB |
Output is correct |
88 |
Correct |
41 ms |
3528 KB |
Output is correct |
89 |
Correct |
36 ms |
3572 KB |
Output is correct |
90 |
Correct |
41 ms |
4444 KB |
Output is correct |
91 |
Correct |
33 ms |
4428 KB |
Output is correct |
92 |
Correct |
44 ms |
4080 KB |
Output is correct |
93 |
Correct |
373 ms |
31452 KB |
Output is correct |
94 |
Correct |
342 ms |
30692 KB |
Output is correct |
95 |
Correct |
335 ms |
31428 KB |
Output is correct |
96 |
Correct |
421 ms |
39108 KB |
Output is correct |
97 |
Correct |
553 ms |
52200 KB |
Output is correct |
98 |
Correct |
613 ms |
52284 KB |
Output is correct |
99 |
Correct |
41 ms |
10136 KB |
Output is correct |
100 |
Correct |
698 ms |
54112 KB |
Output is correct |
101 |
Correct |
607 ms |
54196 KB |
Output is correct |
102 |
Correct |
605 ms |
54288 KB |
Output is correct |
103 |
Correct |
635 ms |
53740 KB |
Output is correct |
104 |
Correct |
660 ms |
53356 KB |
Output is correct |
105 |
Correct |
584 ms |
53808 KB |
Output is correct |
106 |
Correct |
303 ms |
27580 KB |
Output is correct |
107 |
Correct |
360 ms |
32628 KB |
Output is correct |
108 |
Correct |
426 ms |
33412 KB |
Output is correct |
109 |
Correct |
368 ms |
33392 KB |
Output is correct |
110 |
Correct |
612 ms |
54100 KB |
Output is correct |
111 |
Correct |
617 ms |
54160 KB |
Output is correct |