#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define sz(v) (int)v.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 2e5 + 5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const ll inf = 1e9;
const ld pre = 1e-6;
struct th{
int s, e, id;
bool operator <(const th &b){
return(s < b.s);
}
};
int n, q;
vt<int>comp;
int find(int x){
return(lower_bound(comp.begin(), comp.end(), x) - comp.begin());
}
pii up[mxn + 1][18];
int pref[mxn + 1][17], s[mxn + 1], e[mxn + 1];
pii get(int l, int r){
int lg = __lg(r - l + 1);
return(min(up[l][lg], up[r - (1 << lg) + 1][lg]));
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> s[i] >> e[i];
comp.pb(s[i]); comp.pb(e[i]);
}
sort(comp.begin(), comp.end());
for(int i = 0; i < sz(comp); i++){
up[i][0] = make_pair(inf, 0);
}
for(int i = 1; i <= n; i++){
up[find(e[i])][0] = min(up[find(e[i])][0], make_pair(s[i], i));
}
for(int i = 1; i < 18; i++){
for(int j = 0; j + (1 << i) - 1 < sz(comp); j++){
up[j][i] = min(up[j][i - 1], up[j + (1 << (i - 1))][i - 1]);
}
}
for(int i = 1; i <= n; i++){
pref[i][0] = get(find(s[i]), find(e[i])).se;
}
for(int i = 1; i < 17; i++){
for(int j = 1; j <= n; j++){
pref[j][i] = pref[pref[j][i - 1]][i - 1];
}
}
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r;
if(l == r){
cout << 0 << "\n";
}else if(s[r] <= e[l]){
if(e[r] >= e[l]){
cout << 1 << "\n";
}else{
cout << "impossible" << "\n";
}
}else{
int ans = 0;
for(int i = 16; i >= 0; i--){
int id = pref[r][i];
if(s[id] > e[l]){
ans += (1 << i); r = pref[r][i];
}
}
r = pref[r][0];
if(s[r] <= e[l] && e[r] >= e[l]){
cout << ans + 2 << "\n";
}else{
cout << "impossible" << "\n";
}
}
}
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
126 ms |
37404 KB |
Output is correct |
3 |
Correct |
132 ms |
37392 KB |
Output is correct |
4 |
Correct |
158 ms |
37424 KB |
Output is correct |
5 |
Correct |
141 ms |
37420 KB |
Output is correct |
6 |
Correct |
148 ms |
37432 KB |
Output is correct |
7 |
Correct |
138 ms |
37516 KB |
Output is correct |
8 |
Correct |
129 ms |
37996 KB |
Output is correct |
9 |
Correct |
131 ms |
41036 KB |
Output is correct |
10 |
Correct |
138 ms |
40944 KB |
Output is correct |
11 |
Correct |
136 ms |
40984 KB |
Output is correct |
12 |
Correct |
91 ms |
40224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
26 ms |
2704 KB |
Output is correct |
20 |
Correct |
24 ms |
2692 KB |
Output is correct |
21 |
Correct |
27 ms |
2892 KB |
Output is correct |
22 |
Correct |
22 ms |
3044 KB |
Output is correct |
23 |
Correct |
20 ms |
2868 KB |
Output is correct |
24 |
Correct |
20 ms |
2772 KB |
Output is correct |
25 |
Correct |
26 ms |
2580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
109 ms |
36740 KB |
Output is correct |
20 |
Correct |
104 ms |
36800 KB |
Output is correct |
21 |
Correct |
125 ms |
36740 KB |
Output is correct |
22 |
Correct |
121 ms |
38828 KB |
Output is correct |
23 |
Correct |
115 ms |
38752 KB |
Output is correct |
24 |
Correct |
112 ms |
38784 KB |
Output is correct |
25 |
Correct |
67 ms |
38056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
37408 KB |
Output is correct |
2 |
Correct |
130 ms |
37464 KB |
Output is correct |
3 |
Correct |
164 ms |
37392 KB |
Output is correct |
4 |
Correct |
135 ms |
38000 KB |
Output is correct |
5 |
Correct |
138 ms |
37772 KB |
Output is correct |
6 |
Correct |
145 ms |
37508 KB |
Output is correct |
7 |
Correct |
139 ms |
37516 KB |
Output is correct |
8 |
Correct |
135 ms |
37680 KB |
Output is correct |
9 |
Correct |
115 ms |
36836 KB |
Output is correct |
10 |
Correct |
131 ms |
37256 KB |
Output is correct |
11 |
Correct |
156 ms |
37012 KB |
Output is correct |
12 |
Correct |
132 ms |
37192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
126 ms |
37404 KB |
Output is correct |
3 |
Correct |
132 ms |
37392 KB |
Output is correct |
4 |
Correct |
158 ms |
37424 KB |
Output is correct |
5 |
Correct |
141 ms |
37420 KB |
Output is correct |
6 |
Correct |
148 ms |
37432 KB |
Output is correct |
7 |
Correct |
138 ms |
37516 KB |
Output is correct |
8 |
Correct |
129 ms |
37996 KB |
Output is correct |
9 |
Correct |
131 ms |
41036 KB |
Output is correct |
10 |
Correct |
138 ms |
40944 KB |
Output is correct |
11 |
Correct |
136 ms |
40984 KB |
Output is correct |
12 |
Correct |
91 ms |
40224 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
26 ms |
2704 KB |
Output is correct |
32 |
Correct |
24 ms |
2692 KB |
Output is correct |
33 |
Correct |
27 ms |
2892 KB |
Output is correct |
34 |
Correct |
22 ms |
3044 KB |
Output is correct |
35 |
Correct |
20 ms |
2868 KB |
Output is correct |
36 |
Correct |
20 ms |
2772 KB |
Output is correct |
37 |
Correct |
26 ms |
2580 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
724 KB |
Output is correct |
47 |
Correct |
109 ms |
36740 KB |
Output is correct |
48 |
Correct |
104 ms |
36800 KB |
Output is correct |
49 |
Correct |
125 ms |
36740 KB |
Output is correct |
50 |
Correct |
121 ms |
38828 KB |
Output is correct |
51 |
Correct |
115 ms |
38752 KB |
Output is correct |
52 |
Correct |
112 ms |
38784 KB |
Output is correct |
53 |
Correct |
67 ms |
38056 KB |
Output is correct |
54 |
Correct |
126 ms |
37408 KB |
Output is correct |
55 |
Correct |
130 ms |
37464 KB |
Output is correct |
56 |
Correct |
164 ms |
37392 KB |
Output is correct |
57 |
Correct |
135 ms |
38000 KB |
Output is correct |
58 |
Correct |
138 ms |
37772 KB |
Output is correct |
59 |
Correct |
145 ms |
37508 KB |
Output is correct |
60 |
Correct |
139 ms |
37516 KB |
Output is correct |
61 |
Correct |
135 ms |
37680 KB |
Output is correct |
62 |
Correct |
115 ms |
36836 KB |
Output is correct |
63 |
Correct |
131 ms |
37256 KB |
Output is correct |
64 |
Correct |
156 ms |
37012 KB |
Output is correct |
65 |
Correct |
132 ms |
37192 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
129 ms |
40576 KB |
Output is correct |
68 |
Correct |
138 ms |
40496 KB |
Output is correct |
69 |
Correct |
154 ms |
40568 KB |
Output is correct |
70 |
Correct |
133 ms |
40612 KB |
Output is correct |
71 |
Correct |
153 ms |
40572 KB |
Output is correct |
72 |
Correct |
134 ms |
40608 KB |
Output is correct |
73 |
Correct |
134 ms |
40956 KB |
Output is correct |
74 |
Correct |
142 ms |
40960 KB |
Output is correct |
75 |
Correct |
144 ms |
40848 KB |
Output is correct |
76 |
Correct |
134 ms |
40948 KB |
Output is correct |
77 |
Correct |
93 ms |
40312 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
724 KB |
Output is correct |
80 |
Correct |
1 ms |
736 KB |
Output is correct |
81 |
Correct |
1 ms |
724 KB |
Output is correct |
82 |
Correct |
1 ms |
724 KB |
Output is correct |
83 |
Correct |
1 ms |
724 KB |
Output is correct |
84 |
Correct |
1 ms |
740 KB |
Output is correct |
85 |
Correct |
1 ms |
740 KB |
Output is correct |
86 |
Correct |
27 ms |
3616 KB |
Output is correct |
87 |
Correct |
24 ms |
3660 KB |
Output is correct |
88 |
Correct |
27 ms |
3948 KB |
Output is correct |
89 |
Correct |
22 ms |
4056 KB |
Output is correct |
90 |
Correct |
21 ms |
3828 KB |
Output is correct |
91 |
Correct |
21 ms |
3880 KB |
Output is correct |
92 |
Correct |
31 ms |
3456 KB |
Output is correct |
93 |
Correct |
105 ms |
38704 KB |
Output is correct |
94 |
Correct |
103 ms |
38012 KB |
Output is correct |
95 |
Correct |
111 ms |
38616 KB |
Output is correct |
96 |
Correct |
110 ms |
38700 KB |
Output is correct |
97 |
Correct |
111 ms |
38804 KB |
Output is correct |
98 |
Correct |
107 ms |
38804 KB |
Output is correct |
99 |
Correct |
65 ms |
38016 KB |
Output is correct |
100 |
Correct |
164 ms |
40688 KB |
Output is correct |
101 |
Correct |
132 ms |
40580 KB |
Output is correct |
102 |
Correct |
136 ms |
40804 KB |
Output is correct |
103 |
Correct |
132 ms |
40256 KB |
Output is correct |
104 |
Correct |
128 ms |
40136 KB |
Output is correct |
105 |
Correct |
137 ms |
40260 KB |
Output is correct |
106 |
Correct |
132 ms |
39968 KB |
Output is correct |
107 |
Correct |
143 ms |
39908 KB |
Output is correct |
108 |
Correct |
143 ms |
40592 KB |
Output is correct |
109 |
Correct |
131 ms |
40596 KB |
Output is correct |
110 |
Correct |
156 ms |
40664 KB |
Output is correct |
111 |
Correct |
133 ms |
40604 KB |
Output is correct |