//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 = 1e5 + 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] = i;
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];
}
}
for (int i = 1; i <= n; ++i) cerr << lift[i][0] << "\n";
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 'void Init()':
events.cpp:62:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
62 | if (e[u].fi == e[i].fi) continue; lift[i][0] = i;
| ^~
events.cpp:62:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
62 | if (e[u].fi == e[i].fi) continue; lift[i][0] = i;
| ^~~~
events.cpp: In function 'int main()':
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".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
352 ms |
15348 KB |
Output is correct |
3 |
Correct |
358 ms |
15172 KB |
Output is correct |
4 |
Correct |
385 ms |
15076 KB |
Output is correct |
5 |
Correct |
362 ms |
15196 KB |
Output is correct |
6 |
Correct |
355 ms |
15172 KB |
Output is correct |
7 |
Correct |
359 ms |
15172 KB |
Output is correct |
8 |
Runtime error |
43 ms |
10204 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
3 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 |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
3 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 |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
5 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
484 KB |
Output is correct |
16 |
Correct |
4 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
372 KB |
Output is correct |
18 |
Correct |
3 ms |
352 KB |
Output is correct |
19 |
Correct |
43 ms |
2340 KB |
Output is correct |
20 |
Correct |
46 ms |
2480 KB |
Output is correct |
21 |
Correct |
42 ms |
2636 KB |
Output is correct |
22 |
Correct |
36 ms |
2732 KB |
Output is correct |
23 |
Correct |
35 ms |
2676 KB |
Output is correct |
24 |
Correct |
33 ms |
2644 KB |
Output is correct |
25 |
Correct |
39 ms |
2284 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 |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
3 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 |
3 ms |
480 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
4 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
468 KB |
Output is correct |
18 |
Correct |
3 ms |
352 KB |
Output is correct |
19 |
Correct |
326 ms |
13380 KB |
Output is correct |
20 |
Correct |
324 ms |
12604 KB |
Output is correct |
21 |
Correct |
338 ms |
13104 KB |
Output is correct |
22 |
Incorrect |
326 ms |
13768 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
357 ms |
15168 KB |
Output is correct |
2 |
Correct |
350 ms |
15088 KB |
Output is correct |
3 |
Correct |
380 ms |
15112 KB |
Output is correct |
4 |
Runtime error |
57 ms |
10224 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
352 ms |
15348 KB |
Output is correct |
3 |
Correct |
358 ms |
15172 KB |
Output is correct |
4 |
Correct |
385 ms |
15076 KB |
Output is correct |
5 |
Correct |
362 ms |
15196 KB |
Output is correct |
6 |
Correct |
355 ms |
15172 KB |
Output is correct |
7 |
Correct |
359 ms |
15172 KB |
Output is correct |
8 |
Runtime error |
43 ms |
10204 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |