Submission #654722

# Submission time Handle Problem Language Result Execution time Memory
654722 2022-11-01T10:55:12 Z Sam_a17 Rainforest Jumps (APIO21_jumps) C++14
56 / 100
4000 ms 58096 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) {
  if(A == B && C == 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);
    return answ2 + answ;
  } else {
    queue<pair<int, int>> q;
    vector<bool> vis(n + 1);
    for(int i = A; i <= B; i++) {
      q.push({i, 0});
      vis[i] = true;
    }
  
    while(!q.empty()) {
      auto u = q.front();
      q.pop();
  
      if(u.first >= C && u.first <= D) {
        return u.second;
      }
  
      if(nxt[u.first][0] != -1 && !vis[nxt[u.first][0]]) {
        vis[nxt[u.first][0]] = true;
        q.push({nxt[u.first][0], u.second + 1});
      }
  
      if(pre[u.first][0] != -1 && !vis[pre[u.first][0]]) {
        vis[pre[u.first][0]] = true;
        q.push({pre[u.first][0], u.second + 1});
      }
    }
  
    return -1;
  }
}
# 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 Execution timed out 4035 ms 46600 KB Time limit exceeded
4 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 0 ms 208 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 3 ms 336 KB Output is correct
12 Correct 3 ms 336 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 3 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 3 ms 336 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 3 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 0 ms 208 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 2 ms 208 KB Output is correct
28 Correct 2 ms 336 KB Output is correct
29 Correct 3 ms 336 KB Output is correct
30 Correct 2 ms 424 KB Output is correct
31 Correct 3 ms 336 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 0 ms 208 KB Output is correct
34 Correct 0 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 0 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 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 0 ms 208 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 3 ms 336 KB Output is correct
12 Correct 3 ms 336 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 3 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 3 ms 336 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 3 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 0 ms 208 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 2 ms 208 KB Output is correct
28 Correct 2 ms 336 KB Output is correct
29 Correct 3 ms 336 KB Output is correct
30 Correct 2 ms 424 KB Output is correct
31 Correct 3 ms 336 KB Output is correct
32 Correct 2 ms 336 KB Output is correct
33 Correct 0 ms 208 KB Output is correct
34 Correct 0 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 0 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 0 ms 208 KB Output is correct
41 Correct 0 ms 220 KB Output is correct
42 Correct 0 ms 208 KB Output is correct
43 Correct 0 ms 208 KB Output is correct
44 Correct 2 ms 208 KB Output is correct
45 Correct 2 ms 336 KB Output is correct
46 Correct 2 ms 336 KB Output is correct
47 Correct 3 ms 336 KB Output is correct
48 Correct 1 ms 336 KB Output is correct
49 Correct 3 ms 336 KB Output is correct
50 Correct 2 ms 336 KB Output is correct
51 Correct 2 ms 336 KB Output is correct
52 Correct 2 ms 336 KB Output is correct
53 Correct 3 ms 336 KB Output is correct
54 Correct 3 ms 336 KB Output is correct
55 Correct 2 ms 336 KB Output is correct
56 Correct 2 ms 336 KB Output is correct
57 Correct 1 ms 336 KB Output is correct
58 Correct 23 ms 720 KB Output is correct
59 Correct 37 ms 848 KB Output is correct
60 Correct 7 ms 564 KB Output is correct
61 Correct 32 ms 848 KB Output is correct
62 Correct 13 ms 336 KB Output is correct
63 Correct 41 ms 848 KB Output is correct
64 Correct 59 ms 848 KB Output is correct
65 Correct 46 ms 848 KB Output is correct
66 Correct 48 ms 848 KB Output is correct
67 Correct 33 ms 848 KB Output is correct
68 Correct 41 ms 848 KB Output is correct
69 Correct 41 ms 848 KB Output is correct
70 Correct 34 ms 848 KB Output is correct
71 Correct 1 ms 208 KB Output is correct
72 Correct 0 ms 336 KB Output is correct
73 Correct 3 ms 336 KB Output is correct
74 Correct 3 ms 336 KB Output is correct
75 Correct 5 ms 336 KB Output is correct
76 Correct 2 ms 336 KB Output is correct
77 Correct 3 ms 336 KB Output is correct
78 Correct 0 ms 208 KB Output is correct
79 Correct 0 ms 208 KB Output is correct
80 Correct 2 ms 208 KB Output is correct
81 Correct 3 ms 336 KB Output is correct
82 Correct 3 ms 336 KB Output is correct
83 Correct 2 ms 336 KB Output is correct
84 Correct 3 ms 336 KB Output is correct
85 Correct 2 ms 336 KB Output is correct
86 Correct 0 ms 208 KB Output is correct
87 Correct 1 ms 336 KB Output is correct
88 Correct 1 ms 336 KB Output is correct
89 Correct 1 ms 336 KB Output is correct
90 Correct 0 ms 336 KB Output is correct
91 Correct 1 ms 336 KB Output is correct
92 Correct 1 ms 336 KB Output is correct
93 Correct 0 ms 336 KB Output is correct
94 Correct 4 ms 336 KB Output is correct
95 Correct 31 ms 848 KB Output is correct
96 Correct 42 ms 876 KB Output is correct
97 Correct 37 ms 848 KB Output is correct
98 Correct 46 ms 848 KB Output is correct
99 Correct 46 ms 848 KB Output is correct
100 Correct 1 ms 336 KB Output is correct
101 Correct 1 ms 464 KB Output is correct
102 Correct 23 ms 848 KB Output is correct
103 Correct 28 ms 848 KB Output is correct
104 Correct 21 ms 848 KB Output is correct
105 Correct 22 ms 848 KB Output is correct
106 Correct 21 ms 848 KB Output is correct
107 Correct 1 ms 336 KB Output is correct
108 Correct 1 ms 464 KB Output is correct
109 Correct 1 ms 848 KB Output is correct
110 Correct 1 ms 848 KB Output is correct
111 Correct 2 ms 848 KB Output is correct
112 Correct 2 ms 848 KB Output is correct
113 Correct 1 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 208 KB Output is correct
5 Correct 128 ms 45836 KB Output is correct
6 Correct 169 ms 56356 KB Output is correct
7 Correct 73 ms 28968 KB Output is correct
8 Correct 150 ms 57124 KB Output is correct
9 Correct 17 ms 8728 KB Output is correct
10 Correct 154 ms 56364 KB Output is correct
11 Correct 155 ms 58012 KB Output is correct
12 Correct 156 ms 57516 KB Output is correct
13 Correct 147 ms 57536 KB Output is correct
14 Correct 147 ms 56264 KB Output is correct
15 Correct 166 ms 56888 KB Output is correct
16 Correct 160 ms 57544 KB Output is correct
17 Correct 167 ms 57516 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 0 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 1 ms 848 KB Output is correct
28 Correct 2 ms 848 KB Output is correct
29 Correct 1 ms 848 KB Output is correct
30 Correct 1 ms 848 KB Output is correct
31 Correct 1 ms 848 KB Output is correct
32 Correct 1 ms 208 KB Output is correct
33 Correct 158 ms 56276 KB Output is correct
34 Correct 145 ms 56360 KB Output is correct
35 Correct 171 ms 57544 KB Output is correct
36 Correct 155 ms 56364 KB Output is correct
37 Correct 166 ms 56956 KB Output is correct
38 Correct 169 ms 58096 KB Output is correct
39 Correct 0 ms 208 KB Output is correct
40 Correct 83 ms 32576 KB Output is correct
41 Correct 160 ms 56364 KB Output is correct
42 Correct 155 ms 57656 KB Output is correct
43 Correct 157 ms 56260 KB Output is correct
44 Correct 164 ms 57016 KB Output is correct
45 Correct 144 ms 58016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 289 ms 26000 KB Output is correct
5 Correct 879 ms 56264 KB Output is correct
6 Correct 475 ms 9680 KB Output is correct
7 Correct 1096 ms 56300 KB Output is correct
8 Correct 542 ms 19816 KB Output is correct
9 Correct 872 ms 56392 KB Output is correct
10 Correct 1942 ms 57928 KB Output is correct
11 Correct 1631 ms 57888 KB Output is correct
12 Correct 1618 ms 57796 KB Output is correct
13 Correct 969 ms 56376 KB Output is correct
14 Correct 1570 ms 57040 KB Output is correct
15 Correct 1037 ms 57908 KB Output is correct
16 Correct 958 ms 58084 KB Output is correct
17 Correct 1 ms 208 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 3 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 17 ms 848 KB Output is correct
28 Correct 20 ms 848 KB Output is correct
29 Correct 20 ms 848 KB Output is correct
30 Correct 13 ms 848 KB Output is correct
31 Correct 11 ms 848 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 89 ms 32612 KB Output is correct
34 Correct 147 ms 56300 KB Output is correct
35 Correct 146 ms 57780 KB Output is correct
36 Correct 166 ms 56476 KB Output is correct
37 Correct 155 ms 56956 KB Output is correct
38 Correct 140 ms 58024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 289 ms 26000 KB Output is correct
5 Correct 879 ms 56264 KB Output is correct
6 Correct 475 ms 9680 KB Output is correct
7 Correct 1096 ms 56300 KB Output is correct
8 Correct 542 ms 19816 KB Output is correct
9 Correct 872 ms 56392 KB Output is correct
10 Correct 1942 ms 57928 KB Output is correct
11 Correct 1631 ms 57888 KB Output is correct
12 Correct 1618 ms 57796 KB Output is correct
13 Correct 969 ms 56376 KB Output is correct
14 Correct 1570 ms 57040 KB Output is correct
15 Correct 1037 ms 57908 KB Output is correct
16 Correct 958 ms 58084 KB Output is correct
17 Correct 1 ms 208 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 3 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 17 ms 848 KB Output is correct
28 Correct 20 ms 848 KB Output is correct
29 Correct 20 ms 848 KB Output is correct
30 Correct 13 ms 848 KB Output is correct
31 Correct 11 ms 848 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 89 ms 32612 KB Output is correct
34 Correct 147 ms 56300 KB Output is correct
35 Correct 146 ms 57780 KB Output is correct
36 Correct 166 ms 56476 KB Output is correct
37 Correct 155 ms 56956 KB Output is correct
38 Correct 140 ms 58024 KB Output is correct
39 Correct 1 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 265 ms 26028 KB Output is correct
43 Correct 1035 ms 56260 KB Output is correct
44 Correct 626 ms 9736 KB Output is correct
45 Correct 917 ms 56360 KB Output is correct
46 Correct 612 ms 19952 KB Output is correct
47 Correct 1037 ms 56440 KB Output is correct
48 Correct 1944 ms 58008 KB Output is correct
49 Correct 1699 ms 57928 KB Output is correct
50 Correct 1728 ms 57788 KB Output is correct
51 Correct 795 ms 56264 KB Output is correct
52 Correct 1621 ms 56888 KB Output is correct
53 Correct 1054 ms 57892 KB Output is correct
54 Correct 1076 ms 57920 KB Output is correct
55 Correct 0 ms 208 KB Output is correct
56 Execution timed out 4048 ms 57052 KB Time limit exceeded
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 208 KB Output is correct
3 Execution timed out 4035 ms 46600 KB Time limit exceeded
4 Halted 0 ms 0 KB -