//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 4e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, q;
bool invalid[maxN];
pii e[maxN];
int lift[maxN][20];
vector <int> node;
int st[maxN * 4];
void update(int id, int i, int x, int l = 1, int r = node.size()){
if (l == r){
if (e[st[id]].fi > e[x].fi) st[id] = x;
return;
}
int mid = (l + r) >> 1;
if (i <= mid) update(id << 1, i, x, l, mid);
else update(id << 1 | 1, i, x, mid + 1, r);
if (e[st[id << 1]].fi < e[st[id << 1 | 1]].fi) st[id] = st[id << 1];
else st[id] = st[id << 1 | 1];
}
int get(int id, int i, int j, int l = 1, int r = node.size()){
if (i > r || l > j) return 0;
if (i <= l && r <= j) return st[id];
int mid = (l + r) >> 1;
int x = get(id << 1, i, j, l, mid);
int y = get(id << 1 | 1, i, j, mid + 1, r);
if (e[x].fi < e[y].fi) return x;
else return y;
}
void Init(){
cin >> n >> q;
for (int i = 1; i <= n; ++i){
cin >> e[i].fi >> e[i].se;
node.pb(e[i].fi);
node.pb(e[i].se);
}
e[0].fi = INT_MAX;
sort(node.begin(), node.end());
node.resize(unique(node.begin(), node.end()) - node.begin());
for (int i = 1; i <= n; ++i){
e[i].fi = upper_bound(node.begin(), node.end(), e[i].fi) - node.begin();
e[i].se = upper_bound(node.begin(), node.end(), e[i].se) - node.begin();
update(1, e[i].se, i);
}
for (int i = 1; i <= n; ++i){
int u = get(1, e[i].fi, e[i].se);
if (e[u].fi == e[i].fi) continue;
lift[i][0] = u;
}
for (int i = 1; i < 20; ++i){
for (int j = 1; j <= n; ++j){
lift[j][i] = lift[lift[j][i - 1]][i - 1];
}
}
e[0].fi = 0;
while (q--){
int s, t;
cin >> s >> t;
if (s == t){
cout << "0\n"; continue;
}
if (e[t].se < e[s].se){
cout << "impossible\n"; continue;
}
if (e[t].fi <= e[s].se){
cout << "1\n"; continue;
}
int ans = 0;
for (int i = 19; i >= 0; --i){
if (e[lift[t][i]].fi > e[s].se){
t = lift[t][i];
ans += 1 << i;
}
}
t = lift[t][0];
if (t == 0){
cout << "impossible\n"; continue;
}
cout << ans + 2 << "\n";
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
147 ms |
11640 KB |
Output is correct |
3 |
Correct |
145 ms |
11748 KB |
Output is correct |
4 |
Correct |
200 ms |
11740 KB |
Output is correct |
5 |
Correct |
162 ms |
11832 KB |
Output is correct |
6 |
Correct |
151 ms |
11836 KB |
Output is correct |
7 |
Correct |
146 ms |
11772 KB |
Output is correct |
8 |
Correct |
134 ms |
14148 KB |
Output is correct |
9 |
Correct |
126 ms |
16100 KB |
Output is correct |
10 |
Correct |
166 ms |
14904 KB |
Output is correct |
11 |
Correct |
162 ms |
16076 KB |
Output is correct |
12 |
Correct |
87 ms |
14280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
480 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
508 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
35 ms |
1664 KB |
Output is correct |
20 |
Correct |
30 ms |
1624 KB |
Output is correct |
21 |
Correct |
36 ms |
1956 KB |
Output is correct |
22 |
Correct |
25 ms |
1960 KB |
Output is correct |
23 |
Correct |
25 ms |
1828 KB |
Output is correct |
24 |
Correct |
23 ms |
1752 KB |
Output is correct |
25 |
Correct |
33 ms |
1480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
480 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
476 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
105 ms |
11224 KB |
Output is correct |
20 |
Correct |
111 ms |
11076 KB |
Output is correct |
21 |
Correct |
125 ms |
11128 KB |
Output is correct |
22 |
Correct |
118 ms |
12184 KB |
Output is correct |
23 |
Correct |
132 ms |
12852 KB |
Output is correct |
24 |
Correct |
150 ms |
13816 KB |
Output is correct |
25 |
Correct |
39 ms |
11108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
11644 KB |
Output is correct |
2 |
Correct |
138 ms |
11688 KB |
Output is correct |
3 |
Correct |
185 ms |
11644 KB |
Output is correct |
4 |
Correct |
138 ms |
14116 KB |
Output is correct |
5 |
Correct |
182 ms |
14888 KB |
Output is correct |
6 |
Correct |
180 ms |
15752 KB |
Output is correct |
7 |
Correct |
170 ms |
15720 KB |
Output is correct |
8 |
Correct |
161 ms |
15844 KB |
Output is correct |
9 |
Correct |
131 ms |
12808 KB |
Output is correct |
10 |
Correct |
162 ms |
15348 KB |
Output is correct |
11 |
Correct |
166 ms |
14860 KB |
Output is correct |
12 |
Correct |
167 ms |
15312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
147 ms |
11640 KB |
Output is correct |
3 |
Correct |
145 ms |
11748 KB |
Output is correct |
4 |
Correct |
200 ms |
11740 KB |
Output is correct |
5 |
Correct |
162 ms |
11832 KB |
Output is correct |
6 |
Correct |
151 ms |
11836 KB |
Output is correct |
7 |
Correct |
146 ms |
11772 KB |
Output is correct |
8 |
Correct |
134 ms |
14148 KB |
Output is correct |
9 |
Correct |
126 ms |
16100 KB |
Output is correct |
10 |
Correct |
166 ms |
14904 KB |
Output is correct |
11 |
Correct |
162 ms |
16076 KB |
Output is correct |
12 |
Correct |
87 ms |
14280 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
476 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
484 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
480 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
508 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
35 ms |
1664 KB |
Output is correct |
32 |
Correct |
30 ms |
1624 KB |
Output is correct |
33 |
Correct |
36 ms |
1956 KB |
Output is correct |
34 |
Correct |
25 ms |
1960 KB |
Output is correct |
35 |
Correct |
25 ms |
1828 KB |
Output is correct |
36 |
Correct |
23 ms |
1752 KB |
Output is correct |
37 |
Correct |
33 ms |
1480 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
480 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
476 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
472 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
105 ms |
11224 KB |
Output is correct |
48 |
Correct |
111 ms |
11076 KB |
Output is correct |
49 |
Correct |
125 ms |
11128 KB |
Output is correct |
50 |
Correct |
118 ms |
12184 KB |
Output is correct |
51 |
Correct |
132 ms |
12852 KB |
Output is correct |
52 |
Correct |
150 ms |
13816 KB |
Output is correct |
53 |
Correct |
39 ms |
11108 KB |
Output is correct |
54 |
Correct |
133 ms |
11644 KB |
Output is correct |
55 |
Correct |
138 ms |
11688 KB |
Output is correct |
56 |
Correct |
185 ms |
11644 KB |
Output is correct |
57 |
Correct |
138 ms |
14116 KB |
Output is correct |
58 |
Correct |
182 ms |
14888 KB |
Output is correct |
59 |
Correct |
180 ms |
15752 KB |
Output is correct |
60 |
Correct |
170 ms |
15720 KB |
Output is correct |
61 |
Correct |
161 ms |
15844 KB |
Output is correct |
62 |
Correct |
131 ms |
12808 KB |
Output is correct |
63 |
Correct |
162 ms |
15348 KB |
Output is correct |
64 |
Correct |
166 ms |
14860 KB |
Output is correct |
65 |
Correct |
167 ms |
15312 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
131 ms |
14544 KB |
Output is correct |
68 |
Correct |
145 ms |
14556 KB |
Output is correct |
69 |
Correct |
191 ms |
14572 KB |
Output is correct |
70 |
Correct |
153 ms |
14596 KB |
Output is correct |
71 |
Correct |
138 ms |
14672 KB |
Output is correct |
72 |
Correct |
174 ms |
14616 KB |
Output is correct |
73 |
Correct |
150 ms |
16100 KB |
Output is correct |
74 |
Correct |
143 ms |
16076 KB |
Output is correct |
75 |
Correct |
184 ms |
14896 KB |
Output is correct |
76 |
Correct |
152 ms |
16008 KB |
Output is correct |
77 |
Correct |
136 ms |
14232 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
3 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
484 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
352 KB |
Output is correct |
86 |
Correct |
41 ms |
2372 KB |
Output is correct |
87 |
Correct |
48 ms |
2380 KB |
Output is correct |
88 |
Correct |
51 ms |
2604 KB |
Output is correct |
89 |
Correct |
27 ms |
2676 KB |
Output is correct |
90 |
Correct |
24 ms |
2636 KB |
Output is correct |
91 |
Correct |
23 ms |
2592 KB |
Output is correct |
92 |
Correct |
30 ms |
2252 KB |
Output is correct |
93 |
Correct |
119 ms |
12780 KB |
Output is correct |
94 |
Correct |
118 ms |
12028 KB |
Output is correct |
95 |
Correct |
121 ms |
12740 KB |
Output is correct |
96 |
Correct |
128 ms |
13784 KB |
Output is correct |
97 |
Correct |
140 ms |
12844 KB |
Output is correct |
98 |
Correct |
180 ms |
13800 KB |
Output is correct |
99 |
Correct |
40 ms |
11100 KB |
Output is correct |
100 |
Correct |
202 ms |
15688 KB |
Output is correct |
101 |
Correct |
214 ms |
15668 KB |
Output is correct |
102 |
Correct |
214 ms |
15880 KB |
Output is correct |
103 |
Correct |
159 ms |
15316 KB |
Output is correct |
104 |
Correct |
186 ms |
14944 KB |
Output is correct |
105 |
Correct |
197 ms |
15336 KB |
Output is correct |
106 |
Correct |
137 ms |
13936 KB |
Output is correct |
107 |
Correct |
196 ms |
13928 KB |
Output is correct |
108 |
Correct |
177 ms |
14640 KB |
Output is correct |
109 |
Correct |
148 ms |
14704 KB |
Output is correct |
110 |
Correct |
171 ms |
15624 KB |
Output is correct |
111 |
Correct |
186 ms |
15632 KB |
Output is correct |