이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct C_HASH {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef long long ll;
const int INF = 1e9;
const ll INFL = 1e18;
const int MOD = 1000000007;
#define SIZE(a) (int)(a).size()
#define endl '\n'
#define all(x) x.begin(), x.end()
#define ALL(x) begin(x), end(x)
int pos[200005];
int h[200005];
int lo[200005][18];
int hi[200005][18];
int le[200005][18];
int ri[200005][18];
void init(int N, std::vector<int> H) {
for (int i = 0; i < 200005; ++i) {
for (int j = 0; j < 18; ++j) {
lo[i][j] = INF;
hi[i][j] = INF;
le[i][j] = INF;
ri[i][j] = INF;
}
}
for (int i = 0; i < N; ++i) {
h[i] = H[i];
pos[H[i]] = i;
}
set<int> s;
for (int i = N; i >= 1; --i) {
s.insert(pos[i]);
auto l = s.lower_bound(pos[i]);
if (l != s.begin()) {
l--;
le[i][0] = H[*l];
}
auto r = s.upper_bound(pos[i]);
if (r != s.end()) ri[i][0] = H[*r];
hi[i][0] = max(le[i][0], ri[i][0]);
lo[i][0] = min(le[i][0], ri[i][0]);
}
for (int j = 1; j < 18; ++j) {
for (int i = 1; i <= N; ++i) {
if (lo[i][j - 1] != INF) lo[i][j] = lo[lo[i][j - 1]][j - 1];
if (hi[i][j - 1] != INF) hi[i][j] = hi[hi[i][j - 1]][j - 1];
if (le[i][j - 1] != INF) le[i][j] = le[le[i][j - 1]][j - 1];
if (ri[i][j - 1] != INF) ri[i][j] = ri[ri[i][j - 1]][j - 1];
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
int jumps = 0;
int curr = h[B];
for (int i = 17; i >= 0; --i) {
if (le[curr][i] != INF and pos[le[curr][i]] >= A and curr < h[C]) curr = le[curr][i];
}
for (int i = 17; i >= 0; --i) {
if (hi[curr][i] <= h[C]) {
curr = hi[curr][i];
jumps += (1 << i);
}
}
for (int i = 17; i >= 0; --i) {
if (lo[curr][i] <= h[C]) {
curr = lo[curr][i];
jumps += (1 << i);
}
}
if (curr != h[C]) return -1;
return jumps;
}
#ifdef LOCAL_PROJECT
int main() {
int N, Q;
assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> H(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &H[i]));
}
init(N, H);
for (int i = 0; i < Q; ++i) {
int A, B, C, D;
assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
printf("%d\n", minimum_jumps(A, B, C, D));
}
return 0;
}
#else
#include "jumps.h"
#define cerr \
if (false) cerr
#endif
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |