#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops")
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int INF = 1e9, MAXN = 100010;
int n, q;
vector<ti> events; // end, start, index
int st[20][MAXN], order[MAXN];
pi old[MAXN];
struct node { // thanks rs
int s,e,m;
pi v;
node *l,*r;
node(int S,int E){
s=S,e=E,m=(s+e)>>1;
v=pi(INF,0);
l=r=0;
}
void update(int x,pi nval) {
if(s==e) {
v = min(v, nval);
return;
}
if(x>m) cr(), r->update(x,nval);
else cl(), l->update(x,nval);
v=min(l?l->v:pi(INF,0),r?r->v:pi(INF,0));
}
pi rmq(int x,int y) {
if(s==x&&e==y) return v;
if(x>m)return r?r->rmq(x,y):pi(INF,0);
else if(y<=m)return l?l->rmq(x,y):pi(INF,0);
else return min(l?l->rmq(x,m):pi(INF,0),r?r->rmq(m+1,y):pi(INF,0));
}
void cr() { if(!r) r=new node(m+1,e); }
void cl() { if(!l) l=new node(s,m); }
} *seg;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
memset(st, -1, sizeof st);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
int a, b; cin >> a >> b;
events.pb({b, a, i});
old[i] = {a, b};
}
sort(events.begin(), events.end());
seg = new node(1, (int)1e9);
int cnt = 0;
for (auto [e, s, id]: events) {
order[id] = ++cnt;
pi qq = seg->rmq(s, e);
if (qq.fi < s) {
st[0][id] = qq.si;
}
seg->update(e, pi(s, id));
}
for (int i = 1; i < 20; ++i) {
for (int j = 1; j <= n; ++j) {
st[i][j] = st[i - 1][st[i - 1][j]];
}
}
auto check = [&](int i, int j) {
return (old[j].fi <= old[i].si && old[i].si <= old[j].si);
};
while (q--) {
int s, e; cin >> s >> e;
if (s == e) {
cout << 0 << '\n';
continue;
}
if (check(s, e)) {
cout << 1 << '\n';
continue;
}
if (old[s].si > old[e].si) {
cout << "impossible\n";
continue;
}
int node = e, ans = 0;
for (int i = 19; i >= 0; --i) {
int pa = st[i][node];
if (pa == -1 || old[pa].si <= old[s].si || old[pa].fi <= old[s].fi) continue;
node = pa;
ans += (1 << i);
}
if (check(s, node)) {
cout << ans + 1 << '\n';
} else if (check(s, st[0][node])) {
cout << ans + 2 << '\n';
} else cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8160 KB |
Output is correct |
2 |
Correct |
161 ms |
77824 KB |
Output is correct |
3 |
Correct |
177 ms |
79872 KB |
Output is correct |
4 |
Correct |
257 ms |
81096 KB |
Output is correct |
5 |
Correct |
184 ms |
82116 KB |
Output is correct |
6 |
Correct |
189 ms |
81720 KB |
Output is correct |
7 |
Correct |
223 ms |
79236 KB |
Output is correct |
8 |
Correct |
148 ms |
76220 KB |
Output is correct |
9 |
Correct |
173 ms |
84484 KB |
Output is correct |
10 |
Correct |
185 ms |
76876 KB |
Output is correct |
11 |
Correct |
188 ms |
76152 KB |
Output is correct |
12 |
Correct |
93 ms |
23328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8168 KB |
Output is correct |
2 |
Correct |
4 ms |
8164 KB |
Output is correct |
3 |
Correct |
6 ms |
9044 KB |
Output is correct |
4 |
Correct |
5 ms |
9172 KB |
Output is correct |
5 |
Correct |
5 ms |
8276 KB |
Output is correct |
6 |
Correct |
5 ms |
8660 KB |
Output is correct |
7 |
Correct |
8 ms |
9080 KB |
Output is correct |
8 |
Correct |
6 ms |
9072 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8168 KB |
Output is correct |
2 |
Correct |
4 ms |
8164 KB |
Output is correct |
3 |
Correct |
6 ms |
9044 KB |
Output is correct |
4 |
Correct |
5 ms |
9172 KB |
Output is correct |
5 |
Correct |
5 ms |
8276 KB |
Output is correct |
6 |
Correct |
5 ms |
8660 KB |
Output is correct |
7 |
Correct |
8 ms |
9080 KB |
Output is correct |
8 |
Correct |
6 ms |
9072 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
3 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
6 ms |
9072 KB |
Output is correct |
13 |
Correct |
6 ms |
9172 KB |
Output is correct |
14 |
Correct |
4 ms |
8276 KB |
Output is correct |
15 |
Correct |
5 ms |
8660 KB |
Output is correct |
16 |
Correct |
7 ms |
9172 KB |
Output is correct |
17 |
Correct |
6 ms |
9072 KB |
Output is correct |
18 |
Correct |
4 ms |
8176 KB |
Output is correct |
19 |
Correct |
39 ms |
10188 KB |
Output is correct |
20 |
Correct |
51 ms |
13804 KB |
Output is correct |
21 |
Correct |
36 ms |
10452 KB |
Output is correct |
22 |
Correct |
31 ms |
12240 KB |
Output is correct |
23 |
Correct |
32 ms |
14192 KB |
Output is correct |
24 |
Correct |
40 ms |
14088 KB |
Output is correct |
25 |
Correct |
37 ms |
14108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8168 KB |
Output is correct |
2 |
Correct |
4 ms |
8164 KB |
Output is correct |
3 |
Correct |
6 ms |
9044 KB |
Output is correct |
4 |
Correct |
5 ms |
9172 KB |
Output is correct |
5 |
Correct |
5 ms |
8276 KB |
Output is correct |
6 |
Correct |
5 ms |
8660 KB |
Output is correct |
7 |
Correct |
8 ms |
9080 KB |
Output is correct |
8 |
Correct |
6 ms |
9072 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
3 ms |
8164 KB |
Output is correct |
12 |
Correct |
5 ms |
9044 KB |
Output is correct |
13 |
Correct |
6 ms |
9172 KB |
Output is correct |
14 |
Correct |
4 ms |
8276 KB |
Output is correct |
15 |
Correct |
5 ms |
8688 KB |
Output is correct |
16 |
Correct |
5 ms |
9172 KB |
Output is correct |
17 |
Correct |
5 ms |
9044 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
150 ms |
76684 KB |
Output is correct |
20 |
Correct |
78 ms |
20956 KB |
Output is correct |
21 |
Correct |
133 ms |
44728 KB |
Output is correct |
22 |
Correct |
127 ms |
70344 KB |
Output is correct |
23 |
Correct |
138 ms |
75336 KB |
Output is correct |
24 |
Correct |
162 ms |
82216 KB |
Output is correct |
25 |
Correct |
66 ms |
11824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
77716 KB |
Output is correct |
2 |
Correct |
182 ms |
79856 KB |
Output is correct |
3 |
Correct |
218 ms |
80940 KB |
Output is correct |
4 |
Correct |
159 ms |
84164 KB |
Output is correct |
5 |
Correct |
212 ms |
76704 KB |
Output is correct |
6 |
Correct |
191 ms |
84080 KB |
Output is correct |
7 |
Correct |
203 ms |
83900 KB |
Output is correct |
8 |
Correct |
182 ms |
82104 KB |
Output is correct |
9 |
Correct |
138 ms |
75460 KB |
Output is correct |
10 |
Correct |
187 ms |
83612 KB |
Output is correct |
11 |
Correct |
196 ms |
81792 KB |
Output is correct |
12 |
Correct |
209 ms |
83616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8160 KB |
Output is correct |
2 |
Correct |
161 ms |
77824 KB |
Output is correct |
3 |
Correct |
177 ms |
79872 KB |
Output is correct |
4 |
Correct |
257 ms |
81096 KB |
Output is correct |
5 |
Correct |
184 ms |
82116 KB |
Output is correct |
6 |
Correct |
189 ms |
81720 KB |
Output is correct |
7 |
Correct |
223 ms |
79236 KB |
Output is correct |
8 |
Correct |
148 ms |
76220 KB |
Output is correct |
9 |
Correct |
173 ms |
84484 KB |
Output is correct |
10 |
Correct |
185 ms |
76876 KB |
Output is correct |
11 |
Correct |
188 ms |
76152 KB |
Output is correct |
12 |
Correct |
93 ms |
23328 KB |
Output is correct |
13 |
Correct |
5 ms |
8168 KB |
Output is correct |
14 |
Correct |
4 ms |
8164 KB |
Output is correct |
15 |
Correct |
6 ms |
9044 KB |
Output is correct |
16 |
Correct |
5 ms |
9172 KB |
Output is correct |
17 |
Correct |
5 ms |
8276 KB |
Output is correct |
18 |
Correct |
5 ms |
8660 KB |
Output is correct |
19 |
Correct |
8 ms |
9080 KB |
Output is correct |
20 |
Correct |
6 ms |
9072 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
3 ms |
8148 KB |
Output is correct |
23 |
Correct |
4 ms |
8148 KB |
Output is correct |
24 |
Correct |
6 ms |
9072 KB |
Output is correct |
25 |
Correct |
6 ms |
9172 KB |
Output is correct |
26 |
Correct |
4 ms |
8276 KB |
Output is correct |
27 |
Correct |
5 ms |
8660 KB |
Output is correct |
28 |
Correct |
7 ms |
9172 KB |
Output is correct |
29 |
Correct |
6 ms |
9072 KB |
Output is correct |
30 |
Correct |
4 ms |
8176 KB |
Output is correct |
31 |
Correct |
39 ms |
10188 KB |
Output is correct |
32 |
Correct |
51 ms |
13804 KB |
Output is correct |
33 |
Correct |
36 ms |
10452 KB |
Output is correct |
34 |
Correct |
31 ms |
12240 KB |
Output is correct |
35 |
Correct |
32 ms |
14192 KB |
Output is correct |
36 |
Correct |
40 ms |
14088 KB |
Output is correct |
37 |
Correct |
37 ms |
14108 KB |
Output is correct |
38 |
Correct |
4 ms |
8148 KB |
Output is correct |
39 |
Correct |
3 ms |
8164 KB |
Output is correct |
40 |
Correct |
5 ms |
9044 KB |
Output is correct |
41 |
Correct |
6 ms |
9172 KB |
Output is correct |
42 |
Correct |
4 ms |
8276 KB |
Output is correct |
43 |
Correct |
5 ms |
8688 KB |
Output is correct |
44 |
Correct |
5 ms |
9172 KB |
Output is correct |
45 |
Correct |
5 ms |
9044 KB |
Output is correct |
46 |
Correct |
4 ms |
8148 KB |
Output is correct |
47 |
Correct |
150 ms |
76684 KB |
Output is correct |
48 |
Correct |
78 ms |
20956 KB |
Output is correct |
49 |
Correct |
133 ms |
44728 KB |
Output is correct |
50 |
Correct |
127 ms |
70344 KB |
Output is correct |
51 |
Correct |
138 ms |
75336 KB |
Output is correct |
52 |
Correct |
162 ms |
82216 KB |
Output is correct |
53 |
Correct |
66 ms |
11824 KB |
Output is correct |
54 |
Correct |
158 ms |
77716 KB |
Output is correct |
55 |
Correct |
182 ms |
79856 KB |
Output is correct |
56 |
Correct |
218 ms |
80940 KB |
Output is correct |
57 |
Correct |
159 ms |
84164 KB |
Output is correct |
58 |
Correct |
212 ms |
76704 KB |
Output is correct |
59 |
Correct |
191 ms |
84080 KB |
Output is correct |
60 |
Correct |
203 ms |
83900 KB |
Output is correct |
61 |
Correct |
182 ms |
82104 KB |
Output is correct |
62 |
Correct |
138 ms |
75460 KB |
Output is correct |
63 |
Correct |
187 ms |
83612 KB |
Output is correct |
64 |
Correct |
196 ms |
81792 KB |
Output is correct |
65 |
Correct |
209 ms |
83616 KB |
Output is correct |
66 |
Correct |
3 ms |
8148 KB |
Output is correct |
67 |
Correct |
153 ms |
77812 KB |
Output is correct |
68 |
Correct |
179 ms |
79852 KB |
Output is correct |
69 |
Correct |
252 ms |
81296 KB |
Output is correct |
70 |
Correct |
182 ms |
81984 KB |
Output is correct |
71 |
Correct |
189 ms |
81600 KB |
Output is correct |
72 |
Correct |
206 ms |
79160 KB |
Output is correct |
73 |
Correct |
149 ms |
76248 KB |
Output is correct |
74 |
Correct |
175 ms |
84448 KB |
Output is correct |
75 |
Correct |
187 ms |
76856 KB |
Output is correct |
76 |
Correct |
176 ms |
76176 KB |
Output is correct |
77 |
Correct |
92 ms |
23256 KB |
Output is correct |
78 |
Correct |
3 ms |
8148 KB |
Output is correct |
79 |
Correct |
5 ms |
9044 KB |
Output is correct |
80 |
Correct |
5 ms |
9172 KB |
Output is correct |
81 |
Correct |
5 ms |
8276 KB |
Output is correct |
82 |
Correct |
5 ms |
8660 KB |
Output is correct |
83 |
Correct |
5 ms |
9172 KB |
Output is correct |
84 |
Correct |
5 ms |
9044 KB |
Output is correct |
85 |
Correct |
4 ms |
8148 KB |
Output is correct |
86 |
Correct |
36 ms |
10188 KB |
Output is correct |
87 |
Correct |
39 ms |
13812 KB |
Output is correct |
88 |
Correct |
36 ms |
10484 KB |
Output is correct |
89 |
Correct |
30 ms |
12236 KB |
Output is correct |
90 |
Correct |
31 ms |
14268 KB |
Output is correct |
91 |
Correct |
31 ms |
14048 KB |
Output is correct |
92 |
Correct |
34 ms |
14108 KB |
Output is correct |
93 |
Correct |
136 ms |
76728 KB |
Output is correct |
94 |
Correct |
76 ms |
20924 KB |
Output is correct |
95 |
Correct |
134 ms |
44716 KB |
Output is correct |
96 |
Correct |
131 ms |
70424 KB |
Output is correct |
97 |
Correct |
164 ms |
75364 KB |
Output is correct |
98 |
Correct |
142 ms |
82196 KB |
Output is correct |
99 |
Correct |
58 ms |
11868 KB |
Output is correct |
100 |
Correct |
202 ms |
84188 KB |
Output is correct |
101 |
Correct |
180 ms |
84040 KB |
Output is correct |
102 |
Correct |
181 ms |
82164 KB |
Output is correct |
103 |
Correct |
194 ms |
83776 KB |
Output is correct |
104 |
Correct |
180 ms |
81832 KB |
Output is correct |
105 |
Correct |
210 ms |
83740 KB |
Output is correct |
106 |
Correct |
129 ms |
20568 KB |
Output is correct |
107 |
Correct |
131 ms |
22940 KB |
Output is correct |
108 |
Correct |
165 ms |
46832 KB |
Output is correct |
109 |
Correct |
165 ms |
46808 KB |
Output is correct |
110 |
Correct |
208 ms |
80608 KB |
Output is correct |
111 |
Correct |
226 ms |
80684 KB |
Output is correct |