#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define int long long
using namespace std;
const int N = 2e5 + 11;
struct event{
int s, e, i;
};
event a[N];
int p[N], R[N];
int lift[N][20];
int l2[N][20];
int n, q;
struct SegTree{
int t[N << 2];
void update(int p, int val, int v = 0, int l = 0, int r = n * 2){
if(l == r) t[v] = min(t[v], val);
else {
int m = (l + r) / 2;
if(p <= m) update(p, val, v * 2 + 1, l, m);
else update(p, val, v * 2 + 2, m + 1, r);
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
}
int query_suff_min(int p, int v = 0, int l = 0, int r = n * 2){
if(l == r) return t[v];
int m = (l + r) / 2;
if(p <= m) return min(query_suff_min(p, v * 2 + 1, l, m), t[v * 2 + 2]);
else return query_suff_min(p, v * 2 + 2, m + 1, r);
}
} ST[20];
struct query{
int u, v, t;
};
int ans[N];
int32_t main(){
for(int i = 0; i < 20; i++)
fill(ST[i].t, ST[i].t + (N << 2), 1 << 30);
cin >> n >> q;
for(int i = 1; i <= n; i++){
int s, e; cin >> s >> e; a[i] = {s, e, i};
}
{
set<int> S; map<int, int> mp;
for(int i = 1; i <= n; i++) S.insert(a[i].s), S.insert(a[i].e);
int id = 1; for(auto i : S) mp[i] = id++;
for(int i = 1; i <= n; i++) a[i].s = mp[a[i].s], a[i].e = mp[a[i].e];
}
sort(a + 1, a + n + 1, [](event u, event v){ return u.e < v.e; });
R[n] = n; for(int i = n - 1; i >= 1; i--) R[i] = a[i].e == a[i + 1].e ? R[i + 1] : i;
for(int i = 1; i <= n; i++) p[a[i].i] = i;
vector<query> Q;
for(int i = 0; i < q; i++){
int u, v; cin >> u >> v; u = p[u], v = p[v];
Q.push_back({u, v, i});
}
sort(Q.begin(), Q.end(), [](query a, query b){ return a.v < b.v; }); int qi = 0;
for(int i = 1; i <= n; i++){
for(int j = R[i - 1] + 1; j <= R[i]; j++) ST[0].update(a[j].e, a[j].s);
lift[a[i].s][0] = ST[0].query_suff_min(a[i].s);
for(int j = 1; j < 20; j++) {
ST[j - 1].update(a[i].s, lift[a[i].s][j - 1]);
lift[a[i].s][j] = ST[j - 1].query_suff_min(lift[a[i].s][j - 1]);
}
while(qi < Q.size() && Q[qi].v == i){
int u = Q[qi].u, v = Q[qi].v, t = Q[qi].t;
if(a[u].e > a[v].e) ans[t] = -1;
else if(u == v) ans[t] = 0;
else if(a[v].s <= a[u].e && a[u].e <= a[v].e) ans[t] = 1;
else {
ans[t] = 2;
int x = a[v].s;
for(int j = 19; j >= 0; j--){
if(a[u].e < lift[x][j]) ans[t] += (1 << j), x = lift[x][j];
}
}
qi++;
}
}
for(int i = 0; i < q; i++){
cout << (ans[i] == -1 || ans[i] > N ? "impossible" : to_string(ans[i])) << '\n';
}
}
Compilation message
events.cpp: In function 'int32_t main()':
events.cpp:74:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while(qi < Q.size() && Q[qi].v == i){
| ~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
125540 KB |
Output is correct |
2 |
Correct |
482 ms |
157568 KB |
Output is correct |
3 |
Correct |
560 ms |
157448 KB |
Output is correct |
4 |
Correct |
505 ms |
157412 KB |
Output is correct |
5 |
Correct |
472 ms |
160204 KB |
Output is correct |
6 |
Correct |
480 ms |
162628 KB |
Output is correct |
7 |
Correct |
484 ms |
163360 KB |
Output is correct |
8 |
Correct |
481 ms |
184704 KB |
Output is correct |
9 |
Correct |
483 ms |
184636 KB |
Output is correct |
10 |
Correct |
531 ms |
157736 KB |
Output is correct |
11 |
Correct |
499 ms |
168348 KB |
Output is correct |
12 |
Correct |
352 ms |
157948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
125548 KB |
Output is correct |
2 |
Correct |
44 ms |
125492 KB |
Output is correct |
3 |
Correct |
47 ms |
125860 KB |
Output is correct |
4 |
Correct |
47 ms |
125804 KB |
Output is correct |
5 |
Correct |
46 ms |
125820 KB |
Output is correct |
6 |
Correct |
46 ms |
125752 KB |
Output is correct |
7 |
Correct |
47 ms |
126116 KB |
Output is correct |
8 |
Correct |
46 ms |
125896 KB |
Output is correct |
9 |
Correct |
44 ms |
125516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
125548 KB |
Output is correct |
2 |
Correct |
44 ms |
125492 KB |
Output is correct |
3 |
Correct |
47 ms |
125860 KB |
Output is correct |
4 |
Correct |
47 ms |
125804 KB |
Output is correct |
5 |
Correct |
46 ms |
125820 KB |
Output is correct |
6 |
Correct |
46 ms |
125752 KB |
Output is correct |
7 |
Correct |
47 ms |
126116 KB |
Output is correct |
8 |
Correct |
46 ms |
125896 KB |
Output is correct |
9 |
Correct |
44 ms |
125516 KB |
Output is correct |
10 |
Correct |
50 ms |
125516 KB |
Output is correct |
11 |
Correct |
45 ms |
125488 KB |
Output is correct |
12 |
Correct |
46 ms |
125828 KB |
Output is correct |
13 |
Correct |
45 ms |
125840 KB |
Output is correct |
14 |
Correct |
53 ms |
125872 KB |
Output is correct |
15 |
Correct |
46 ms |
125816 KB |
Output is correct |
16 |
Correct |
46 ms |
126028 KB |
Output is correct |
17 |
Correct |
48 ms |
125936 KB |
Output is correct |
18 |
Correct |
46 ms |
125524 KB |
Output is correct |
19 |
Correct |
127 ms |
130644 KB |
Output is correct |
20 |
Correct |
110 ms |
130700 KB |
Output is correct |
21 |
Correct |
114 ms |
131008 KB |
Output is correct |
22 |
Correct |
107 ms |
131000 KB |
Output is correct |
23 |
Correct |
133 ms |
133124 KB |
Output is correct |
24 |
Correct |
107 ms |
132384 KB |
Output is correct |
25 |
Correct |
108 ms |
132880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
125548 KB |
Output is correct |
2 |
Correct |
44 ms |
125492 KB |
Output is correct |
3 |
Correct |
47 ms |
125860 KB |
Output is correct |
4 |
Correct |
47 ms |
125804 KB |
Output is correct |
5 |
Correct |
46 ms |
125820 KB |
Output is correct |
6 |
Correct |
46 ms |
125752 KB |
Output is correct |
7 |
Correct |
47 ms |
126116 KB |
Output is correct |
8 |
Correct |
46 ms |
125896 KB |
Output is correct |
9 |
Correct |
44 ms |
125516 KB |
Output is correct |
10 |
Correct |
47 ms |
125536 KB |
Output is correct |
11 |
Correct |
44 ms |
125556 KB |
Output is correct |
12 |
Correct |
51 ms |
125856 KB |
Output is correct |
13 |
Correct |
46 ms |
125796 KB |
Output is correct |
14 |
Correct |
46 ms |
125880 KB |
Output is correct |
15 |
Correct |
46 ms |
125852 KB |
Output is correct |
16 |
Correct |
48 ms |
126136 KB |
Output is correct |
17 |
Correct |
48 ms |
125900 KB |
Output is correct |
18 |
Correct |
45 ms |
125596 KB |
Output is correct |
19 |
Correct |
440 ms |
155992 KB |
Output is correct |
20 |
Correct |
405 ms |
156032 KB |
Output is correct |
21 |
Correct |
424 ms |
156104 KB |
Output is correct |
22 |
Correct |
446 ms |
165852 KB |
Output is correct |
23 |
Correct |
470 ms |
166984 KB |
Output is correct |
24 |
Correct |
505 ms |
182612 KB |
Output is correct |
25 |
Correct |
275 ms |
129484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
482 ms |
157432 KB |
Output is correct |
2 |
Correct |
505 ms |
157476 KB |
Output is correct |
3 |
Correct |
502 ms |
157360 KB |
Output is correct |
4 |
Correct |
483 ms |
184520 KB |
Output is correct |
5 |
Correct |
493 ms |
157772 KB |
Output is correct |
6 |
Correct |
648 ms |
184236 KB |
Output is correct |
7 |
Correct |
613 ms |
184124 KB |
Output is correct |
8 |
Correct |
565 ms |
184260 KB |
Output is correct |
9 |
Correct |
485 ms |
166964 KB |
Output is correct |
10 |
Correct |
567 ms |
183784 KB |
Output is correct |
11 |
Correct |
528 ms |
179676 KB |
Output is correct |
12 |
Correct |
579 ms |
183772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
125540 KB |
Output is correct |
2 |
Correct |
482 ms |
157568 KB |
Output is correct |
3 |
Correct |
560 ms |
157448 KB |
Output is correct |
4 |
Correct |
505 ms |
157412 KB |
Output is correct |
5 |
Correct |
472 ms |
160204 KB |
Output is correct |
6 |
Correct |
480 ms |
162628 KB |
Output is correct |
7 |
Correct |
484 ms |
163360 KB |
Output is correct |
8 |
Correct |
481 ms |
184704 KB |
Output is correct |
9 |
Correct |
483 ms |
184636 KB |
Output is correct |
10 |
Correct |
531 ms |
157736 KB |
Output is correct |
11 |
Correct |
499 ms |
168348 KB |
Output is correct |
12 |
Correct |
352 ms |
157948 KB |
Output is correct |
13 |
Correct |
50 ms |
125548 KB |
Output is correct |
14 |
Correct |
44 ms |
125492 KB |
Output is correct |
15 |
Correct |
47 ms |
125860 KB |
Output is correct |
16 |
Correct |
47 ms |
125804 KB |
Output is correct |
17 |
Correct |
46 ms |
125820 KB |
Output is correct |
18 |
Correct |
46 ms |
125752 KB |
Output is correct |
19 |
Correct |
47 ms |
126116 KB |
Output is correct |
20 |
Correct |
46 ms |
125896 KB |
Output is correct |
21 |
Correct |
44 ms |
125516 KB |
Output is correct |
22 |
Correct |
50 ms |
125516 KB |
Output is correct |
23 |
Correct |
45 ms |
125488 KB |
Output is correct |
24 |
Correct |
46 ms |
125828 KB |
Output is correct |
25 |
Correct |
45 ms |
125840 KB |
Output is correct |
26 |
Correct |
53 ms |
125872 KB |
Output is correct |
27 |
Correct |
46 ms |
125816 KB |
Output is correct |
28 |
Correct |
46 ms |
126028 KB |
Output is correct |
29 |
Correct |
48 ms |
125936 KB |
Output is correct |
30 |
Correct |
46 ms |
125524 KB |
Output is correct |
31 |
Correct |
127 ms |
130644 KB |
Output is correct |
32 |
Correct |
110 ms |
130700 KB |
Output is correct |
33 |
Correct |
114 ms |
131008 KB |
Output is correct |
34 |
Correct |
107 ms |
131000 KB |
Output is correct |
35 |
Correct |
133 ms |
133124 KB |
Output is correct |
36 |
Correct |
107 ms |
132384 KB |
Output is correct |
37 |
Correct |
108 ms |
132880 KB |
Output is correct |
38 |
Correct |
47 ms |
125536 KB |
Output is correct |
39 |
Correct |
44 ms |
125556 KB |
Output is correct |
40 |
Correct |
51 ms |
125856 KB |
Output is correct |
41 |
Correct |
46 ms |
125796 KB |
Output is correct |
42 |
Correct |
46 ms |
125880 KB |
Output is correct |
43 |
Correct |
46 ms |
125852 KB |
Output is correct |
44 |
Correct |
48 ms |
126136 KB |
Output is correct |
45 |
Correct |
48 ms |
125900 KB |
Output is correct |
46 |
Correct |
45 ms |
125596 KB |
Output is correct |
47 |
Correct |
440 ms |
155992 KB |
Output is correct |
48 |
Correct |
405 ms |
156032 KB |
Output is correct |
49 |
Correct |
424 ms |
156104 KB |
Output is correct |
50 |
Correct |
446 ms |
165852 KB |
Output is correct |
51 |
Correct |
470 ms |
166984 KB |
Output is correct |
52 |
Correct |
505 ms |
182612 KB |
Output is correct |
53 |
Correct |
275 ms |
129484 KB |
Output is correct |
54 |
Correct |
482 ms |
157432 KB |
Output is correct |
55 |
Correct |
505 ms |
157476 KB |
Output is correct |
56 |
Correct |
502 ms |
157360 KB |
Output is correct |
57 |
Correct |
483 ms |
184520 KB |
Output is correct |
58 |
Correct |
493 ms |
157772 KB |
Output is correct |
59 |
Correct |
648 ms |
184236 KB |
Output is correct |
60 |
Correct |
613 ms |
184124 KB |
Output is correct |
61 |
Correct |
565 ms |
184260 KB |
Output is correct |
62 |
Correct |
485 ms |
166964 KB |
Output is correct |
63 |
Correct |
567 ms |
183784 KB |
Output is correct |
64 |
Correct |
528 ms |
179676 KB |
Output is correct |
65 |
Correct |
579 ms |
183772 KB |
Output is correct |
66 |
Correct |
46 ms |
125508 KB |
Output is correct |
67 |
Correct |
480 ms |
160536 KB |
Output is correct |
68 |
Correct |
483 ms |
160616 KB |
Output is correct |
69 |
Correct |
527 ms |
160452 KB |
Output is correct |
70 |
Correct |
480 ms |
163240 KB |
Output is correct |
71 |
Correct |
480 ms |
165868 KB |
Output is correct |
72 |
Correct |
501 ms |
166640 KB |
Output is correct |
73 |
Correct |
508 ms |
187712 KB |
Output is correct |
74 |
Correct |
472 ms |
187596 KB |
Output is correct |
75 |
Correct |
519 ms |
160876 KB |
Output is correct |
76 |
Correct |
502 ms |
171480 KB |
Output is correct |
77 |
Correct |
388 ms |
160324 KB |
Output is correct |
78 |
Correct |
48 ms |
125560 KB |
Output is correct |
79 |
Correct |
49 ms |
125816 KB |
Output is correct |
80 |
Correct |
46 ms |
125888 KB |
Output is correct |
81 |
Correct |
46 ms |
125860 KB |
Output is correct |
82 |
Correct |
47 ms |
125828 KB |
Output is correct |
83 |
Correct |
48 ms |
126080 KB |
Output is correct |
84 |
Correct |
46 ms |
125896 KB |
Output is correct |
85 |
Correct |
45 ms |
125556 KB |
Output is correct |
86 |
Correct |
109 ms |
131640 KB |
Output is correct |
87 |
Correct |
131 ms |
131700 KB |
Output is correct |
88 |
Correct |
111 ms |
131908 KB |
Output is correct |
89 |
Correct |
105 ms |
132056 KB |
Output is correct |
90 |
Correct |
111 ms |
133192 KB |
Output is correct |
91 |
Correct |
127 ms |
132456 KB |
Output is correct |
92 |
Correct |
108 ms |
132796 KB |
Output is correct |
93 |
Correct |
428 ms |
157948 KB |
Output is correct |
94 |
Correct |
429 ms |
157268 KB |
Output is correct |
95 |
Correct |
475 ms |
157980 KB |
Output is correct |
96 |
Correct |
427 ms |
167732 KB |
Output is correct |
97 |
Correct |
452 ms |
168872 KB |
Output is correct |
98 |
Correct |
517 ms |
184500 KB |
Output is correct |
99 |
Correct |
305 ms |
130756 KB |
Output is correct |
100 |
Correct |
605 ms |
187248 KB |
Output is correct |
101 |
Correct |
621 ms |
187220 KB |
Output is correct |
102 |
Correct |
613 ms |
187352 KB |
Output is correct |
103 |
Correct |
559 ms |
186824 KB |
Output is correct |
104 |
Correct |
586 ms |
182876 KB |
Output is correct |
105 |
Correct |
569 ms |
186988 KB |
Output is correct |
106 |
Correct |
504 ms |
153272 KB |
Output is correct |
107 |
Correct |
562 ms |
159816 KB |
Output is correct |
108 |
Correct |
503 ms |
160712 KB |
Output is correct |
109 |
Correct |
498 ms |
160832 KB |
Output is correct |
110 |
Correct |
912 ms |
186048 KB |
Output is correct |
111 |
Correct |
848 ms |
186064 KB |
Output is correct |