# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
746070 | nguyentunglam | Railway Trip 2 (JOI22_ho_t4) | C++17 | 2079 ms | 1968 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.
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 5010;
int l[20][N], r[20][N];
int n, m, k;
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> k >> m;
for(int i = 1; i <= n; i++) l[0][i] = r[0][i] = i;
for(int i = 1; i <= m; i++) {
int a, b; cin >> a >> b;
if (a < b) for(int j = a; j <= min(a + k - 1, b - 1); j++) for(int k = j + 1; k <= b; k++) r[0][j] = max(r[0][j], b);
else for(int j = a; j >= max(a - k + 1, b + 1); j--) for(int k = j - 1; k >= b; k--) l[0][j] = min(l[0][j], b);
}
int lg = log2(n);
for(int j = 1; j <= lg; j++) for(int i = 1; i <= n; i++) {
l[j][i] = r[j][i] = i;
for(int k = l[j - 1][i]; k <= r[j - 1][i]; k++) {
l[j][i] = min(l[j][i], l[j - 1][k]);
r[j][i] = max(r[j][i], r[j - 1][k]);
}
}
int q; cin >> q;
while (q--) {
int s, t; cin >> s >> t;
int left = s, right = s, ans = 0;
for(int j = lg; j >= 0; j--) {
int ll = left, rr = right;
for(int k = left; k <= right; k++) {
ll = min(ll, l[j][k]);
rr = max(rr, r[j][k]);
}
if (t > rr || t < ll) {
left = ll; right = rr;
ans += (1 << j);
}
}
if (ans == (1 << lg << 1) - 1) ans = -2;
cout << ans + 1 << endl;
}
}
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... |