#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
int n, q;
int doseg[100005];
int tree[400005];
void update(int v, int le, int ri, int pos)
{
if(le==ri) tree[v]=pos;
else
{
int mid=(le+ri)/2;
if(pos<=mid) update(2*v, le, mid, pos);
else update(2*v+1, mid+1, ri, pos);
if(doseg[tree[2*v]]<doseg[tree[2*v+1]]) tree[v]=tree[2*v];
else tree[v]=tree[2*v+1];
}
}
int query(int v, int le, int ri, int be, int en)
{
if(le>en || ri<be) return n;
else if(be<=le && ri<=en) return tree[v];
else
{
int mid=(le+ri)/2;
int p1=query(2*v, le, mid, be, en), p2=query(2*v+1, mid+1, ri, be, en);
if(doseg[p1]<doseg[p2]) return p1;
else return p2;
}
}
struct event
{
int be, en, nom;
event() {}
event(int a, int b, int c)
{
be=a;
en=b;
nom=c;
}
};
bool operator<(event p, event q)
{
if(p.en!=q.en) return p.en<q.en;
return p.be<q.be;
}
int nnom[100005];
event ev[100005];
int par[20][100005];
void fill_table()
{
for(int i=1; i<20; i++)
{
for(int j=0; j<n; j++) par[i][j]=par[i-1][par[i-1][j]];
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=0; i<n; i++)
{
cin>>ev[i].be>>ev[i].en;
ev[i].nom=i+1;
}
sort(ev, ev+n);
for(int i=0; i<n; i++) nnom[ev[i].nom]=i;
doseg[n]=1e9;
for(int i=0; i<n; i++)
{
int le=-1, ri=i;
while(ri-le>1)
{
int mid=(le+ri)/2;
if(ev[mid].en>=ev[i].be) ri=mid;
else le=mid;
}
doseg[i]=ri;
update(1, 0, n-1, i);
par[0][i]=query(1, 0, n-1, doseg[i], i);
}
fill_table();
for(int i=0; i<q; i++)
{
int le, ri;
cin>>le>>ri;
le=nnom[le];
ri=nnom[ri];
if(le==ri) cout<<0<<endl;
else if(ev[le].en==ev[ri].en) cout<<1<<endl;
else if(le>ri) cout<<"impossible\n";
else if(doseg[par[19][ri]]>le) cout<<"impossible\n";
else if(doseg[ri]<=le) cout<<1<<endl;
else
{
int brpr=0;
for(int i=19; i>=0; i--)
{
int p2=par[i][ri];
if(doseg[p2]>le)
{
brpr+=(1<<i);
ri=p2;
}
}
cout<<brpr+2<<endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
78 ms |
13488 KB |
Output is correct |
3 |
Correct |
75 ms |
13544 KB |
Output is correct |
4 |
Correct |
79 ms |
13648 KB |
Output is correct |
5 |
Correct |
77 ms |
13604 KB |
Output is correct |
6 |
Correct |
78 ms |
13712 KB |
Output is correct |
7 |
Correct |
78 ms |
13908 KB |
Output is correct |
8 |
Correct |
69 ms |
13908 KB |
Output is correct |
9 |
Correct |
71 ms |
15440 KB |
Output is correct |
10 |
Correct |
81 ms |
15244 KB |
Output is correct |
11 |
Correct |
80 ms |
15552 KB |
Output is correct |
12 |
Correct |
61 ms |
14708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10712 KB |
Output is correct |
10 |
Correct |
1 ms |
10584 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10716 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10716 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
26 ms |
11860 KB |
Output is correct |
20 |
Correct |
27 ms |
12040 KB |
Output is correct |
21 |
Correct |
29 ms |
12124 KB |
Output is correct |
22 |
Correct |
20 ms |
12380 KB |
Output is correct |
23 |
Correct |
20 ms |
12376 KB |
Output is correct |
24 |
Correct |
19 ms |
12372 KB |
Output is correct |
25 |
Correct |
22 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10712 KB |
Output is correct |
10 |
Correct |
1 ms |
10700 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10736 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10712 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
57 ms |
12452 KB |
Output is correct |
20 |
Correct |
53 ms |
11856 KB |
Output is correct |
21 |
Correct |
68 ms |
12700 KB |
Output is correct |
22 |
Correct |
57 ms |
13124 KB |
Output is correct |
23 |
Correct |
54 ms |
12988 KB |
Output is correct |
24 |
Correct |
64 ms |
13456 KB |
Output is correct |
25 |
Correct |
45 ms |
12368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
13596 KB |
Output is correct |
2 |
Correct |
76 ms |
13484 KB |
Output is correct |
3 |
Correct |
79 ms |
13520 KB |
Output is correct |
4 |
Correct |
70 ms |
13940 KB |
Output is correct |
5 |
Correct |
80 ms |
14152 KB |
Output is correct |
6 |
Correct |
88 ms |
13740 KB |
Output is correct |
7 |
Correct |
88 ms |
13656 KB |
Output is correct |
8 |
Correct |
83 ms |
13724 KB |
Output is correct |
9 |
Correct |
57 ms |
12760 KB |
Output is correct |
10 |
Correct |
81 ms |
13396 KB |
Output is correct |
11 |
Correct |
82 ms |
13060 KB |
Output is correct |
12 |
Correct |
80 ms |
13392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
78 ms |
13488 KB |
Output is correct |
3 |
Correct |
75 ms |
13544 KB |
Output is correct |
4 |
Correct |
79 ms |
13648 KB |
Output is correct |
5 |
Correct |
77 ms |
13604 KB |
Output is correct |
6 |
Correct |
78 ms |
13712 KB |
Output is correct |
7 |
Correct |
78 ms |
13908 KB |
Output is correct |
8 |
Correct |
69 ms |
13908 KB |
Output is correct |
9 |
Correct |
71 ms |
15440 KB |
Output is correct |
10 |
Correct |
81 ms |
15244 KB |
Output is correct |
11 |
Correct |
80 ms |
15552 KB |
Output is correct |
12 |
Correct |
61 ms |
14708 KB |
Output is correct |
13 |
Correct |
1 ms |
10584 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10584 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10584 KB |
Output is correct |
18 |
Correct |
2 ms |
10584 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10712 KB |
Output is correct |
22 |
Correct |
1 ms |
10584 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10716 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
2 ms |
10716 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
26 ms |
11860 KB |
Output is correct |
32 |
Correct |
27 ms |
12040 KB |
Output is correct |
33 |
Correct |
29 ms |
12124 KB |
Output is correct |
34 |
Correct |
20 ms |
12380 KB |
Output is correct |
35 |
Correct |
20 ms |
12376 KB |
Output is correct |
36 |
Correct |
19 ms |
12372 KB |
Output is correct |
37 |
Correct |
22 ms |
11980 KB |
Output is correct |
38 |
Correct |
1 ms |
10700 KB |
Output is correct |
39 |
Correct |
1 ms |
10584 KB |
Output is correct |
40 |
Correct |
2 ms |
10736 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10712 KB |
Output is correct |
44 |
Correct |
2 ms |
10588 KB |
Output is correct |
45 |
Correct |
2 ms |
10588 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
57 ms |
12452 KB |
Output is correct |
48 |
Correct |
53 ms |
11856 KB |
Output is correct |
49 |
Correct |
68 ms |
12700 KB |
Output is correct |
50 |
Correct |
57 ms |
13124 KB |
Output is correct |
51 |
Correct |
54 ms |
12988 KB |
Output is correct |
52 |
Correct |
64 ms |
13456 KB |
Output is correct |
53 |
Correct |
45 ms |
12368 KB |
Output is correct |
54 |
Correct |
83 ms |
13596 KB |
Output is correct |
55 |
Correct |
76 ms |
13484 KB |
Output is correct |
56 |
Correct |
79 ms |
13520 KB |
Output is correct |
57 |
Correct |
70 ms |
13940 KB |
Output is correct |
58 |
Correct |
80 ms |
14152 KB |
Output is correct |
59 |
Correct |
88 ms |
13740 KB |
Output is correct |
60 |
Correct |
88 ms |
13656 KB |
Output is correct |
61 |
Correct |
83 ms |
13724 KB |
Output is correct |
62 |
Correct |
57 ms |
12760 KB |
Output is correct |
63 |
Correct |
81 ms |
13396 KB |
Output is correct |
64 |
Correct |
82 ms |
13060 KB |
Output is correct |
65 |
Correct |
80 ms |
13392 KB |
Output is correct |
66 |
Correct |
1 ms |
10588 KB |
Output is correct |
67 |
Correct |
76 ms |
14932 KB |
Output is correct |
68 |
Correct |
76 ms |
14836 KB |
Output is correct |
69 |
Correct |
81 ms |
14840 KB |
Output is correct |
70 |
Correct |
77 ms |
14928 KB |
Output is correct |
71 |
Correct |
76 ms |
14928 KB |
Output is correct |
72 |
Correct |
77 ms |
14856 KB |
Output is correct |
73 |
Correct |
70 ms |
15444 KB |
Output is correct |
74 |
Correct |
70 ms |
15444 KB |
Output is correct |
75 |
Correct |
83 ms |
15184 KB |
Output is correct |
76 |
Correct |
84 ms |
15432 KB |
Output is correct |
77 |
Correct |
64 ms |
14676 KB |
Output is correct |
78 |
Correct |
1 ms |
10588 KB |
Output is correct |
79 |
Correct |
2 ms |
10588 KB |
Output is correct |
80 |
Correct |
2 ms |
10588 KB |
Output is correct |
81 |
Correct |
2 ms |
10588 KB |
Output is correct |
82 |
Correct |
2 ms |
10588 KB |
Output is correct |
83 |
Correct |
2 ms |
10720 KB |
Output is correct |
84 |
Correct |
2 ms |
10584 KB |
Output is correct |
85 |
Correct |
2 ms |
10584 KB |
Output is correct |
86 |
Correct |
30 ms |
12228 KB |
Output is correct |
87 |
Correct |
22 ms |
12112 KB |
Output is correct |
88 |
Correct |
26 ms |
12380 KB |
Output is correct |
89 |
Correct |
20 ms |
12376 KB |
Output is correct |
90 |
Correct |
19 ms |
12376 KB |
Output is correct |
91 |
Correct |
19 ms |
12328 KB |
Output is correct |
92 |
Correct |
22 ms |
11856 KB |
Output is correct |
93 |
Correct |
56 ms |
13136 KB |
Output is correct |
94 |
Correct |
53 ms |
12372 KB |
Output is correct |
95 |
Correct |
64 ms |
13124 KB |
Output is correct |
96 |
Correct |
58 ms |
13124 KB |
Output is correct |
97 |
Correct |
57 ms |
13232 KB |
Output is correct |
98 |
Correct |
63 ms |
13136 KB |
Output is correct |
99 |
Correct |
45 ms |
12372 KB |
Output is correct |
100 |
Correct |
89 ms |
15048 KB |
Output is correct |
101 |
Correct |
88 ms |
14904 KB |
Output is correct |
102 |
Correct |
90 ms |
15360 KB |
Output is correct |
103 |
Correct |
81 ms |
14676 KB |
Output is correct |
104 |
Correct |
83 ms |
14420 KB |
Output is correct |
105 |
Correct |
81 ms |
14676 KB |
Output is correct |
106 |
Correct |
83 ms |
14360 KB |
Output is correct |
107 |
Correct |
86 ms |
14360 KB |
Output is correct |
108 |
Correct |
81 ms |
14932 KB |
Output is correct |
109 |
Correct |
82 ms |
14928 KB |
Output is correct |
110 |
Correct |
101 ms |
15204 KB |
Output is correct |
111 |
Correct |
93 ms |
14932 KB |
Output is correct |