#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
struct interval{
int L, R;
};
int n, qq;
vector<interval> a(maxn+5);
vector<vector<int>> BL(30, vector<int>(2*maxn+5, -1));
template<typename T>
struct RMQ{
vector<vector<T>> table;
int sz, log_sz;
RMQ(vector<T>& a, int n) : sz(n), log_sz(__lg(n)){
table.resize(log_sz+5);
for(int i = 0; i < log_sz+5; i++)
table[i].resize(n+5, {1e9, 1e9});
table[0] = a;
for(int jump = 1; jump <= log_sz; jump++)
for(int i = 0; i+(1<<(jump-1)) < n; i++)
table[jump][i] = min(table[jump-1][i], table[jump-1][i+(1<<(jump-1))]);
}
T get(int l, int r){
if(l > r)
swap(l, r);
int LogN = __lg(r-l+1);
return min(table[LogN][l], table[LogN][r-(1<<(LogN))+1]);
}
};
void compress(){
vector<int> values(2*n), compressed(2*n);
for(int i = 0; i < 2*n; i+= 2){
values[i] = a[i/2].L;
values[i+1] = a[i/2].R;
}
sort(values.begin(), values.end());
int timer = 0;
compressed[0] = timer;
for(int i = 1; i < 2*n; i++){
if(values[i] == values[i-1]){
compressed[i] = timer;
}else{
compressed[i] = ++timer;
}
}
for(int i = 0; i < n; i++){
a[i].L = compressed[lower_bound(values.begin(), values.end(), a[i].L)-values.begin()];
a[i].R = compressed[lower_bound(values.begin(), values.end(), a[i].R)-values.begin()];
}
}
void solve(){
cin >> n >> qq;
for(int i = 0; i < n; i++)
cin >> a[i].L >> a[i].R;
compress();
vector<pair<int, int>> ST_beg(2*maxn+5, {1e9, 1e9});
for(int i = 0; i < n; i++)
ST_beg[a[i].R] = min(make_pair(a[i].L, i), ST_beg[a[i].R]);
RMQ<pair<int, int>> ST(ST_beg, 2*maxn);
for(int i = 0; i < n; i++){
pair<int, int> got = ST.get(a[i].L, a[i].R);
if(got.first < a[i].L)
BL[0][i] = got.second;
}
for(int jump = 1; jump < 30; jump++){
for(int v = 0; v < n; v++){
if(BL[jump-1][v] != -1)
BL[jump][v] = BL[jump-1][BL[jump-1][v]];
}
}
while(qq--){
int s, e;
cin >> s >> e;
s--, e--;
if(s != e && a[s].L == a[e].L && a[s].R == a[e].R){
cout << 1 << "\n";
continue;
}
int k = 0;
for(int jump = 20; jump >= 0; jump--){
if(a[e].L <= a[s].R && a[e].R >= a[s].R)
break;
if(BL[jump][e] != -1 && a[BL[jump][e]].L > a[s].R){
k+= (1<<jump);
e = BL[jump][e];
}
}
if(a[e].L > a[s].R){
k++;
e = BL[0][e];
}
if(a[e].R == a[s].R && a[e].L == a[s].L){
cout << k << "\n";
}else if(a[e].R >= a[s].R && a[e].L <= a[s].R){
cout << k+1 << "\n";
}else{
cout << "impossible\n";
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
// cin >> tt;
while(tt--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
60664 KB |
Output is correct |
2 |
Correct |
118 ms |
62464 KB |
Output is correct |
3 |
Correct |
172 ms |
62092 KB |
Output is correct |
4 |
Correct |
184 ms |
62212 KB |
Output is correct |
5 |
Correct |
156 ms |
62264 KB |
Output is correct |
6 |
Correct |
151 ms |
62308 KB |
Output is correct |
7 |
Correct |
171 ms |
62288 KB |
Output is correct |
8 |
Correct |
152 ms |
62596 KB |
Output is correct |
9 |
Correct |
138 ms |
62776 KB |
Output is correct |
10 |
Correct |
190 ms |
62640 KB |
Output is correct |
11 |
Correct |
183 ms |
62524 KB |
Output is correct |
12 |
Correct |
110 ms |
62664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
60640 KB |
Output is correct |
2 |
Correct |
29 ms |
60612 KB |
Output is correct |
3 |
Correct |
31 ms |
60732 KB |
Output is correct |
4 |
Correct |
31 ms |
60680 KB |
Output is correct |
5 |
Correct |
34 ms |
60660 KB |
Output is correct |
6 |
Correct |
36 ms |
60748 KB |
Output is correct |
7 |
Correct |
39 ms |
60744 KB |
Output is correct |
8 |
Correct |
30 ms |
60712 KB |
Output is correct |
9 |
Correct |
31 ms |
60740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
60640 KB |
Output is correct |
2 |
Correct |
29 ms |
60612 KB |
Output is correct |
3 |
Correct |
31 ms |
60732 KB |
Output is correct |
4 |
Correct |
31 ms |
60680 KB |
Output is correct |
5 |
Correct |
34 ms |
60660 KB |
Output is correct |
6 |
Correct |
36 ms |
60748 KB |
Output is correct |
7 |
Correct |
39 ms |
60744 KB |
Output is correct |
8 |
Correct |
30 ms |
60712 KB |
Output is correct |
9 |
Correct |
31 ms |
60740 KB |
Output is correct |
10 |
Correct |
30 ms |
60596 KB |
Output is correct |
11 |
Correct |
31 ms |
60656 KB |
Output is correct |
12 |
Correct |
30 ms |
60676 KB |
Output is correct |
13 |
Correct |
31 ms |
60656 KB |
Output is correct |
14 |
Correct |
40 ms |
60656 KB |
Output is correct |
15 |
Correct |
34 ms |
60740 KB |
Output is correct |
16 |
Correct |
35 ms |
60740 KB |
Output is correct |
17 |
Correct |
31 ms |
60744 KB |
Output is correct |
18 |
Correct |
31 ms |
60736 KB |
Output is correct |
19 |
Correct |
63 ms |
62140 KB |
Output is correct |
20 |
Correct |
67 ms |
62168 KB |
Output is correct |
21 |
Correct |
74 ms |
62440 KB |
Output is correct |
22 |
Correct |
59 ms |
62408 KB |
Output is correct |
23 |
Correct |
59 ms |
62252 KB |
Output is correct |
24 |
Correct |
64 ms |
62284 KB |
Output is correct |
25 |
Correct |
64 ms |
61956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
60640 KB |
Output is correct |
2 |
Correct |
29 ms |
60612 KB |
Output is correct |
3 |
Correct |
31 ms |
60732 KB |
Output is correct |
4 |
Correct |
31 ms |
60680 KB |
Output is correct |
5 |
Correct |
34 ms |
60660 KB |
Output is correct |
6 |
Correct |
36 ms |
60748 KB |
Output is correct |
7 |
Correct |
39 ms |
60744 KB |
Output is correct |
8 |
Correct |
30 ms |
60712 KB |
Output is correct |
9 |
Correct |
31 ms |
60740 KB |
Output is correct |
10 |
Correct |
32 ms |
60640 KB |
Output is correct |
11 |
Correct |
32 ms |
60708 KB |
Output is correct |
12 |
Correct |
32 ms |
60684 KB |
Output is correct |
13 |
Correct |
32 ms |
60688 KB |
Output is correct |
14 |
Correct |
33 ms |
60648 KB |
Output is correct |
15 |
Correct |
33 ms |
60660 KB |
Output is correct |
16 |
Correct |
32 ms |
60760 KB |
Output is correct |
17 |
Correct |
33 ms |
60652 KB |
Output is correct |
18 |
Correct |
32 ms |
60808 KB |
Output is correct |
19 |
Correct |
98 ms |
61724 KB |
Output is correct |
20 |
Correct |
94 ms |
61728 KB |
Output is correct |
21 |
Correct |
103 ms |
61660 KB |
Output is correct |
22 |
Correct |
103 ms |
61628 KB |
Output is correct |
23 |
Correct |
101 ms |
61652 KB |
Output is correct |
24 |
Correct |
105 ms |
61660 KB |
Output is correct |
25 |
Correct |
58 ms |
61608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
61388 KB |
Output is correct |
2 |
Correct |
141 ms |
61392 KB |
Output is correct |
3 |
Correct |
224 ms |
61400 KB |
Output is correct |
4 |
Correct |
123 ms |
61876 KB |
Output is correct |
5 |
Correct |
188 ms |
61704 KB |
Output is correct |
6 |
Correct |
181 ms |
62372 KB |
Output is correct |
7 |
Correct |
181 ms |
62552 KB |
Output is correct |
8 |
Correct |
172 ms |
62912 KB |
Output is correct |
9 |
Correct |
95 ms |
61580 KB |
Output is correct |
10 |
Correct |
174 ms |
62308 KB |
Output is correct |
11 |
Correct |
135 ms |
61960 KB |
Output is correct |
12 |
Correct |
141 ms |
62240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
60664 KB |
Output is correct |
2 |
Correct |
118 ms |
62464 KB |
Output is correct |
3 |
Correct |
172 ms |
62092 KB |
Output is correct |
4 |
Correct |
184 ms |
62212 KB |
Output is correct |
5 |
Correct |
156 ms |
62264 KB |
Output is correct |
6 |
Correct |
151 ms |
62308 KB |
Output is correct |
7 |
Correct |
171 ms |
62288 KB |
Output is correct |
8 |
Correct |
152 ms |
62596 KB |
Output is correct |
9 |
Correct |
138 ms |
62776 KB |
Output is correct |
10 |
Correct |
190 ms |
62640 KB |
Output is correct |
11 |
Correct |
183 ms |
62524 KB |
Output is correct |
12 |
Correct |
110 ms |
62664 KB |
Output is correct |
13 |
Correct |
34 ms |
60640 KB |
Output is correct |
14 |
Correct |
29 ms |
60612 KB |
Output is correct |
15 |
Correct |
31 ms |
60732 KB |
Output is correct |
16 |
Correct |
31 ms |
60680 KB |
Output is correct |
17 |
Correct |
34 ms |
60660 KB |
Output is correct |
18 |
Correct |
36 ms |
60748 KB |
Output is correct |
19 |
Correct |
39 ms |
60744 KB |
Output is correct |
20 |
Correct |
30 ms |
60712 KB |
Output is correct |
21 |
Correct |
31 ms |
60740 KB |
Output is correct |
22 |
Correct |
30 ms |
60596 KB |
Output is correct |
23 |
Correct |
31 ms |
60656 KB |
Output is correct |
24 |
Correct |
30 ms |
60676 KB |
Output is correct |
25 |
Correct |
31 ms |
60656 KB |
Output is correct |
26 |
Correct |
40 ms |
60656 KB |
Output is correct |
27 |
Correct |
34 ms |
60740 KB |
Output is correct |
28 |
Correct |
35 ms |
60740 KB |
Output is correct |
29 |
Correct |
31 ms |
60744 KB |
Output is correct |
30 |
Correct |
31 ms |
60736 KB |
Output is correct |
31 |
Correct |
63 ms |
62140 KB |
Output is correct |
32 |
Correct |
67 ms |
62168 KB |
Output is correct |
33 |
Correct |
74 ms |
62440 KB |
Output is correct |
34 |
Correct |
59 ms |
62408 KB |
Output is correct |
35 |
Correct |
59 ms |
62252 KB |
Output is correct |
36 |
Correct |
64 ms |
62284 KB |
Output is correct |
37 |
Correct |
64 ms |
61956 KB |
Output is correct |
38 |
Correct |
32 ms |
60640 KB |
Output is correct |
39 |
Correct |
32 ms |
60708 KB |
Output is correct |
40 |
Correct |
32 ms |
60684 KB |
Output is correct |
41 |
Correct |
32 ms |
60688 KB |
Output is correct |
42 |
Correct |
33 ms |
60648 KB |
Output is correct |
43 |
Correct |
33 ms |
60660 KB |
Output is correct |
44 |
Correct |
32 ms |
60760 KB |
Output is correct |
45 |
Correct |
33 ms |
60652 KB |
Output is correct |
46 |
Correct |
32 ms |
60808 KB |
Output is correct |
47 |
Correct |
98 ms |
61724 KB |
Output is correct |
48 |
Correct |
94 ms |
61728 KB |
Output is correct |
49 |
Correct |
103 ms |
61660 KB |
Output is correct |
50 |
Correct |
103 ms |
61628 KB |
Output is correct |
51 |
Correct |
101 ms |
61652 KB |
Output is correct |
52 |
Correct |
105 ms |
61660 KB |
Output is correct |
53 |
Correct |
58 ms |
61608 KB |
Output is correct |
54 |
Correct |
116 ms |
61388 KB |
Output is correct |
55 |
Correct |
141 ms |
61392 KB |
Output is correct |
56 |
Correct |
224 ms |
61400 KB |
Output is correct |
57 |
Correct |
123 ms |
61876 KB |
Output is correct |
58 |
Correct |
188 ms |
61704 KB |
Output is correct |
59 |
Correct |
181 ms |
62372 KB |
Output is correct |
60 |
Correct |
181 ms |
62552 KB |
Output is correct |
61 |
Correct |
172 ms |
62912 KB |
Output is correct |
62 |
Correct |
95 ms |
61580 KB |
Output is correct |
63 |
Correct |
174 ms |
62308 KB |
Output is correct |
64 |
Correct |
135 ms |
61960 KB |
Output is correct |
65 |
Correct |
141 ms |
62240 KB |
Output is correct |
66 |
Correct |
31 ms |
60652 KB |
Output is correct |
67 |
Correct |
134 ms |
63092 KB |
Output is correct |
68 |
Correct |
143 ms |
63044 KB |
Output is correct |
69 |
Correct |
228 ms |
63072 KB |
Output is correct |
70 |
Correct |
157 ms |
63164 KB |
Output is correct |
71 |
Correct |
181 ms |
63144 KB |
Output is correct |
72 |
Correct |
145 ms |
63172 KB |
Output is correct |
73 |
Correct |
120 ms |
63580 KB |
Output is correct |
74 |
Correct |
129 ms |
63520 KB |
Output is correct |
75 |
Correct |
178 ms |
63472 KB |
Output is correct |
76 |
Correct |
153 ms |
63460 KB |
Output is correct |
77 |
Correct |
92 ms |
62760 KB |
Output is correct |
78 |
Correct |
35 ms |
60640 KB |
Output is correct |
79 |
Correct |
33 ms |
60680 KB |
Output is correct |
80 |
Correct |
34 ms |
60740 KB |
Output is correct |
81 |
Correct |
32 ms |
60712 KB |
Output is correct |
82 |
Correct |
33 ms |
60732 KB |
Output is correct |
83 |
Correct |
40 ms |
60632 KB |
Output is correct |
84 |
Correct |
33 ms |
60732 KB |
Output is correct |
85 |
Correct |
35 ms |
60688 KB |
Output is correct |
86 |
Correct |
62 ms |
62168 KB |
Output is correct |
87 |
Correct |
63 ms |
62204 KB |
Output is correct |
88 |
Correct |
70 ms |
62396 KB |
Output is correct |
89 |
Correct |
60 ms |
62396 KB |
Output is correct |
90 |
Correct |
58 ms |
62324 KB |
Output is correct |
91 |
Correct |
56 ms |
62232 KB |
Output is correct |
92 |
Correct |
67 ms |
61964 KB |
Output is correct |
93 |
Correct |
101 ms |
61648 KB |
Output is correct |
94 |
Correct |
93 ms |
61732 KB |
Output is correct |
95 |
Correct |
96 ms |
61692 KB |
Output is correct |
96 |
Correct |
101 ms |
61656 KB |
Output is correct |
97 |
Correct |
100 ms |
61664 KB |
Output is correct |
98 |
Correct |
101 ms |
61772 KB |
Output is correct |
99 |
Correct |
62 ms |
61708 KB |
Output is correct |
100 |
Correct |
177 ms |
63192 KB |
Output is correct |
101 |
Correct |
165 ms |
63192 KB |
Output is correct |
102 |
Correct |
153 ms |
63392 KB |
Output is correct |
103 |
Correct |
139 ms |
62876 KB |
Output is correct |
104 |
Correct |
142 ms |
62708 KB |
Output is correct |
105 |
Correct |
145 ms |
62876 KB |
Output is correct |
106 |
Correct |
158 ms |
62456 KB |
Output is correct |
107 |
Correct |
168 ms |
62492 KB |
Output is correct |
108 |
Correct |
144 ms |
63136 KB |
Output is correct |
109 |
Correct |
134 ms |
63216 KB |
Output is correct |
110 |
Correct |
144 ms |
63364 KB |
Output is correct |
111 |
Correct |
144 ms |
63104 KB |
Output is correct |