This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n, q;
int s[100100];
int e[100100];
int p[100100];
int par[100100];
int nxt[100100][20];
int cmp(int i, int j) {
return e[i] < e[j];
}
int get_par(int v) {
if(par[v] == v) {
return v;
}
return par[v] = get_par(par[v]);
}
int calc(int x, int y) {
int ans = 0;
for(int i = 17; i >= 0; i--) {
int v = nxt[x][i];
if(v > 0 && e[v] < e[y]) {
ans += 1<<i;
x = v;
}
}
if(e[x] >= s[y]) {
return ans;
} else {
return -1;
}
}
int go(int x, int k) {
for(int i = 17; i >= 0; i--) {
int v = nxt[x][i];
if(k >= (1<<i)) {
k -= 1<<i;
if(v == -1) return -1;
x = v;
}
}
return x;
}
int can(int x, int y, int k) {
int v = get_par(x);
int cnt = 0;
int c = nxt[v][0];
if(v == x || s[c] <= e[x]) {
cnt++;
x = c;
}
if(k - cnt - 1 <= 0) {
return 0;
}
int t = go(x, k - cnt - 1);
if(t == -1) {
return 0;
}
if(e[t] >= s[y]) return 1;
return 0;
}
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> e[i];
p[i] = i;
}
sort(p+1, p + n + 1, cmp);
vector<int> st;
for(int i = 1; i <= n; i++) {
int j = p[i];
while(st.size() > 0 && s[st.back()] >= s[j]) {
par[st.back()] = j;
st.pop_back();
}
if(st.size() > 0 && e[st.back()] == e[j] && s[st.back()] < s[j]) {
par[j] = st.back();
} else {
par[j] = j;
st.push_back(j);
}
}
for(int i = 0; i < st.size(); i++) {
if(i + 1 == st.size()) {
nxt[st[i]][0] = -1;
} else {
if(e[st[i]] >= s[st[i+1]]) {
nxt[st[i]][0] = st[i + 1];
}
else {
nxt[st[i]][0] = -1;
}
}
}
for(int i = 1; i <= n; i++) {
if(par[i] != i) {
nxt[i][0] = get_par(i);
}
}
for(int k = 1; k < 18; k++) {
for(int i = 1; i <= n; i++) {
int v = nxt[i][k-1];
if(v != -1) {
nxt[i][k] = nxt[v][k-1];
} else {
nxt[i][k] = -1;
}
}
}
for(int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
if(e[x] > e[y]) {
swap(x, y);
}
if(e[x] >= s[y]) {
cout << 0 << "\n";
} else if(get_par(x) == get_par(y)){
cout << 1 << "\n";
} else {
int k = calc(get_par(x), get_par(y));
if(k == -1) {
cout << "impossible\n";
} else {
if(can(x, y, k)) {
cout << k << "\n";
} else if(can(x, y, k+1)) {
cout << k + 1 << "\n";
} else {
cout << k + 2 << "\n";
}
}
}
}
}
int main() {
solve();
}
Compilation message (stderr)
events.cpp: In function 'void solve()':
events.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0; i < st.size(); i++) {
| ~~^~~~~~~~~~~
events.cpp:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if(i + 1 == st.size()) {
| ~~~~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |