# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
659843 | 600Mihnea | Event Hopping (BOI22_events) | C++17 | 1587 ms | 2716 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
bool home = 0;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int q;
struct T {
int l;
int r;
int init;
};
bool operator < (T a, T b) {
if (a.r != b.r) {
return a.r < b.r;
} else {
return a.l < b.l;
}
}
T v[N];
int dp[N];
int nxt[N];
int ant[N];
int inv[N];
int low[N];
int high[N];
int cnt_rs_under(int limit) {
int low = 1, high = n, cnt = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (v[mid].r <= limit) {
cnt = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return cnt;
}
int main() {
if (!home) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
} else {
freopen("input.txt", "r", stdin);
}
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> v[i].l >> v[i].r;
v[i].init = i;
}
sort(v + 1, v + n + 1);
for (int i = 1; i <= n; i++) {
int x = cnt_rs_under(v[i].l - 1) + 1;
int y = cnt_rs_under(v[i].r);
low[i] = x;
high[i] = y;
}
for (int i = 1; i <= n; i++) {
inv[v[i].init] = i;
}
for (int iq = 1; iq <= q; iq++) {
int ff, ss;
cin >> ff >> ss;
ff = inv[ff];
ss = inv[ss];
int L = ss, R = ss, cost = 0;
while (!(L <= ff && ff <= R)) {
int L2 = L, R2 = R;
for (int j = L; j <= R; j++) {
L2 = min(L2, low[j]);
R2 = max(R2, high[j]);
}
if (L2 == L && R2 == R) {
break;
}
L = L2;
R = R2;
cost++;
}
if (!(L <= ff && ff <= R)) {
cout << "impossible\n";
} else {
cout << cost << "\n";
}
}
return 0;
}
/**
**/
Compilation message (stderr)
# | 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... |