Submission #413341

# Submission time Handle Problem Language Result Execution time Memory
413341 2021-05-28T14:08:47 Z mat_v Rainforest Jumps (APIO21_jumps) C++14
48 / 100
1749 ms 51372 KB
#include "jumps.h"
#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))

using namespace std;

int n;
int niz[200005];
int levo[200005][20];
int desno[200005][20];
int pravi[200005][20];
int poz[200005];

void init(int N, std::vector<int> H) {
    n = N;
    ff(i,0,n - 1){
        niz[i + 1] = H[i];
        poz[niz[i + 1]] = i+1;
    }
    stack<int> s;
    ff(i,1,n){
        while(!s.empty() && niz[s.top()] < niz[i])s.pop();
        if(!s.empty())levo[i][0] = s.top();
        s.push(i);
    }
    while(!s.empty())s.pop();
    fb(i,n,1){
        while(!s.empty() && niz[s.top()] < niz[i])s.pop();
        if(!s.empty())desno[i][0] = s.top();
        s.push(i);
    }
    fb(i,n,1){
        ff(j,1,18)desno[i][j] = desno[desno[i][j - 1]][j - 1];
    }
    ff(i,1,n){
        ff(j,1,18)levo[i][j] = levo[levo[i][j - 1]][j - 1];
    }
    fb(i,n - 1,1){
        int sta = poz[i];
        int lv = levo[sta][0], rv = desno[sta][0];
        if(niz[lv] > niz[rv])pravi[sta][0] = lv;
        else pravi[sta][0] = rv;
        ff(j,1,18)pravi[sta][j] = pravi[pravi[sta][j - 1]][j - 1];
    }

}

int dist(int x, int y){
    if(niz[y] < niz[x])return -1;
    int ans = 0;
    int lv = x;
    int poc = y;
    fb(j,18,0){
        if(pravi[lv][j] && niz[pravi[lv][j]] < niz[poc]){
            ans += (1<<j);
            lv = pravi[lv][j];
        }
    }
    fb(j,18,0){
        if(desno[lv][j] && niz[desno[lv][j]] < niz[poc]){
            ans += (1<<j);
            lv = desno[lv][j];
        }
    }
    if(desno[lv][0] == poc)return ans+1;
    return -1;
}

int minimum_jumps(int A, int B, int C, int D) {
    A++;
    B++;
    C++;
    D++;
    int l1,r1,l2,r2;
    l1 = A, r1 = B, l2 = C, r2 = D;
    int tr = l2;
    fb(j,18,0){
        if(desno[tr][j] && desno[tr][j] <= r2)tr = desno[tr][j];
    }
    int val = niz[tr];

    int lv = r1;
    fb(j,18,0){
        if(levo[lv][j] && levo[lv][j] >= l1 && niz[levo[lv][j]] < val)lv = levo[lv][j];
    }
    if(niz[lv] > val)return -1;

    int mks = 0;
    if(r1+1 <= l2-1){
        int idx = r1+1;
        fb(j,18,0){
            if(desno[idx][j] && desno[idx][j] <= l2-1)idx = desno[idx][j];
        }
        mks = niz[idx];
    }

    mks = max(mks, niz[lv]);
    if(mks > val)return -1;

    int poc = l2;
    fb(j,18,0){
        if(desno[poc][j] && desno[poc][j] <= r2 && niz[desno[poc][j]] < mks)poc = desno[poc][j];
    }
    if(niz[poc] < mks)poc = desno[poc][0];
    if(!poc || poc > r2 || niz[poc] < mks)return -1;

    val = niz[poc];
    lv = r1;
    fb(j,18,0){
        if(levo[lv][j] && levo[lv][j] >= l1 && niz[levo[lv][j]] < val)lv = levo[lv][j];
    }
    if(niz[lv] > val)return -1;

    int ans = 1e9;

    return dist(lv, poc);
}
/*
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2

*/

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
jumps.cpp:17:5: note: in expansion of macro 'ff'
   17 |     ff(i,0,n - 1){
      |     ^~
jumps.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
jumps.cpp:22:5: note: in expansion of macro 'ff'
   22 |     ff(i,1,n){
      |     ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:28:5: note: in expansion of macro 'fb'
   28 |     fb(i,n,1){
      |     ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:33:5: note: in expansion of macro 'fb'
   33 |     fb(i,n,1){
      |     ^~
jumps.cpp:3:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
jumps.cpp:34:9: note: in expansion of macro 'ff'
   34 |         ff(j,1,18)desno[i][j] = desno[desno[i][j - 1]][j - 1];
      |         ^~
jumps.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
jumps.cpp:36:5: note: in expansion of macro 'ff'
   36 |     ff(i,1,n){
      |     ^~
jumps.cpp:3:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
jumps.cpp:37:9: note: in expansion of macro 'ff'
   37 |         ff(j,1,18)levo[i][j] = levo[levo[i][j - 1]][j - 1];
      |         ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:39:5: note: in expansion of macro 'fb'
   39 |     fb(i,n - 1,1){
      |     ^~
jumps.cpp:3:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
jumps.cpp:44:9: note: in expansion of macro 'ff'
   44 |         ff(j,1,18)pravi[sta][j] = pravi[pravi[sta][j - 1]][j - 1];
      |         ^~
jumps.cpp: In function 'int dist(int, int)':
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:54:5: note: in expansion of macro 'fb'
   54 |     fb(j,18,0){
      |     ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:60:5: note: in expansion of macro 'fb'
   60 |     fb(j,18,0){
      |     ^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:78:5: note: in expansion of macro 'fb'
   78 |     fb(j,18,0){
      |     ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:84:5: note: in expansion of macro 'fb'
   84 |     fb(j,18,0){
      |     ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:92:9: note: in expansion of macro 'fb'
   92 |         fb(j,18,0){
      |         ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:102:5: note: in expansion of macro 'fb'
  102 |     fb(j,18,0){
      |     ^~
jumps.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
jumps.cpp:110:5: note: in expansion of macro 'fb'
  110 |     fb(j,18,0){
      |     ^~
jumps.cpp:115:9: warning: unused variable 'ans' [-Wunused-variable]
  115 |     int ans = 1e9;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 247 ms 40744 KB Output is correct
4 Correct 1749 ms 51136 KB Output is correct
5 Correct 1188 ms 25916 KB Output is correct
6 Correct 1535 ms 51096 KB Output is correct
7 Correct 1100 ms 35080 KB Output is correct
8 Correct 1747 ms 51120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Correct 4 ms 328 KB Output is correct
7 Correct 2 ms 328 KB Output is correct
8 Correct 2 ms 328 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 4 ms 328 KB Output is correct
11 Correct 4 ms 328 KB Output is correct
12 Correct 3 ms 328 KB Output is correct
13 Incorrect 3 ms 328 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Correct 4 ms 328 KB Output is correct
7 Correct 2 ms 328 KB Output is correct
8 Correct 2 ms 328 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 4 ms 328 KB Output is correct
11 Correct 4 ms 328 KB Output is correct
12 Correct 3 ms 328 KB Output is correct
13 Incorrect 3 ms 328 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 117 ms 40600 KB Output is correct
6 Correct 151 ms 50348 KB Output is correct
7 Correct 80 ms 25848 KB Output is correct
8 Correct 150 ms 50348 KB Output is correct
9 Correct 19 ms 7752 KB Output is correct
10 Correct 145 ms 50320 KB Output is correct
11 Correct 116 ms 51276 KB Output is correct
12 Correct 107 ms 51088 KB Output is correct
13 Incorrect 114 ms 50872 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 350 ms 23196 KB Output is correct
5 Correct 1213 ms 50368 KB Output is correct
6 Correct 874 ms 8520 KB Output is correct
7 Correct 1354 ms 50316 KB Output is correct
8 Correct 804 ms 17468 KB Output is correct
9 Correct 1418 ms 50352 KB Output is correct
10 Correct 1346 ms 51248 KB Output is correct
11 Correct 1412 ms 51104 KB Output is correct
12 Correct 1395 ms 51072 KB Output is correct
13 Correct 1159 ms 50376 KB Output is correct
14 Correct 1116 ms 50808 KB Output is correct
15 Correct 1215 ms 51116 KB Output is correct
16 Correct 992 ms 51200 KB Output is correct
17 Correct 1 ms 200 KB Output is correct
18 Correct 1 ms 200 KB Output is correct
19 Correct 3 ms 200 KB Output is correct
20 Correct 4 ms 328 KB Output is correct
21 Correct 3 ms 328 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 3 ms 328 KB Output is correct
24 Correct 4 ms 284 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 2 ms 412 KB Output is correct
27 Correct 24 ms 712 KB Output is correct
28 Correct 32 ms 712 KB Output is correct
29 Correct 22 ms 712 KB Output is correct
30 Correct 32 ms 712 KB Output is correct
31 Correct 20 ms 712 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 89 ms 29392 KB Output is correct
34 Correct 149 ms 50336 KB Output is correct
35 Correct 102 ms 51008 KB Output is correct
36 Correct 130 ms 50396 KB Output is correct
37 Correct 141 ms 50780 KB Output is correct
38 Correct 105 ms 51244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 350 ms 23196 KB Output is correct
5 Correct 1213 ms 50368 KB Output is correct
6 Correct 874 ms 8520 KB Output is correct
7 Correct 1354 ms 50316 KB Output is correct
8 Correct 804 ms 17468 KB Output is correct
9 Correct 1418 ms 50352 KB Output is correct
10 Correct 1346 ms 51248 KB Output is correct
11 Correct 1412 ms 51104 KB Output is correct
12 Correct 1395 ms 51072 KB Output is correct
13 Correct 1159 ms 50376 KB Output is correct
14 Correct 1116 ms 50808 KB Output is correct
15 Correct 1215 ms 51116 KB Output is correct
16 Correct 992 ms 51200 KB Output is correct
17 Correct 1 ms 200 KB Output is correct
18 Correct 1 ms 200 KB Output is correct
19 Correct 3 ms 200 KB Output is correct
20 Correct 4 ms 328 KB Output is correct
21 Correct 3 ms 328 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 3 ms 328 KB Output is correct
24 Correct 4 ms 284 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 2 ms 412 KB Output is correct
27 Correct 24 ms 712 KB Output is correct
28 Correct 32 ms 712 KB Output is correct
29 Correct 22 ms 712 KB Output is correct
30 Correct 32 ms 712 KB Output is correct
31 Correct 20 ms 712 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 89 ms 29392 KB Output is correct
34 Correct 149 ms 50336 KB Output is correct
35 Correct 102 ms 51008 KB Output is correct
36 Correct 130 ms 50396 KB Output is correct
37 Correct 141 ms 50780 KB Output is correct
38 Correct 105 ms 51244 KB Output is correct
39 Correct 1 ms 200 KB Output is correct
40 Correct 1 ms 200 KB Output is correct
41 Correct 1 ms 200 KB Output is correct
42 Correct 393 ms 23224 KB Output is correct
43 Correct 1292 ms 50332 KB Output is correct
44 Correct 884 ms 8492 KB Output is correct
45 Correct 1201 ms 50384 KB Output is correct
46 Correct 609 ms 17460 KB Output is correct
47 Correct 1179 ms 50476 KB Output is correct
48 Correct 1266 ms 51096 KB Output is correct
49 Correct 1220 ms 51096 KB Output is correct
50 Correct 1425 ms 50988 KB Output is correct
51 Correct 1239 ms 50296 KB Output is correct
52 Correct 1117 ms 50712 KB Output is correct
53 Correct 1169 ms 51116 KB Output is correct
54 Correct 1238 ms 51136 KB Output is correct
55 Correct 1 ms 200 KB Output is correct
56 Correct 221 ms 50112 KB Output is correct
57 Correct 1244 ms 50444 KB Output is correct
58 Correct 607 ms 9032 KB Output is correct
59 Correct 1096 ms 50328 KB Output is correct
60 Correct 506 ms 18092 KB Output is correct
61 Correct 1249 ms 50352 KB Output is correct
62 Correct 1359 ms 51116 KB Output is correct
63 Correct 1462 ms 50892 KB Output is correct
64 Correct 1197 ms 50880 KB Output is correct
65 Correct 1088 ms 50348 KB Output is correct
66 Correct 1475 ms 50704 KB Output is correct
67 Correct 866 ms 51212 KB Output is correct
68 Correct 1081 ms 51112 KB Output is correct
69 Correct 1 ms 200 KB Output is correct
70 Correct 1 ms 328 KB Output is correct
71 Correct 3 ms 328 KB Output is correct
72 Correct 2 ms 328 KB Output is correct
73 Correct 3 ms 328 KB Output is correct
74 Correct 4 ms 328 KB Output is correct
75 Correct 4 ms 328 KB Output is correct
76 Correct 1 ms 200 KB Output is correct
77 Correct 1 ms 200 KB Output is correct
78 Correct 3 ms 200 KB Output is correct
79 Correct 3 ms 328 KB Output is correct
80 Correct 4 ms 328 KB Output is correct
81 Correct 3 ms 328 KB Output is correct
82 Correct 4 ms 328 KB Output is correct
83 Correct 6 ms 328 KB Output is correct
84 Correct 1 ms 328 KB Output is correct
85 Correct 6 ms 328 KB Output is correct
86 Correct 20 ms 712 KB Output is correct
87 Correct 24 ms 712 KB Output is correct
88 Correct 25 ms 712 KB Output is correct
89 Correct 21 ms 712 KB Output is correct
90 Correct 28 ms 712 KB Output is correct
91 Correct 1 ms 328 KB Output is correct
92 Correct 2 ms 456 KB Output is correct
93 Correct 22 ms 712 KB Output is correct
94 Correct 25 ms 712 KB Output is correct
95 Correct 25 ms 712 KB Output is correct
96 Correct 32 ms 712 KB Output is correct
97 Correct 19 ms 712 KB Output is correct
98 Correct 1 ms 200 KB Output is correct
99 Correct 159 ms 50240 KB Output is correct
100 Correct 154 ms 50352 KB Output is correct
101 Correct 103 ms 50988 KB Output is correct
102 Correct 142 ms 50400 KB Output is correct
103 Correct 119 ms 50716 KB Output is correct
104 Correct 103 ms 51372 KB Output is correct
105 Correct 1 ms 200 KB Output is correct
106 Correct 94 ms 29312 KB Output is correct
107 Correct 154 ms 50368 KB Output is correct
108 Correct 102 ms 50992 KB Output is correct
109 Correct 149 ms 50320 KB Output is correct
110 Correct 120 ms 50712 KB Output is correct
111 Correct 104 ms 51232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 247 ms 40744 KB Output is correct
4 Correct 1749 ms 51136 KB Output is correct
5 Correct 1188 ms 25916 KB Output is correct
6 Correct 1535 ms 51096 KB Output is correct
7 Correct 1100 ms 35080 KB Output is correct
8 Correct 1747 ms 51120 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 200 KB Output is correct
11 Correct 1 ms 200 KB Output is correct
12 Correct 1 ms 200 KB Output is correct
13 Correct 2 ms 328 KB Output is correct
14 Correct 4 ms 328 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 2 ms 328 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 4 ms 328 KB Output is correct
19 Correct 4 ms 328 KB Output is correct
20 Correct 3 ms 328 KB Output is correct
21 Incorrect 3 ms 328 KB Output isn't correct
22 Halted 0 ms 0 KB -