Submission #654721

# Submission time Handle Problem Language Result Execution time Memory
654721 2022-11-01T10:52:01 Z Sam_a17 Rainforest Jumps (APIO21_jumps) C++14
23 / 100
2086 ms 58056 KB
#include <bits/stdc++.h>
using namespace std;
 
#define dbg(x) cerr << #x << " " << x << endl;
 
const int maxN = 2e5 + 10, LOG = 22, inf = 1e8;
int h[maxN], n, pre[maxN][LOG], nxt[maxN][LOG];
int mx[maxN][LOG];

struct SegTree {
  vector<int> tree;
  int size = 1;

  void init(int s) {
    while(size < s) {
      size *= 2;
    }
    tree.assign(2 * size - 1, 0);
  }

  void upd(int u, int val, int x, int lx, int rx) {
    if(rx - lx == 1) {
      // dbg(val)
      tree[x] = val;
      return;
    }

    int mid = (lx + rx) / 2;
    if(u < mid) {
      upd(u, val, 2 * x + 1, lx, mid);
    } else {
      upd(u, val, 2 * x + 2, mid, rx);
    }

    tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]);
  }
  
  void upd(int u, int val) {
    upd(u, val, 0, 0, size);
  }

  int qry(int l, int r, int x, int lx, int rx) {
    if(lx >= r || rx <= l) {
      return 0;
    }

    if(lx >= l && rx <= r) {
      return tree[x];
    }

    int mid = (lx + rx) / 2;
    int s1 = qry(l, r,  2 * x + 1, lx, mid);
    int s2 = qry(l, r,  2 * x + 2, mid, rx);
    return max(s1, s2);
  } 

  int qry(int l, int r) {
    return qry(l, r, 0, 0, size);
  } 
};

SegTree seg;

void init(int N, std::vector<int> H) {
  n = N, seg.init(n + 1);
  for(int i = 0; i < N; i++) {
    h[i] = H[i];
    seg.upd(i, h[i]);
 
    //
    nxt[i][0] = pre[i][0] = i;
  }
 
  stack<pair<int, int>> st;
  for(int i = 0; i < N; i++) {
    while(!st.empty() && h[i] > st.top().first) {
      nxt[st.top().second][0] = i;
      st.pop();
    }
    st.push({h[i], i});
  }
 
  while(!st.empty()) st.pop();
 
  for(int i = N - 1; i >= 0; i--) {
    while(!st.empty() && h[i] > st.top().first) {
      pre[st.top().second][0] = i;
      st.pop();
    }
    st.push({h[i], i});
  }

  for(int i = 0; i < N; i++) {
    if(h[nxt[i][0]] >= h[pre[i][0]]) {
      mx[i][0] = nxt[i][0];
    } else {
      mx[i][0] = pre[i][0];
    }
  }

  for(int j = 1; j < LOG; j++) {
    for(int i = 0; i < N; i++) {
      mx[i][j] = mx[mx[i][j - 1]][j - 1];
      nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
      pre[i][j] = pre[pre[i][j - 1]][j - 1];
    }
  }
}
 
int get_mx(int mid, int node) {
  for(int i = 0; i < LOG; i++) {
    if(mid & (1 << i)) {
      node = mx[node][i];
    }
  }
  return node;
}

int get_r(int mid, int node) {
  for(int i = 0; i < LOG; i++) {
    if(mid & (1 << i)) {
      node = nxt[node][i];
    }
  }
  return node;
}

int minimum_jumps(int A, int B, int C, int D) {
  int maxim = seg.qry(A, D);
  if(maxim >= h[D]) {
    return -1;
  }

  int ina = 0, inb = maxN, answ = 0;
  while(ina <= inb) {
    int mid = (ina + inb) / 2;
    int pos = get_mx(mid, A);
    if(h[pos] >= maxim) {
      answ = mid;
      inb = mid - 1;
    } else {
      ina = mid + 1;
    }
  }
  

  int pos = get_mx(answ, A);
  if(pos == D) {
    return answ;
  }
  
  if(h[pos] >= h[D]) {
    answ--;
  }

  pos = get_mx(answ, A);

  ina = 0, inb = maxN;
  int answ2 = 0;
  while(ina <= inb) {
    int mid = (ina + inb) / 2;
    int pos2 = get_r(mid, pos);
    if(h[pos2] >= h[D]) {
      answ2 = mid;
      inb = mid - 1;
    } else {
      ina = mid + 1;
    }
  }

  pos = get_r(answ2, pos);
  if(pos == D) {
    return answ2 + answ;
  }

  return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 248 KB Output is correct
3 Incorrect 307 ms 46736 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 295 ms 25928 KB Output is correct
5 Correct 769 ms 56256 KB Output is correct
6 Correct 730 ms 9680 KB Output is correct
7 Correct 833 ms 56396 KB Output is correct
8 Correct 597 ms 19920 KB Output is correct
9 Correct 623 ms 56392 KB Output is correct
10 Correct 1669 ms 57892 KB Output is correct
11 Correct 1626 ms 57900 KB Output is correct
12 Correct 1929 ms 57780 KB Output is correct
13 Correct 1073 ms 56364 KB Output is correct
14 Correct 1480 ms 57068 KB Output is correct
15 Correct 1061 ms 57920 KB Output is correct
16 Correct 1079 ms 58056 KB Output is correct
17 Correct 0 ms 308 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 2 ms 208 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 20 ms 848 KB Output is correct
28 Correct 26 ms 848 KB Output is correct
29 Correct 23 ms 848 KB Output is correct
30 Correct 23 ms 848 KB Output is correct
31 Correct 22 ms 848 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 86 ms 32612 KB Output is correct
34 Correct 141 ms 56328 KB Output is correct
35 Correct 142 ms 57816 KB Output is correct
36 Correct 142 ms 56460 KB Output is correct
37 Correct 164 ms 57044 KB Output is correct
38 Correct 142 ms 58012 KB Output is correct
# Verdict Execution time Memory 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 295 ms 25928 KB Output is correct
5 Correct 769 ms 56256 KB Output is correct
6 Correct 730 ms 9680 KB Output is correct
7 Correct 833 ms 56396 KB Output is correct
8 Correct 597 ms 19920 KB Output is correct
9 Correct 623 ms 56392 KB Output is correct
10 Correct 1669 ms 57892 KB Output is correct
11 Correct 1626 ms 57900 KB Output is correct
12 Correct 1929 ms 57780 KB Output is correct
13 Correct 1073 ms 56364 KB Output is correct
14 Correct 1480 ms 57068 KB Output is correct
15 Correct 1061 ms 57920 KB Output is correct
16 Correct 1079 ms 58056 KB Output is correct
17 Correct 0 ms 308 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 2 ms 208 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 20 ms 848 KB Output is correct
28 Correct 26 ms 848 KB Output is correct
29 Correct 23 ms 848 KB Output is correct
30 Correct 23 ms 848 KB Output is correct
31 Correct 22 ms 848 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 86 ms 32612 KB Output is correct
34 Correct 141 ms 56328 KB Output is correct
35 Correct 142 ms 57816 KB Output is correct
36 Correct 142 ms 56460 KB Output is correct
37 Correct 164 ms 57044 KB Output is correct
38 Correct 142 ms 58012 KB Output is correct
39 Correct 0 ms 208 KB Output is correct
40 Correct 0 ms 208 KB Output is correct
41 Correct 0 ms 208 KB Output is correct
42 Correct 284 ms 26000 KB Output is correct
43 Correct 1045 ms 56352 KB Output is correct
44 Correct 659 ms 9680 KB Output is correct
45 Correct 1025 ms 56348 KB Output is correct
46 Correct 583 ms 19940 KB Output is correct
47 Correct 741 ms 56252 KB Output is correct
48 Correct 2086 ms 57928 KB Output is correct
49 Correct 1447 ms 57816 KB Output is correct
50 Correct 1908 ms 57844 KB Output is correct
51 Correct 1099 ms 56252 KB Output is correct
52 Correct 1501 ms 56904 KB Output is correct
53 Correct 835 ms 57932 KB Output is correct
54 Correct 1091 ms 58016 KB Output is correct
55 Correct 0 ms 208 KB Output is correct
56 Incorrect 199 ms 56156 KB Output isn't correct
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 248 KB Output is correct
3 Incorrect 307 ms 46736 KB Output isn't correct
4 Halted 0 ms 0 KB -