#if defined(ONPC) && !defined(_GLIBCXX_ASSERTIONS)
#define _GLIBCXX_ASSERTIONS
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include "jumps.h"
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define allit(a) (a).begin(), (a).end()
#define sz(a) ((int) (a).size())
using namespace std;
using ll = long long;
using vi = vector<int>;
namespace pd = __gnu_pbds;
template<typename K>
using ordered_set = pd::tree<K, pd::null_type, less<int>, pd::rb_tree_tag, pd::tree_order_statistics_node_update>;
template<typename... T>
using gp_hash_table = pd::gp_hash_table<T...>;//simple using statement gives warnings
const int INF = 1e9;
const ll INFL = 1e18;
const int N = 2000;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
int subtask;
struct SegTree {
int n;
vi tree;
SegTree(int n_) : n(n_), tree(2*n_, INF) {}
inline void set(int i, int v) {tree[n+i] = min(tree[n+i], v);}
inline int get(int i) {return tree[n+i];}
void calc() {
for (int i = n-1; i >= 1; i--) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
}
int query(int l, int r) {
l += n;
r += n;
int res = INF;
while (l <= r) {
if (l % 2 == 1) res = min(res, tree[l++]);
if (r % 2 == 0) res = min(res, tree[r--]);
l /= 2;
r /= 2;
}
return res;
}
void update(int i, int v) {
tree[n+i] = v;
for (i = (i+n) / 2; i >= 1; i /= 2) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
}
void print() {
for (int i = n; i < 2*n; i++) {
if (tree[i] == INF) printf(" #");
else printf("%3d", tree[i]);
}
cout << endl;
}
};
int next_power2(int i) {
i--;
i |= i >> 1;
i |= i >> 2;
i |= i >> 4;
i |= i >> 8;
i |= i >> 16;
return 1+i;
}
vector<vi> adj;
vector<SegTree> dist;
int dfs(int i, int j) {
if (i == j) return 0;
if (dist[i].get(j) < INF) return dist[i].get(j);
int res = INF;
for (int e : adj[i]) {
res = min(res, 1+dfs(e, j));
}
dist[i].set(j, res);
return res;
}
void init_2_3(int n, vi& h) {
adj.resize(n);
dist.reserve(n);
for (int i = 0; i < n; i++) {
dist.push_back(SegTree(next_power2(n)));
}
for (int i = 0; i < n; i++) {
int j = i-1, k = i+1;
for (; j >= 0; j--) {
if (h[j] > h[i]) break;
}
for (; k < n; k++) {
if (h[k] > h[i]) break;
}
if (j >= 0) adj[i].push_back(j);
if (k < n) adj[i].push_back(k);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (h[i] < h[j]) dfs(i, j);
}
}
for (int i = 0; i < n; i++) {
dist[i].calc();
}
}
void init_4(int n, vi& h) {
adj.resize(n);
stack<int> prev;
for (int i = 0; i < n; i++) {
while (!prev.empty() && h[prev.top()] < h[i]) {
adj[prev.top()].push_back(i);
prev.pop();
}
prev.push(i);
}
while (!prev.empty()) prev.pop();
for (int i = n-1; i >= 0; i--) {
while (!prev.empty() && h[prev.top()] < h[i]) {
adj[prev.top()].push_back(i);
prev.pop();
}
prev.push(i);
}
debug(adj);
}
void init(int n, std::vector<int> h) {
if (false && n <= 2000) {
subtask = 2;
init_2_3(n, h);
} else {
int inc = 1;
for (int i = 1; i < n; i++) inc &= h[i-1] < h[i];
if (inc)
subtask = 1;
else {
subtask = 4;
init_4(n, h);
}
}
debug(subtask);
}
int minimum_jumps_2_3(int a, int b, int c, int d) {
int res = INF;
for (int i = a; i <= b; i++) {
res = min(res, dist[i].query(c, d));
debug(i, res);
}
return (res < INF ? res : -1);
}
int minimum_jumps_4(int a, int b, int c, int d) {
queue<pair<int,int>> q;
for (int i = a; i <= b; i++)
q.push({i, 0});
int res = -1;
while (!q.empty()) {
auto [cur, depth] = q.front();
q.pop();
if (c <= cur && cur <= d) {
res = depth;
break;
}
for (int e : adj[cur])
q.push({e, depth+1});
}
return res;
}
int minimum_jumps(int A, int B, int C, int D) {
if (subtask == 2) return minimum_jumps_2_3(A,B,C,D);
if (subtask == 4) return minimum_jumps_4(A,B,C,D);
else return C - B;
}
Compilation message
jumps.cpp: In function 'void init_4(int, vi&)':
jumps.cpp:11:24: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 42
| ^~
jumps.cpp:140:5: note: in expansion of macro 'debug'
140 | debug(adj);
| ^~~~~
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:11:24: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 42
| ^~
jumps.cpp:157:5: note: in expansion of macro 'debug'
157 | debug(subtask);
| ^~~~~
jumps.cpp: In function 'int minimum_jumps_2_3(int, int, int, int)':
jumps.cpp:11:24: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 42
| ^~
jumps.cpp:164:9: note: in expansion of macro 'debug'
164 | debug(i, res);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
109 ms |
1440 KB |
Output is correct |
4 |
Correct |
781 ms |
1840 KB |
Output is correct |
5 |
Correct |
667 ms |
956 KB |
Output is correct |
6 |
Correct |
645 ms |
1872 KB |
Output is correct |
7 |
Correct |
500 ms |
1312 KB |
Output is correct |
8 |
Correct |
824 ms |
1864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Correct |
7 ms |
336 KB |
Output is correct |
7 |
Correct |
4 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
324 KB |
Output is correct |
9 |
Correct |
3 ms |
208 KB |
Output is correct |
10 |
Correct |
10 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
10 ms |
320 KB |
Output is correct |
14 |
Correct |
9 ms |
328 KB |
Output is correct |
15 |
Runtime error |
2621 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Correct |
7 ms |
336 KB |
Output is correct |
7 |
Correct |
4 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
324 KB |
Output is correct |
9 |
Correct |
3 ms |
208 KB |
Output is correct |
10 |
Correct |
10 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
10 ms |
320 KB |
Output is correct |
14 |
Correct |
9 ms |
328 KB |
Output is correct |
15 |
Runtime error |
2621 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
59 ms |
13856 KB |
Output is correct |
6 |
Runtime error |
3345 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Execution timed out |
4030 ms |
23320 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Execution timed out |
4030 ms |
23320 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
109 ms |
1440 KB |
Output is correct |
4 |
Correct |
781 ms |
1840 KB |
Output is correct |
5 |
Correct |
667 ms |
956 KB |
Output is correct |
6 |
Correct |
645 ms |
1872 KB |
Output is correct |
7 |
Correct |
500 ms |
1312 KB |
Output is correct |
8 |
Correct |
824 ms |
1864 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
7 ms |
336 KB |
Output is correct |
15 |
Correct |
4 ms |
208 KB |
Output is correct |
16 |
Correct |
7 ms |
324 KB |
Output is correct |
17 |
Correct |
3 ms |
208 KB |
Output is correct |
18 |
Correct |
10 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
208 KB |
Output is correct |
20 |
Correct |
6 ms |
208 KB |
Output is correct |
21 |
Correct |
10 ms |
320 KB |
Output is correct |
22 |
Correct |
9 ms |
328 KB |
Output is correct |
23 |
Runtime error |
2621 ms |
1048576 KB |
Execution killed with signal 9 |
24 |
Halted |
0 ms |
0 KB |
- |