#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define ld long double
#define pi pair<int, int>
const int INF = (int)4e18;
const int MODa = 1e9 + 7;
const int MOD = 998244353;
const int MXN = 100005;
int n, q;
int l[MXN]{};
int r[MXN]{};
int bl[MXN][25]{};
int cs(int a, int b)
{
if(r[a]==r[b]) return l[a]>l[b];
return r[a]>r[b];
}
int seg[4*MXN]{};
vector<int> ss(0);
vector<int> sl(0);
void build(int sl, int sr, int p)
{
if(sl==sr)
{
seg[p] = ss[sl];
return;
}
int m = (sl+sr)/2;
build(sl, m, 2*p);
build(m+1, sr, 2*p + 1);
if(l[seg[2*p]]<=l[seg[2*p + 1]]) seg[p] = seg[2*p];
else seg[p] = seg[2*p + 1];
}
int query(int sl, int sr, int p, int ql, int qr)
{
if(sl>qr || ql>sr) return 0;
if(ql<=sl && sr<=qr) return seg[p];
int m = (sl+sr)/2;
int x = query(sl, m, 2*p, ql, qr);
int y = query(m+1, sr, 2*p + 1, ql, qr);
if(l[x]<=l[y]) return x;
return y;
}
void solve()
{
cin>>n>>q;
l[0] = INF;
for(int i=1; i<=n; i++)
{
cin>>l[i]>>r[i];
}
ss.resize(n+1);
sl.resize(n+2);
iota(ss.begin(), ss.end(), 0);
sort(ss.begin()+1, ss.end(), cs);
for(int i=1; i<=n; i++)
{
sl[i] = -r[ss[i]];
}
build(1, n, 1);
for(int i=1; i<=n; i++)
{
int j = ss[i];
int lm = -l[j];
int el = upper_bound(sl.begin()+1, sl.end(), lm) - sl.begin();
bl[j][0] = query(1, n, 1, i, el-1);
}
for(int j=1; j<25; j++)
{
for(int i=1; i<=n; i++)
{
bl[i][j] = bl[bl[i][j-1]][j-1];
}
}
for(int i=1; i<=q ;i++)
{
int s, e;
cin>>s>>e;
if(r[s]>r[e])
{
cout<<"impossible\n";
continue;
}
int ans = 0;
if(s==e)
{
cout<<0<<"\n";
continue;
}
if(r[s]>=l[e])
{
cout<<1<<"\n";
continue;
}
if(l[bl[e][24]]>r[s])
{
cout<<"impossible\n";
continue;
}
int x = e;
for(int j=24; j>=0; j--)
{
if(l[bl[x][j]]<=r[s]) continue;
ans += (1<<j);
x = bl[x][j];
}
cout<<ans+2<<"\n";
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin>>t;
while(t--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
79 ms |
29924 KB |
Output is correct |
3 |
Correct |
85 ms |
29780 KB |
Output is correct |
4 |
Correct |
108 ms |
29760 KB |
Output is correct |
5 |
Correct |
85 ms |
30036 KB |
Output is correct |
6 |
Correct |
84 ms |
30032 KB |
Output is correct |
7 |
Correct |
83 ms |
30036 KB |
Output is correct |
8 |
Correct |
92 ms |
30232 KB |
Output is correct |
9 |
Correct |
76 ms |
30292 KB |
Output is correct |
10 |
Correct |
93 ms |
30224 KB |
Output is correct |
11 |
Correct |
92 ms |
30488 KB |
Output is correct |
12 |
Correct |
55 ms |
29520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4552 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4596 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4552 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4596 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4596 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
26 ms |
6300 KB |
Output is correct |
20 |
Correct |
24 ms |
6224 KB |
Output is correct |
21 |
Correct |
26 ms |
6408 KB |
Output is correct |
22 |
Correct |
20 ms |
6484 KB |
Output is correct |
23 |
Correct |
20 ms |
6480 KB |
Output is correct |
24 |
Correct |
19 ms |
6408 KB |
Output is correct |
25 |
Correct |
22 ms |
5976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4552 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4596 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4584 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
60 ms |
27988 KB |
Output is correct |
20 |
Correct |
55 ms |
27684 KB |
Output is correct |
21 |
Correct |
65 ms |
27924 KB |
Output is correct |
22 |
Correct |
70 ms |
28244 KB |
Output is correct |
23 |
Correct |
57 ms |
27988 KB |
Output is correct |
24 |
Correct |
66 ms |
28220 KB |
Output is correct |
25 |
Correct |
36 ms |
27340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
29768 KB |
Output is correct |
2 |
Correct |
88 ms |
30036 KB |
Output is correct |
3 |
Correct |
109 ms |
29776 KB |
Output is correct |
4 |
Correct |
71 ms |
30284 KB |
Output is correct |
5 |
Correct |
94 ms |
30292 KB |
Output is correct |
6 |
Correct |
109 ms |
30116 KB |
Output is correct |
7 |
Correct |
99 ms |
30036 KB |
Output is correct |
8 |
Correct |
87 ms |
30128 KB |
Output is correct |
9 |
Correct |
61 ms |
27988 KB |
Output is correct |
10 |
Correct |
88 ms |
29524 KB |
Output is correct |
11 |
Correct |
86 ms |
29520 KB |
Output is correct |
12 |
Correct |
90 ms |
29968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
79 ms |
29924 KB |
Output is correct |
3 |
Correct |
85 ms |
29780 KB |
Output is correct |
4 |
Correct |
108 ms |
29760 KB |
Output is correct |
5 |
Correct |
85 ms |
30036 KB |
Output is correct |
6 |
Correct |
84 ms |
30032 KB |
Output is correct |
7 |
Correct |
83 ms |
30036 KB |
Output is correct |
8 |
Correct |
92 ms |
30232 KB |
Output is correct |
9 |
Correct |
76 ms |
30292 KB |
Output is correct |
10 |
Correct |
93 ms |
30224 KB |
Output is correct |
11 |
Correct |
92 ms |
30488 KB |
Output is correct |
12 |
Correct |
55 ms |
29520 KB |
Output is correct |
13 |
Correct |
1 ms |
4552 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4596 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4596 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
26 ms |
6300 KB |
Output is correct |
32 |
Correct |
24 ms |
6224 KB |
Output is correct |
33 |
Correct |
26 ms |
6408 KB |
Output is correct |
34 |
Correct |
20 ms |
6484 KB |
Output is correct |
35 |
Correct |
20 ms |
6480 KB |
Output is correct |
36 |
Correct |
19 ms |
6408 KB |
Output is correct |
37 |
Correct |
22 ms |
5976 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4584 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
60 ms |
27988 KB |
Output is correct |
48 |
Correct |
55 ms |
27684 KB |
Output is correct |
49 |
Correct |
65 ms |
27924 KB |
Output is correct |
50 |
Correct |
70 ms |
28244 KB |
Output is correct |
51 |
Correct |
57 ms |
27988 KB |
Output is correct |
52 |
Correct |
66 ms |
28220 KB |
Output is correct |
53 |
Correct |
36 ms |
27340 KB |
Output is correct |
54 |
Correct |
80 ms |
29768 KB |
Output is correct |
55 |
Correct |
88 ms |
30036 KB |
Output is correct |
56 |
Correct |
109 ms |
29776 KB |
Output is correct |
57 |
Correct |
71 ms |
30284 KB |
Output is correct |
58 |
Correct |
94 ms |
30292 KB |
Output is correct |
59 |
Correct |
109 ms |
30116 KB |
Output is correct |
60 |
Correct |
99 ms |
30036 KB |
Output is correct |
61 |
Correct |
87 ms |
30128 KB |
Output is correct |
62 |
Correct |
61 ms |
27988 KB |
Output is correct |
63 |
Correct |
88 ms |
29524 KB |
Output is correct |
64 |
Correct |
86 ms |
29520 KB |
Output is correct |
65 |
Correct |
90 ms |
29968 KB |
Output is correct |
66 |
Correct |
1 ms |
4440 KB |
Output is correct |
67 |
Correct |
80 ms |
29796 KB |
Output is correct |
68 |
Correct |
84 ms |
29780 KB |
Output is correct |
69 |
Correct |
107 ms |
29988 KB |
Output is correct |
70 |
Correct |
85 ms |
30032 KB |
Output is correct |
71 |
Correct |
89 ms |
30032 KB |
Output is correct |
72 |
Correct |
83 ms |
30004 KB |
Output is correct |
73 |
Correct |
71 ms |
30256 KB |
Output is correct |
74 |
Correct |
71 ms |
30284 KB |
Output is correct |
75 |
Correct |
92 ms |
30196 KB |
Output is correct |
76 |
Correct |
85 ms |
30300 KB |
Output is correct |
77 |
Correct |
55 ms |
29516 KB |
Output is correct |
78 |
Correct |
1 ms |
4444 KB |
Output is correct |
79 |
Correct |
1 ms |
4444 KB |
Output is correct |
80 |
Correct |
1 ms |
4576 KB |
Output is correct |
81 |
Correct |
1 ms |
4444 KB |
Output is correct |
82 |
Correct |
1 ms |
4444 KB |
Output is correct |
83 |
Correct |
1 ms |
4444 KB |
Output is correct |
84 |
Correct |
1 ms |
4444 KB |
Output is correct |
85 |
Correct |
1 ms |
4640 KB |
Output is correct |
86 |
Correct |
26 ms |
6200 KB |
Output is correct |
87 |
Correct |
25 ms |
6236 KB |
Output is correct |
88 |
Correct |
29 ms |
6576 KB |
Output is correct |
89 |
Correct |
20 ms |
6480 KB |
Output is correct |
90 |
Correct |
20 ms |
6480 KB |
Output is correct |
91 |
Correct |
19 ms |
6480 KB |
Output is correct |
92 |
Correct |
22 ms |
5976 KB |
Output is correct |
93 |
Correct |
58 ms |
28252 KB |
Output is correct |
94 |
Correct |
55 ms |
27236 KB |
Output is correct |
95 |
Correct |
64 ms |
28120 KB |
Output is correct |
96 |
Correct |
58 ms |
28252 KB |
Output is correct |
97 |
Correct |
55 ms |
27984 KB |
Output is correct |
98 |
Correct |
68 ms |
27976 KB |
Output is correct |
99 |
Correct |
41 ms |
27560 KB |
Output is correct |
100 |
Correct |
105 ms |
30032 KB |
Output is correct |
101 |
Correct |
99 ms |
30036 KB |
Output is correct |
102 |
Correct |
88 ms |
30028 KB |
Output is correct |
103 |
Correct |
97 ms |
29728 KB |
Output is correct |
104 |
Correct |
86 ms |
29472 KB |
Output is correct |
105 |
Correct |
93 ms |
29732 KB |
Output is correct |
106 |
Correct |
94 ms |
29376 KB |
Output is correct |
107 |
Correct |
103 ms |
29264 KB |
Output is correct |
108 |
Correct |
85 ms |
30032 KB |
Output is correct |
109 |
Correct |
87 ms |
29860 KB |
Output is correct |
110 |
Correct |
96 ms |
29780 KB |
Output is correct |
111 |
Correct |
99 ms |
30032 KB |
Output is correct |