#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 |
173 ms |
39800 KB |
Output is correct |
3 |
Correct |
192 ms |
39852 KB |
Output is correct |
4 |
Correct |
228 ms |
39836 KB |
Output is correct |
5 |
Correct |
183 ms |
39988 KB |
Output is correct |
6 |
Correct |
179 ms |
40136 KB |
Output is correct |
7 |
Correct |
180 ms |
40120 KB |
Output is correct |
8 |
Correct |
217 ms |
41060 KB |
Output is correct |
9 |
Correct |
176 ms |
40996 KB |
Output is correct |
10 |
Correct |
204 ms |
38944 KB |
Output is correct |
11 |
Correct |
183 ms |
40140 KB |
Output is correct |
12 |
Correct |
105 ms |
33596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
4 ms |
5296 KB |
Output is correct |
5 |
Correct |
3 ms |
5300 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
3 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 |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
4 ms |
5296 KB |
Output is correct |
5 |
Correct |
3 ms |
5300 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5332 KB |
Output is correct |
13 |
Correct |
3 ms |
5300 KB |
Output is correct |
14 |
Correct |
5 ms |
5296 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 |
5300 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
31 ms |
7960 KB |
Output is correct |
20 |
Correct |
29 ms |
8076 KB |
Output is correct |
21 |
Correct |
31 ms |
8392 KB |
Output is correct |
22 |
Correct |
26 ms |
8112 KB |
Output is correct |
23 |
Correct |
26 ms |
8396 KB |
Output is correct |
24 |
Correct |
25 ms |
8292 KB |
Output is correct |
25 |
Correct |
27 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5028 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
4 ms |
5296 KB |
Output is correct |
5 |
Correct |
3 ms |
5300 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5332 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5296 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
3 ms |
5292 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
154 ms |
38076 KB |
Output is correct |
20 |
Correct |
151 ms |
37204 KB |
Output is correct |
21 |
Correct |
154 ms |
33004 KB |
Output is correct |
22 |
Correct |
173 ms |
37972 KB |
Output is correct |
23 |
Correct |
165 ms |
37704 KB |
Output is correct |
24 |
Correct |
197 ms |
42348 KB |
Output is correct |
25 |
Correct |
47 ms |
24708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
39892 KB |
Output is correct |
2 |
Correct |
175 ms |
39772 KB |
Output is correct |
3 |
Correct |
185 ms |
39764 KB |
Output is correct |
4 |
Correct |
180 ms |
41116 KB |
Output is correct |
5 |
Correct |
206 ms |
38956 KB |
Output is correct |
6 |
Correct |
219 ms |
44236 KB |
Output is correct |
7 |
Correct |
227 ms |
44240 KB |
Output is correct |
8 |
Correct |
224 ms |
42880 KB |
Output is correct |
9 |
Correct |
166 ms |
37708 KB |
Output is correct |
10 |
Correct |
228 ms |
43908 KB |
Output is correct |
11 |
Correct |
215 ms |
42548 KB |
Output is correct |
12 |
Correct |
239 ms |
43912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
173 ms |
39800 KB |
Output is correct |
3 |
Correct |
192 ms |
39852 KB |
Output is correct |
4 |
Correct |
228 ms |
39836 KB |
Output is correct |
5 |
Correct |
183 ms |
39988 KB |
Output is correct |
6 |
Correct |
179 ms |
40136 KB |
Output is correct |
7 |
Correct |
180 ms |
40120 KB |
Output is correct |
8 |
Correct |
217 ms |
41060 KB |
Output is correct |
9 |
Correct |
176 ms |
40996 KB |
Output is correct |
10 |
Correct |
204 ms |
38944 KB |
Output is correct |
11 |
Correct |
183 ms |
40140 KB |
Output is correct |
12 |
Correct |
105 ms |
33596 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
2 ms |
5028 KB |
Output is correct |
15 |
Correct |
4 ms |
5332 KB |
Output is correct |
16 |
Correct |
4 ms |
5296 KB |
Output is correct |
17 |
Correct |
3 ms |
5300 KB |
Output is correct |
18 |
Correct |
3 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5332 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5032 KB |
Output is correct |
23 |
Correct |
2 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5332 KB |
Output is correct |
25 |
Correct |
3 ms |
5300 KB |
Output is correct |
26 |
Correct |
5 ms |
5296 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 |
5300 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
31 ms |
7960 KB |
Output is correct |
32 |
Correct |
29 ms |
8076 KB |
Output is correct |
33 |
Correct |
31 ms |
8392 KB |
Output is correct |
34 |
Correct |
26 ms |
8112 KB |
Output is correct |
35 |
Correct |
26 ms |
8396 KB |
Output is correct |
36 |
Correct |
25 ms |
8292 KB |
Output is correct |
37 |
Correct |
27 ms |
8140 KB |
Output is correct |
38 |
Correct |
2 ms |
5076 KB |
Output is correct |
39 |
Correct |
2 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5296 KB |
Output is correct |
41 |
Correct |
3 ms |
5332 KB |
Output is correct |
42 |
Correct |
3 ms |
5292 KB |
Output is correct |
43 |
Correct |
3 ms |
5332 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
3 ms |
5332 KB |
Output is correct |
46 |
Correct |
3 ms |
5204 KB |
Output is correct |
47 |
Correct |
154 ms |
38076 KB |
Output is correct |
48 |
Correct |
151 ms |
37204 KB |
Output is correct |
49 |
Correct |
154 ms |
33004 KB |
Output is correct |
50 |
Correct |
173 ms |
37972 KB |
Output is correct |
51 |
Correct |
165 ms |
37704 KB |
Output is correct |
52 |
Correct |
197 ms |
42348 KB |
Output is correct |
53 |
Correct |
47 ms |
24708 KB |
Output is correct |
54 |
Correct |
214 ms |
39892 KB |
Output is correct |
55 |
Correct |
175 ms |
39772 KB |
Output is correct |
56 |
Correct |
185 ms |
39764 KB |
Output is correct |
57 |
Correct |
180 ms |
41116 KB |
Output is correct |
58 |
Correct |
206 ms |
38956 KB |
Output is correct |
59 |
Correct |
219 ms |
44236 KB |
Output is correct |
60 |
Correct |
227 ms |
44240 KB |
Output is correct |
61 |
Correct |
224 ms |
42880 KB |
Output is correct |
62 |
Correct |
166 ms |
37708 KB |
Output is correct |
63 |
Correct |
228 ms |
43908 KB |
Output is correct |
64 |
Correct |
215 ms |
42548 KB |
Output is correct |
65 |
Correct |
239 ms |
43912 KB |
Output is correct |
66 |
Correct |
3 ms |
5076 KB |
Output is correct |
67 |
Correct |
182 ms |
39868 KB |
Output is correct |
68 |
Correct |
193 ms |
39756 KB |
Output is correct |
69 |
Correct |
186 ms |
39736 KB |
Output is correct |
70 |
Correct |
220 ms |
40024 KB |
Output is correct |
71 |
Correct |
183 ms |
40132 KB |
Output is correct |
72 |
Correct |
178 ms |
40236 KB |
Output is correct |
73 |
Correct |
176 ms |
41264 KB |
Output is correct |
74 |
Correct |
172 ms |
41092 KB |
Output is correct |
75 |
Correct |
176 ms |
38900 KB |
Output is correct |
76 |
Correct |
180 ms |
40096 KB |
Output is correct |
77 |
Correct |
100 ms |
33580 KB |
Output is correct |
78 |
Correct |
3 ms |
5112 KB |
Output is correct |
79 |
Correct |
3 ms |
5332 KB |
Output is correct |
80 |
Correct |
5 ms |
5296 KB |
Output is correct |
81 |
Correct |
3 ms |
5332 KB |
Output is correct |
82 |
Correct |
4 ms |
5332 KB |
Output is correct |
83 |
Correct |
3 ms |
5332 KB |
Output is correct |
84 |
Correct |
3 ms |
5372 KB |
Output is correct |
85 |
Correct |
3 ms |
5172 KB |
Output is correct |
86 |
Correct |
31 ms |
8024 KB |
Output is correct |
87 |
Correct |
28 ms |
8096 KB |
Output is correct |
88 |
Correct |
31 ms |
8272 KB |
Output is correct |
89 |
Correct |
26 ms |
8112 KB |
Output is correct |
90 |
Correct |
25 ms |
8316 KB |
Output is correct |
91 |
Correct |
25 ms |
8296 KB |
Output is correct |
92 |
Correct |
27 ms |
8148 KB |
Output is correct |
93 |
Correct |
154 ms |
38120 KB |
Output is correct |
94 |
Correct |
155 ms |
37312 KB |
Output is correct |
95 |
Correct |
153 ms |
33100 KB |
Output is correct |
96 |
Correct |
164 ms |
37796 KB |
Output is correct |
97 |
Correct |
194 ms |
37660 KB |
Output is correct |
98 |
Correct |
199 ms |
42276 KB |
Output is correct |
99 |
Correct |
41 ms |
24652 KB |
Output is correct |
100 |
Correct |
231 ms |
44280 KB |
Output is correct |
101 |
Correct |
242 ms |
44280 KB |
Output is correct |
102 |
Correct |
214 ms |
42884 KB |
Output is correct |
103 |
Correct |
226 ms |
43916 KB |
Output is correct |
104 |
Correct |
246 ms |
42496 KB |
Output is correct |
105 |
Correct |
238 ms |
43824 KB |
Output is correct |
106 |
Correct |
187 ms |
36240 KB |
Output is correct |
107 |
Correct |
190 ms |
39280 KB |
Output is correct |
108 |
Correct |
173 ms |
35020 KB |
Output is correct |
109 |
Correct |
173 ms |
35060 KB |
Output is correct |
110 |
Correct |
247 ms |
41600 KB |
Output is correct |
111 |
Correct |
228 ms |
41508 KB |
Output is correct |