#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Nmax=100010, Nt=(1<<18);
int N, Q, S[Nmax], E[Nmax], L[20][Nmax];
vector<int> V;
class Seg {
public:
Seg() {fill(Tree1+1, Tree1+2*Nt, Nt);}
void Update(int k, int v1, int v2) {Update(1, 1, Nt, k, v1, v2);}
int Query(int l, int r) {return Query(1, 1, Nt, l, r).second;}
private:
int Tree1[2*Nt], Tree2[2*Nt];
void Update(int node, int s, int e, int k, int v1, int v2) {
if(s==e) {
if(Tree1[node]>v1) Tree1[node]=v1, Tree2[node]=v2;
return;
}
int lch=2*node, rch=lch+1, m=(s+e)/2;
if(k<=m) Update(lch, s, m, k, v1, v2);
else Update(rch, m+1, e, k, v1, v2);
if(Tree1[lch]<Tree1[rch]) Tree1[node]=Tree1[lch], Tree2[node]=Tree2[lch];
else Tree1[node]=Tree1[rch], Tree2[node]=Tree2[rch];
}
pair<int, int> Query(int node, int s, int e, int l, int r) {
if(s>r || l>e) return {Nt, 0};
if(l<=s && e<=r) return {Tree1[node], Tree2[node]};
int lch=2*node, rch=lch+1, m=(s+e)/2;
pair<int, int> a=Query(lch, s, m, l, r), b=Query(rch, m+1, e, l, r);
if(a.first<b.first) return a;
return b;
}
}T;
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>Q;
for(int i=1; i<=N; i++) cin>>S[i]>>E[i], V.push_back(S[i]), V.push_back(E[i]);
sort(V.begin(), V.end()), V.erase(unique(V.begin(), V.end()), V.end());
for(int i=1; i<=N; i++) {
S[i]=lower_bound(V.begin(), V.end(), S[i])-V.begin()+1;
E[i]=lower_bound(V.begin(), V.end(), E[i])-V.begin()+1;
T.Update(E[i], S[i], i);
}
for(int i=1; i<=N; i++) L[0][i]=T.Query(S[i], E[i]);
for(int i=1; i<20; i++) for(int j=1; j<=N; j++) L[i][j]=L[i-1][L[i-1][j]];
while(Q--) {
int ans=0;
int s, e; cin>>s>>e;
for(int i=19; i>=0; i--) if(S[L[i][e]]>E[s]) e=L[i][e], ans+=(1<<i);
if(s==e) cout<<ans<<"\n";
else {
if(S[e]>E[s]) e=L[0][e], ans++;
if(S[e]<=E[s] && E[s]<=E[e]) cout<<ans+1<<"\n";
else cout<<"impossible\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
98 ms |
25564 KB |
Output is correct |
3 |
Correct |
120 ms |
25796 KB |
Output is correct |
4 |
Correct |
129 ms |
25544 KB |
Output is correct |
5 |
Correct |
117 ms |
25712 KB |
Output is correct |
6 |
Correct |
138 ms |
29128 KB |
Output is correct |
7 |
Correct |
148 ms |
29168 KB |
Output is correct |
8 |
Correct |
120 ms |
30660 KB |
Output is correct |
9 |
Correct |
114 ms |
30664 KB |
Output is correct |
10 |
Correct |
134 ms |
29124 KB |
Output is correct |
11 |
Correct |
116 ms |
29808 KB |
Output is correct |
12 |
Correct |
57 ms |
28356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
4 ms |
4792 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
4 ms |
4792 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
10 |
Correct |
2 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4488 KB |
Output is correct |
12 |
Correct |
3 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4700 KB |
Output is correct |
19 |
Correct |
26 ms |
7192 KB |
Output is correct |
20 |
Correct |
27 ms |
7256 KB |
Output is correct |
21 |
Correct |
29 ms |
7516 KB |
Output is correct |
22 |
Correct |
22 ms |
7516 KB |
Output is correct |
23 |
Correct |
22 ms |
7516 KB |
Output is correct |
24 |
Correct |
28 ms |
7456 KB |
Output is correct |
25 |
Correct |
25 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
3 ms |
4700 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
4 ms |
4792 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
10 |
Correct |
2 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4500 KB |
Output is correct |
12 |
Correct |
3 ms |
4908 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4888 KB |
Output is correct |
17 |
Correct |
3 ms |
4912 KB |
Output is correct |
18 |
Correct |
3 ms |
4848 KB |
Output is correct |
19 |
Correct |
82 ms |
26968 KB |
Output is correct |
20 |
Correct |
82 ms |
26212 KB |
Output is correct |
21 |
Correct |
94 ms |
26852 KB |
Output is correct |
22 |
Correct |
81 ms |
27588 KB |
Output is correct |
23 |
Correct |
99 ms |
26820 KB |
Output is correct |
24 |
Correct |
104 ms |
28360 KB |
Output is correct |
25 |
Correct |
44 ms |
24776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
25540 KB |
Output is correct |
2 |
Correct |
121 ms |
25472 KB |
Output is correct |
3 |
Correct |
131 ms |
25508 KB |
Output is correct |
4 |
Correct |
97 ms |
27592 KB |
Output is correct |
5 |
Correct |
133 ms |
25972 KB |
Output is correct |
6 |
Correct |
133 ms |
27276 KB |
Output is correct |
7 |
Correct |
132 ms |
30232 KB |
Output is correct |
8 |
Correct |
125 ms |
30564 KB |
Output is correct |
9 |
Correct |
101 ms |
26824 KB |
Output is correct |
10 |
Correct |
131 ms |
29896 KB |
Output is correct |
11 |
Correct |
128 ms |
29456 KB |
Output is correct |
12 |
Correct |
153 ms |
30052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
98 ms |
25564 KB |
Output is correct |
3 |
Correct |
120 ms |
25796 KB |
Output is correct |
4 |
Correct |
129 ms |
25544 KB |
Output is correct |
5 |
Correct |
117 ms |
25712 KB |
Output is correct |
6 |
Correct |
138 ms |
29128 KB |
Output is correct |
7 |
Correct |
148 ms |
29168 KB |
Output is correct |
8 |
Correct |
120 ms |
30660 KB |
Output is correct |
9 |
Correct |
114 ms |
30664 KB |
Output is correct |
10 |
Correct |
134 ms |
29124 KB |
Output is correct |
11 |
Correct |
116 ms |
29808 KB |
Output is correct |
12 |
Correct |
57 ms |
28356 KB |
Output is correct |
13 |
Correct |
3 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
3 ms |
4700 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
3 ms |
4700 KB |
Output is correct |
19 |
Correct |
4 ms |
4792 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
4700 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4488 KB |
Output is correct |
24 |
Correct |
3 ms |
4700 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
4956 KB |
Output is correct |
30 |
Correct |
3 ms |
4700 KB |
Output is correct |
31 |
Correct |
26 ms |
7192 KB |
Output is correct |
32 |
Correct |
27 ms |
7256 KB |
Output is correct |
33 |
Correct |
29 ms |
7516 KB |
Output is correct |
34 |
Correct |
22 ms |
7516 KB |
Output is correct |
35 |
Correct |
22 ms |
7516 KB |
Output is correct |
36 |
Correct |
28 ms |
7456 KB |
Output is correct |
37 |
Correct |
25 ms |
7252 KB |
Output is correct |
38 |
Correct |
2 ms |
4700 KB |
Output is correct |
39 |
Correct |
2 ms |
4500 KB |
Output is correct |
40 |
Correct |
3 ms |
4908 KB |
Output is correct |
41 |
Correct |
3 ms |
4956 KB |
Output is correct |
42 |
Correct |
2 ms |
4700 KB |
Output is correct |
43 |
Correct |
2 ms |
4956 KB |
Output is correct |
44 |
Correct |
3 ms |
4888 KB |
Output is correct |
45 |
Correct |
3 ms |
4912 KB |
Output is correct |
46 |
Correct |
3 ms |
4848 KB |
Output is correct |
47 |
Correct |
82 ms |
26968 KB |
Output is correct |
48 |
Correct |
82 ms |
26212 KB |
Output is correct |
49 |
Correct |
94 ms |
26852 KB |
Output is correct |
50 |
Correct |
81 ms |
27588 KB |
Output is correct |
51 |
Correct |
99 ms |
26820 KB |
Output is correct |
52 |
Correct |
104 ms |
28360 KB |
Output is correct |
53 |
Correct |
44 ms |
24776 KB |
Output is correct |
54 |
Correct |
93 ms |
25540 KB |
Output is correct |
55 |
Correct |
121 ms |
25472 KB |
Output is correct |
56 |
Correct |
131 ms |
25508 KB |
Output is correct |
57 |
Correct |
97 ms |
27592 KB |
Output is correct |
58 |
Correct |
133 ms |
25972 KB |
Output is correct |
59 |
Correct |
133 ms |
27276 KB |
Output is correct |
60 |
Correct |
132 ms |
30232 KB |
Output is correct |
61 |
Correct |
125 ms |
30564 KB |
Output is correct |
62 |
Correct |
101 ms |
26824 KB |
Output is correct |
63 |
Correct |
131 ms |
29896 KB |
Output is correct |
64 |
Correct |
128 ms |
29456 KB |
Output is correct |
65 |
Correct |
153 ms |
30052 KB |
Output is correct |
66 |
Correct |
2 ms |
4580 KB |
Output is correct |
67 |
Correct |
100 ms |
28704 KB |
Output is correct |
68 |
Correct |
121 ms |
28612 KB |
Output is correct |
69 |
Correct |
129 ms |
28788 KB |
Output is correct |
70 |
Correct |
132 ms |
28888 KB |
Output is correct |
71 |
Correct |
124 ms |
29160 KB |
Output is correct |
72 |
Correct |
124 ms |
29200 KB |
Output is correct |
73 |
Correct |
125 ms |
30820 KB |
Output is correct |
74 |
Correct |
114 ms |
30664 KB |
Output is correct |
75 |
Correct |
119 ms |
29024 KB |
Output is correct |
76 |
Correct |
115 ms |
29640 KB |
Output is correct |
77 |
Correct |
60 ms |
28352 KB |
Output is correct |
78 |
Correct |
2 ms |
4700 KB |
Output is correct |
79 |
Correct |
2 ms |
4720 KB |
Output is correct |
80 |
Correct |
3 ms |
4952 KB |
Output is correct |
81 |
Correct |
2 ms |
4700 KB |
Output is correct |
82 |
Correct |
2 ms |
4956 KB |
Output is correct |
83 |
Correct |
3 ms |
4956 KB |
Output is correct |
84 |
Correct |
2 ms |
4952 KB |
Output is correct |
85 |
Correct |
2 ms |
4700 KB |
Output is correct |
86 |
Correct |
25 ms |
7248 KB |
Output is correct |
87 |
Correct |
28 ms |
7232 KB |
Output is correct |
88 |
Correct |
33 ms |
7508 KB |
Output is correct |
89 |
Correct |
22 ms |
7516 KB |
Output is correct |
90 |
Correct |
22 ms |
7512 KB |
Output is correct |
91 |
Correct |
21 ms |
7508 KB |
Output is correct |
92 |
Correct |
23 ms |
7256 KB |
Output is correct |
93 |
Correct |
80 ms |
27024 KB |
Output is correct |
94 |
Correct |
78 ms |
26056 KB |
Output is correct |
95 |
Correct |
99 ms |
26720 KB |
Output is correct |
96 |
Correct |
85 ms |
27588 KB |
Output is correct |
97 |
Correct |
120 ms |
26820 KB |
Output is correct |
98 |
Correct |
98 ms |
28492 KB |
Output is correct |
99 |
Correct |
44 ms |
24776 KB |
Output is correct |
100 |
Correct |
135 ms |
30304 KB |
Output is correct |
101 |
Correct |
132 ms |
30408 KB |
Output is correct |
102 |
Correct |
145 ms |
30556 KB |
Output is correct |
103 |
Correct |
133 ms |
30144 KB |
Output is correct |
104 |
Correct |
118 ms |
29408 KB |
Output is correct |
105 |
Correct |
132 ms |
29900 KB |
Output is correct |
106 |
Correct |
117 ms |
27680 KB |
Output is correct |
107 |
Correct |
122 ms |
28192 KB |
Output is correct |
108 |
Correct |
116 ms |
28860 KB |
Output is correct |
109 |
Correct |
129 ms |
28696 KB |
Output is correct |
110 |
Correct |
136 ms |
30292 KB |
Output is correct |
111 |
Correct |
135 ms |
30232 KB |
Output is correct |