#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
#define forn(i,n) for(int i = 0; i < n; ++i)
#define forr(i,n) for(int i = n-1; i >= 0; --i)
#define forbe(i,b,e) for(int i = b; i < e; ++i)
#define all(v) (v).begin(),(v).end()
#define sort(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define pb push_back
#define endl '\n'
const int K = 18;
using ai = array<int,2>;
using aii = array<int,3>;
struct aint {
ai nm;
int n;
vector<ai> f;
aint(int _n, ai _nm) :
n(_n), nm(_nm)
{
f.assign(4*n, nm);
}
void upd(int i, int l, int r, int e, int s, int ind) {
int mid = (l+r)>>1;
if (e < l || r < e) return;
if (l == r) {
if (f[i][0] > s) {
f[i][0] = s;
f[i][1] = ind;
}
return;
}
upd(i<<1, l, mid, e, s , ind);
upd(i<<1|1, mid+1, r, e, s , ind);
ai al = f[i<<1];
ai ar = f[i<<1|1];
f[i] = al;
if (al[0] > ar[0]) f[i] = ar;
}
ai query(int i, int l, int r, int tl, int tr) {
int mid = (l+r)>>1;
if (tr < l || r < tl) return nm;
if (tl <= l && r <= tr) return f[i];
ai al = query(i<<1, l, mid, tl, tr);
ai ar = query(i<<1|1, mid+1, r, tl, tr);
if (al[0] > ar[0]) return ar;
return al;
}
void upd(int s, int e, int i) { upd(1, 0, n-1, e, s, i); }
ai query(int l, int r) { return query(1, 0, n-1, l, r); }
};
void
solve() {
int n, q;
cin >> n >> q;
vi s(n), e(n);
forn(i,n)
cin >> s[i] >> e[i];
vi norm;
map<int, int> normap;
forn(i,n) norm.pb(s[i]);
forn(i,n) norm.pb(e[i]);
sort(norm);
norm.resize(distance(norm.begin(), unique(all(norm))));
forn(i, sz(norm)) normap[norm[i]] = i;
forn(i,n) s[i] = normap[s[i]];
forn(i,n) e[i] = normap[e[i]];
aint tr(sz(norm), {sz(norm)+5,-1});
forn(i, n) tr.upd(s[i], e[i], i);
vector<array<int,K>> p(n);
forn(i, n) p[i][0] = i;
forn(i, n) {
ai qv = tr.query(s[i], e[i]);
p[i][0] = qv[1];
}
forbe(j, 1, K) {
forn(i, n) {
p[i][j] = p[p[i][j-1]][j-1];
}
}
// return;
//
//
// set<int> cords;
// vector<pi> qu;
//
// forn(i, n) { qu.pb({s[i], e[i]}); }
// sort(qu);
//
// int pnt = 0;
//
vector<ai> qs(q);
vi ans(q);
forn(i, q) cin >> qs[i][0] >> qs[i][1];
forn(i, q) --qs[i][0];
forn(i, q) --qs[i][1];
// priority_queue<ai> pq;
forn(i, q) {
if (qs[i][0] == qs[i][1]) {
ans[i] = 0;
continue;
}
if (e[qs[i][0]] > e[qs[i][1]]) {
ans[i] = -1;
continue;
}
if (e[qs[i][0]] >= s[qs[i][1]]) {
ans[i] = 1;
continue;
}
int st = qs[i][0];
int en = qs[i][1];
// cout << "Query " << i << ": " << st << ' ' << en << endl;
int tans = 0;
forr(j, K) {
if (s[p[en][j]] > e[st]) {
tans += (1<<j);
en = p[en][j];
}
}
if (s[p[en][0]] > e[st]) {
ans[i] = -1;
continue;
}
ans[i] = tans+2;
}
forn(i, q) {
if (ans[i] == -1)
cout << "impossible\n";
else
cout << ans[i] << '\n';
}
}
int32_t
main(int argc, char** argv) {
if (argc > 1)
freopen(argv[1],"r", stdin);
solve();
}
Compilation message
events.cpp: In constructor 'aint::aint(int, ai)':
events.cpp:28:9: warning: 'aint::n' will be initialized after [-Wreorder]
28 | int n;
| ^
events.cpp:27:8: warning: 'ai aint::nm' [-Wreorder]
27 | ai nm;
| ^~
events.cpp:31:5: warning: when initialized here [-Wreorder]
31 | aint(int _n, ai _nm) :
| ^~~~
events.cpp: In function 'int32_t main(int, char**)':
events.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
185 | freopen(argv[1],"r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
309 ms |
19044 KB |
Output is correct |
3 |
Correct |
333 ms |
19036 KB |
Output is correct |
4 |
Correct |
362 ms |
19060 KB |
Output is correct |
5 |
Correct |
358 ms |
19956 KB |
Output is correct |
6 |
Correct |
314 ms |
20744 KB |
Output is correct |
7 |
Correct |
370 ms |
20948 KB |
Output is correct |
8 |
Correct |
384 ms |
27380 KB |
Output is correct |
9 |
Correct |
395 ms |
29728 KB |
Output is correct |
10 |
Correct |
372 ms |
21884 KB |
Output is correct |
11 |
Correct |
365 ms |
25128 KB |
Output is correct |
12 |
Correct |
214 ms |
21216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
448 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
444 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
68 ms |
3812 KB |
Output is correct |
20 |
Correct |
65 ms |
3704 KB |
Output is correct |
21 |
Correct |
74 ms |
4044 KB |
Output is correct |
22 |
Correct |
67 ms |
4068 KB |
Output is correct |
23 |
Correct |
71 ms |
4304 KB |
Output is correct |
24 |
Correct |
65 ms |
4340 KB |
Output is correct |
25 |
Correct |
64 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
464 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
444 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
312 KB |
Output is correct |
19 |
Correct |
210 ms |
17380 KB |
Output is correct |
20 |
Correct |
194 ms |
17872 KB |
Output is correct |
21 |
Correct |
212 ms |
18440 KB |
Output is correct |
22 |
Correct |
244 ms |
21312 KB |
Output is correct |
23 |
Correct |
261 ms |
26400 KB |
Output is correct |
24 |
Correct |
272 ms |
26428 KB |
Output is correct |
25 |
Correct |
67 ms |
10244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
19064 KB |
Output is correct |
2 |
Correct |
266 ms |
19064 KB |
Output is correct |
3 |
Correct |
303 ms |
19064 KB |
Output is correct |
4 |
Correct |
304 ms |
27408 KB |
Output is correct |
5 |
Correct |
334 ms |
19488 KB |
Output is correct |
6 |
Correct |
337 ms |
27120 KB |
Output is correct |
7 |
Correct |
328 ms |
26996 KB |
Output is correct |
8 |
Correct |
324 ms |
27152 KB |
Output is correct |
9 |
Correct |
238 ms |
25136 KB |
Output is correct |
10 |
Correct |
315 ms |
26688 KB |
Output is correct |
11 |
Correct |
312 ms |
26508 KB |
Output is correct |
12 |
Correct |
349 ms |
26740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
309 ms |
19044 KB |
Output is correct |
3 |
Correct |
333 ms |
19036 KB |
Output is correct |
4 |
Correct |
362 ms |
19060 KB |
Output is correct |
5 |
Correct |
358 ms |
19956 KB |
Output is correct |
6 |
Correct |
314 ms |
20744 KB |
Output is correct |
7 |
Correct |
370 ms |
20948 KB |
Output is correct |
8 |
Correct |
384 ms |
27380 KB |
Output is correct |
9 |
Correct |
395 ms |
29728 KB |
Output is correct |
10 |
Correct |
372 ms |
21884 KB |
Output is correct |
11 |
Correct |
365 ms |
25128 KB |
Output is correct |
12 |
Correct |
214 ms |
21216 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
464 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
464 KB |
Output is correct |
18 |
Correct |
2 ms |
452 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
3 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
3 ms |
444 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
68 ms |
3812 KB |
Output is correct |
32 |
Correct |
65 ms |
3704 KB |
Output is correct |
33 |
Correct |
74 ms |
4044 KB |
Output is correct |
34 |
Correct |
67 ms |
4068 KB |
Output is correct |
35 |
Correct |
71 ms |
4304 KB |
Output is correct |
36 |
Correct |
65 ms |
4340 KB |
Output is correct |
37 |
Correct |
64 ms |
3948 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
444 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
312 KB |
Output is correct |
47 |
Correct |
210 ms |
17380 KB |
Output is correct |
48 |
Correct |
194 ms |
17872 KB |
Output is correct |
49 |
Correct |
212 ms |
18440 KB |
Output is correct |
50 |
Correct |
244 ms |
21312 KB |
Output is correct |
51 |
Correct |
261 ms |
26400 KB |
Output is correct |
52 |
Correct |
272 ms |
26428 KB |
Output is correct |
53 |
Correct |
67 ms |
10244 KB |
Output is correct |
54 |
Correct |
292 ms |
19064 KB |
Output is correct |
55 |
Correct |
266 ms |
19064 KB |
Output is correct |
56 |
Correct |
303 ms |
19064 KB |
Output is correct |
57 |
Correct |
304 ms |
27408 KB |
Output is correct |
58 |
Correct |
334 ms |
19488 KB |
Output is correct |
59 |
Correct |
337 ms |
27120 KB |
Output is correct |
60 |
Correct |
328 ms |
26996 KB |
Output is correct |
61 |
Correct |
324 ms |
27152 KB |
Output is correct |
62 |
Correct |
238 ms |
25136 KB |
Output is correct |
63 |
Correct |
315 ms |
26688 KB |
Output is correct |
64 |
Correct |
312 ms |
26508 KB |
Output is correct |
65 |
Correct |
349 ms |
26740 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
269 ms |
21532 KB |
Output is correct |
68 |
Correct |
303 ms |
21504 KB |
Output is correct |
69 |
Correct |
281 ms |
21516 KB |
Output is correct |
70 |
Correct |
280 ms |
22424 KB |
Output is correct |
71 |
Correct |
267 ms |
23128 KB |
Output is correct |
72 |
Correct |
290 ms |
23320 KB |
Output is correct |
73 |
Correct |
309 ms |
29852 KB |
Output is correct |
74 |
Correct |
306 ms |
29840 KB |
Output is correct |
75 |
Correct |
264 ms |
21908 KB |
Output is correct |
76 |
Correct |
282 ms |
25156 KB |
Output is correct |
77 |
Correct |
214 ms |
21276 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
448 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
468 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
64 ms |
3832 KB |
Output is correct |
87 |
Correct |
79 ms |
3756 KB |
Output is correct |
88 |
Correct |
67 ms |
4044 KB |
Output is correct |
89 |
Correct |
69 ms |
4084 KB |
Output is correct |
90 |
Correct |
67 ms |
4336 KB |
Output is correct |
91 |
Correct |
66 ms |
4340 KB |
Output is correct |
92 |
Correct |
64 ms |
3948 KB |
Output is correct |
93 |
Correct |
233 ms |
18592 KB |
Output is correct |
94 |
Correct |
194 ms |
17840 KB |
Output is correct |
95 |
Correct |
203 ms |
18568 KB |
Output is correct |
96 |
Correct |
220 ms |
21456 KB |
Output is correct |
97 |
Correct |
237 ms |
26480 KB |
Output is correct |
98 |
Correct |
276 ms |
26464 KB |
Output is correct |
99 |
Correct |
76 ms |
10252 KB |
Output is correct |
100 |
Correct |
370 ms |
29392 KB |
Output is correct |
101 |
Correct |
335 ms |
29436 KB |
Output is correct |
102 |
Correct |
369 ms |
29520 KB |
Output is correct |
103 |
Correct |
339 ms |
29092 KB |
Output is correct |
104 |
Correct |
361 ms |
28724 KB |
Output is correct |
105 |
Correct |
380 ms |
29176 KB |
Output is correct |
106 |
Correct |
253 ms |
18964 KB |
Output is correct |
107 |
Correct |
276 ms |
20948 KB |
Output is correct |
108 |
Correct |
302 ms |
21652 KB |
Output is correct |
109 |
Correct |
298 ms |
21560 KB |
Output is correct |
110 |
Correct |
342 ms |
29340 KB |
Output is correct |
111 |
Correct |
332 ms |
29408 KB |
Output is correct |