#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
const int maxn = 2e5 + 5;
const int INF = 1e18;
int succ[maxn][18];
struct info{
int l, r, id;
info(){}
info(int _l, int _r, int _id) : l(_l), r(_r), id(_id){}
bool operator < (const info& other) const{
return r < other.r;
}
};
vector<int> val;
int getid(int x){ return lower_bound(val.begin(), val.end(), x) - val.begin();}
pll st[maxn * 4];
pll op(pll a, pll b){
if(a.f < b.f) return a;
return b;
}
void upd(int pos, int val, int id, int v = 1, int l = 0, int r = maxn - 1){
if(l == r){
if(st[v].f > val) st[v] = {val, id};
return;
}
int m = (l + r) / 2;
if(pos <= m) upd(pos, val, id, v * 2, l, m);
else upd(pos, val, id, v * 2 + 1, m + 1, r);
st[v] = op(st[v * 2], st[v * 2 + 1]);
}
pll qry(int l, int r, int v = 1, int L = 0, int R = maxn - 1){
if(l > R || r < L) return {INF, -1};
if(l <= L && r >= R) return st[v];
int m = (L + R) / 2;
return op(qry(l, r, v * 2, L, m), qry(l, r, v * 2 + 1, m + 1, R));
}
signed main(void){
fastio;
int n, q;
cin>>n>>q;
for(int i = 0; i < maxn * 4; i++) st[i] = {INF, -1};
vector<info> e(n), c(n);
for(int i = 0; i < n; i++){
int l, r;
cin>>l>>r;
val.pb(l), val.pb(r);
e[i] = info(l, r, i);
}
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
for(int i = 0; i < n; i++) e[i].l = getid(e[i].l), e[i].r = getid(e[i].r);
c = e;
sort(e.begin(), e.end());
for(int i = 0; i < n; i++){
int t = e[i].r;
int tmp = i;
while(tmp < n && e[tmp].r == t){
upd(e[tmp].r, e[tmp].l, e[tmp].id);
tmp++;
}
while(i < n && e[i].r == t){
succ[e[i].id][0] = qry(e[i].l, e[i].r).s;
if(succ[e[i].id][0] == -1) succ[e[i].id][0] = e[i].id;
i++;
}
i--;
}
for(int j = 1; j < 18; j++){
for(int i = 0; i < n; i++) succ[i][j] = succ[succ[i][j - 1]][j - 1];
}
while(q--){
int s, t;
cin>>s>>t;
s--, t--;
if(s == t){
cout<<0<<"\n";
continue;
}
if(c[s].r >= c[t].l && c[s].r <= c[t].r){
cout<<1<<"\n";
continue;
}
int ans = 0;
for(int i = 17; i >= 0; i--){
if(c[succ[t][i]].l > c[s].r) t = succ[t][i], ans += (1 << i);
}
if(c[succ[t][0]].l <= c[s].r && c[succ[t][0]].r >= c[s].r) cout<<ans + 2<<"\n";
else{
cout<<"impossible\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14940 KB |
Output is correct |
2 |
Correct |
126 ms |
36924 KB |
Output is correct |
3 |
Correct |
121 ms |
37340 KB |
Output is correct |
4 |
Correct |
156 ms |
37344 KB |
Output is correct |
5 |
Correct |
127 ms |
37408 KB |
Output is correct |
6 |
Correct |
135 ms |
37324 KB |
Output is correct |
7 |
Correct |
124 ms |
37336 KB |
Output is correct |
8 |
Correct |
116 ms |
37656 KB |
Output is correct |
9 |
Correct |
116 ms |
37648 KB |
Output is correct |
10 |
Correct |
134 ms |
37628 KB |
Output is correct |
11 |
Correct |
127 ms |
37700 KB |
Output is correct |
12 |
Correct |
77 ms |
36948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
15012 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
15012 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
15000 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
15088 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
15076 KB |
Output is correct |
19 |
Correct |
31 ms |
16760 KB |
Output is correct |
20 |
Correct |
32 ms |
16884 KB |
Output is correct |
21 |
Correct |
33 ms |
17396 KB |
Output is correct |
22 |
Correct |
25 ms |
17132 KB |
Output is correct |
23 |
Correct |
26 ms |
16988 KB |
Output is correct |
24 |
Correct |
25 ms |
16980 KB |
Output is correct |
25 |
Correct |
27 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
15012 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14936 KB |
Output is correct |
11 |
Correct |
2 ms |
14936 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
15008 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
93 ms |
35740 KB |
Output is correct |
20 |
Correct |
92 ms |
34760 KB |
Output is correct |
21 |
Correct |
116 ms |
35532 KB |
Output is correct |
22 |
Correct |
94 ms |
35512 KB |
Output is correct |
23 |
Correct |
110 ms |
35448 KB |
Output is correct |
24 |
Correct |
122 ms |
35604 KB |
Output is correct |
25 |
Correct |
44 ms |
34760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
37068 KB |
Output is correct |
2 |
Correct |
123 ms |
37340 KB |
Output is correct |
3 |
Correct |
143 ms |
37832 KB |
Output is correct |
4 |
Correct |
128 ms |
37764 KB |
Output is correct |
5 |
Correct |
150 ms |
37656 KB |
Output is correct |
6 |
Correct |
150 ms |
37408 KB |
Output is correct |
7 |
Correct |
144 ms |
37320 KB |
Output is correct |
8 |
Correct |
135 ms |
37596 KB |
Output is correct |
9 |
Correct |
108 ms |
35640 KB |
Output is correct |
10 |
Correct |
138 ms |
37324 KB |
Output is correct |
11 |
Correct |
159 ms |
36892 KB |
Output is correct |
12 |
Correct |
138 ms |
37116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14940 KB |
Output is correct |
2 |
Correct |
126 ms |
36924 KB |
Output is correct |
3 |
Correct |
121 ms |
37340 KB |
Output is correct |
4 |
Correct |
156 ms |
37344 KB |
Output is correct |
5 |
Correct |
127 ms |
37408 KB |
Output is correct |
6 |
Correct |
135 ms |
37324 KB |
Output is correct |
7 |
Correct |
124 ms |
37336 KB |
Output is correct |
8 |
Correct |
116 ms |
37656 KB |
Output is correct |
9 |
Correct |
116 ms |
37648 KB |
Output is correct |
10 |
Correct |
134 ms |
37628 KB |
Output is correct |
11 |
Correct |
127 ms |
37700 KB |
Output is correct |
12 |
Correct |
77 ms |
36948 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
15012 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14936 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
15000 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
15088 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 ms |
15076 KB |
Output is correct |
31 |
Correct |
31 ms |
16760 KB |
Output is correct |
32 |
Correct |
32 ms |
16884 KB |
Output is correct |
33 |
Correct |
33 ms |
17396 KB |
Output is correct |
34 |
Correct |
25 ms |
17132 KB |
Output is correct |
35 |
Correct |
26 ms |
16988 KB |
Output is correct |
36 |
Correct |
25 ms |
16980 KB |
Output is correct |
37 |
Correct |
27 ms |
16728 KB |
Output is correct |
38 |
Correct |
2 ms |
14936 KB |
Output is correct |
39 |
Correct |
2 ms |
14936 KB |
Output is correct |
40 |
Correct |
3 ms |
14940 KB |
Output is correct |
41 |
Correct |
4 ms |
15008 KB |
Output is correct |
42 |
Correct |
3 ms |
14940 KB |
Output is correct |
43 |
Correct |
3 ms |
14940 KB |
Output is correct |
44 |
Correct |
3 ms |
14940 KB |
Output is correct |
45 |
Correct |
3 ms |
14940 KB |
Output is correct |
46 |
Correct |
3 ms |
14940 KB |
Output is correct |
47 |
Correct |
93 ms |
35740 KB |
Output is correct |
48 |
Correct |
92 ms |
34760 KB |
Output is correct |
49 |
Correct |
116 ms |
35532 KB |
Output is correct |
50 |
Correct |
94 ms |
35512 KB |
Output is correct |
51 |
Correct |
110 ms |
35448 KB |
Output is correct |
52 |
Correct |
122 ms |
35604 KB |
Output is correct |
53 |
Correct |
44 ms |
34760 KB |
Output is correct |
54 |
Correct |
114 ms |
37068 KB |
Output is correct |
55 |
Correct |
123 ms |
37340 KB |
Output is correct |
56 |
Correct |
143 ms |
37832 KB |
Output is correct |
57 |
Correct |
128 ms |
37764 KB |
Output is correct |
58 |
Correct |
150 ms |
37656 KB |
Output is correct |
59 |
Correct |
150 ms |
37408 KB |
Output is correct |
60 |
Correct |
144 ms |
37320 KB |
Output is correct |
61 |
Correct |
135 ms |
37596 KB |
Output is correct |
62 |
Correct |
108 ms |
35640 KB |
Output is correct |
63 |
Correct |
138 ms |
37324 KB |
Output is correct |
64 |
Correct |
159 ms |
36892 KB |
Output is correct |
65 |
Correct |
138 ms |
37116 KB |
Output is correct |
66 |
Correct |
2 ms |
14940 KB |
Output is correct |
67 |
Correct |
123 ms |
37604 KB |
Output is correct |
68 |
Correct |
120 ms |
37324 KB |
Output is correct |
69 |
Correct |
151 ms |
37324 KB |
Output is correct |
70 |
Correct |
124 ms |
37352 KB |
Output is correct |
71 |
Correct |
126 ms |
37472 KB |
Output is correct |
72 |
Correct |
124 ms |
37320 KB |
Output is correct |
73 |
Correct |
127 ms |
37832 KB |
Output is correct |
74 |
Correct |
117 ms |
37864 KB |
Output is correct |
75 |
Correct |
145 ms |
37580 KB |
Output is correct |
76 |
Correct |
136 ms |
37668 KB |
Output is correct |
77 |
Correct |
82 ms |
37064 KB |
Output is correct |
78 |
Correct |
2 ms |
14932 KB |
Output is correct |
79 |
Correct |
3 ms |
14940 KB |
Output is correct |
80 |
Correct |
3 ms |
14936 KB |
Output is correct |
81 |
Correct |
3 ms |
15192 KB |
Output is correct |
82 |
Correct |
3 ms |
14940 KB |
Output is correct |
83 |
Correct |
3 ms |
14940 KB |
Output is correct |
84 |
Correct |
4 ms |
15076 KB |
Output is correct |
85 |
Correct |
3 ms |
14940 KB |
Output is correct |
86 |
Correct |
33 ms |
16708 KB |
Output is correct |
87 |
Correct |
29 ms |
16980 KB |
Output is correct |
88 |
Correct |
32 ms |
16976 KB |
Output is correct |
89 |
Correct |
26 ms |
17232 KB |
Output is correct |
90 |
Correct |
26 ms |
16984 KB |
Output is correct |
91 |
Correct |
31 ms |
17008 KB |
Output is correct |
92 |
Correct |
27 ms |
16724 KB |
Output is correct |
93 |
Correct |
99 ms |
35672 KB |
Output is correct |
94 |
Correct |
111 ms |
34864 KB |
Output is correct |
95 |
Correct |
103 ms |
35532 KB |
Output is correct |
96 |
Correct |
97 ms |
35672 KB |
Output is correct |
97 |
Correct |
110 ms |
35532 KB |
Output is correct |
98 |
Correct |
110 ms |
35528 KB |
Output is correct |
99 |
Correct |
45 ms |
35000 KB |
Output is correct |
100 |
Correct |
149 ms |
37576 KB |
Output is correct |
101 |
Correct |
149 ms |
37360 KB |
Output is correct |
102 |
Correct |
154 ms |
37652 KB |
Output is correct |
103 |
Correct |
146 ms |
37360 KB |
Output is correct |
104 |
Correct |
129 ms |
36808 KB |
Output is correct |
105 |
Correct |
137 ms |
37040 KB |
Output is correct |
106 |
Correct |
148 ms |
36788 KB |
Output is correct |
107 |
Correct |
143 ms |
36844 KB |
Output is correct |
108 |
Correct |
126 ms |
37516 KB |
Output is correct |
109 |
Correct |
149 ms |
37404 KB |
Output is correct |
110 |
Correct |
145 ms |
37408 KB |
Output is correct |
111 |
Correct |
145 ms |
37320 KB |
Output is correct |