#include<bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix = vector<vector<T>>;
const int INF = 1<<30;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<pii> v(n);
for(pii& i : v){
cin >> i.ff >> i.ss;
i.ff = INF-i.ff;
i.ss = INF-i.ss;
swap(i.ff,i.ss);
}
// cout << "----------\n";
// for(pii i : v)
// cout << i.ff << ' ' << i.ss << '\n';
// cout << "----------\n";
vector<int>o(n);
iota(all(o),0);
sort(all(o),[&](int a, int b){
return v[a] > v[b];
});
vector<int> jmp(n);
vector<int> st;
for(int i : o){
int ini = 0, fim = st.size();
while(ini!=fim){
int m = (ini+fim)>>1;
if(v[st[m]].ff <= v[i].ss)
fim = m;
else ini = m+1;
}
if(ini == st.size()){
jmp[i] = i;
} else jmp[i] = st[ini];
while(!st.empty() && v[st.back()].ss <= v[i].ss)
st.pop_back();
st.push_back(i);
// cout << i << ":\n";
// for(int i : st)
// cout << i << ' ';
// cout << '\n';
}
matrix<int> lift(20,vector<int>(n));
for(int i = 0; i < n; i++){
lift[0][i] = jmp[i];
// cout << jmp[i] << ' ';
}
// cout << '\n';
for(int i = 1; i < 20; i++){
for(int j = 0; j < n; j++){
lift[i][j] = lift[i-1][lift[i-1][j]];
}
}
while(q--){
int a, b;
cin >> a >> b;
a--, b--;
swap(a,b);
if(v[a] > v[b]){
cout << "impossible\n";
continue;
}
if(a == b){
cout << 0 << '\n';
continue;
}
if(v[a].ss >= v[b].ff){
cout << 1 << '\n';
continue;
}
int ct = 2;
for(int i = 19; i >= 0; i--){
if(v[lift[i][a]].ss < v[b].ff)
ct+=1<<i, a = lift[i][a];
}
a = lift[0][a];
if(v[a].ss >= v[b].ff){
cout << ct << '\n';
} else {
cout << "impossible\n";
}
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:46:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if(ini == st.size()){
| ~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
61 ms |
10836 KB |
Output is correct |
3 |
Correct |
78 ms |
13832 KB |
Output is correct |
4 |
Correct |
112 ms |
13788 KB |
Output is correct |
5 |
Correct |
93 ms |
14020 KB |
Output is correct |
6 |
Correct |
83 ms |
13836 KB |
Output is correct |
7 |
Correct |
98 ms |
13872 KB |
Output is correct |
8 |
Correct |
100 ms |
14388 KB |
Output is correct |
9 |
Correct |
101 ms |
14264 KB |
Output is correct |
10 |
Correct |
85 ms |
14264 KB |
Output is correct |
11 |
Incorrect |
92 ms |
14152 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
28 ms |
1308 KB |
Output is correct |
20 |
Correct |
28 ms |
1268 KB |
Output is correct |
21 |
Correct |
30 ms |
1552 KB |
Output is correct |
22 |
Incorrect |
23 ms |
1532 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 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 |
1 ms |
340 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
37 ms |
10588 KB |
Output is correct |
20 |
Correct |
33 ms |
10584 KB |
Output is correct |
21 |
Correct |
35 ms |
10060 KB |
Output is correct |
22 |
Incorrect |
36 ms |
10448 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
10856 KB |
Output is correct |
2 |
Correct |
83 ms |
13880 KB |
Output is correct |
3 |
Correct |
114 ms |
13760 KB |
Output is correct |
4 |
Correct |
94 ms |
14256 KB |
Output is correct |
5 |
Correct |
81 ms |
14148 KB |
Output is correct |
6 |
Correct |
87 ms |
13984 KB |
Output is correct |
7 |
Correct |
80 ms |
14092 KB |
Output is correct |
8 |
Correct |
83 ms |
14148 KB |
Output is correct |
9 |
Correct |
36 ms |
12460 KB |
Output is correct |
10 |
Correct |
80 ms |
13676 KB |
Output is correct |
11 |
Correct |
74 ms |
13376 KB |
Output is correct |
12 |
Correct |
72 ms |
13584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
61 ms |
10836 KB |
Output is correct |
3 |
Correct |
78 ms |
13832 KB |
Output is correct |
4 |
Correct |
112 ms |
13788 KB |
Output is correct |
5 |
Correct |
93 ms |
14020 KB |
Output is correct |
6 |
Correct |
83 ms |
13836 KB |
Output is correct |
7 |
Correct |
98 ms |
13872 KB |
Output is correct |
8 |
Correct |
100 ms |
14388 KB |
Output is correct |
9 |
Correct |
101 ms |
14264 KB |
Output is correct |
10 |
Correct |
85 ms |
14264 KB |
Output is correct |
11 |
Incorrect |
92 ms |
14152 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |