Submission #725725

# Submission time Handle Problem Language Result Execution time Memory
725725 2023-04-18T01:02:51 Z n0sk1ll Rainforest Jumps (APIO21_jumps) C++14
56 / 100
4000 ms 89544 KB
#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;







//Note to self: Check for overflow

#include "jumps.h"

struct segtree
{
    int k=1;
    int mn[550001],mx[550001];
    int l[550001],r[550001];

    void Build(vector<int> &niz, int n)
    {
        while (k<n) k*=2;
        ff(i,0,k) l[i+k]=i,r[i+k]=i;
        bff(i,1,k) l[i]=l[2*i],r[i]=r[2*i+1];
        ff(i,0,n) mn[i+k]=niz[i],mx[i+k]=niz[i];
        bff(i,1,k) mn[i]=min(mn[2*i],mn[2*i+1]);
        bff(i,1,k) mx[i]=max(mx[2*i],mx[2*i+1]);
    }

    int Min(int p, int ll, int rr)
    {
        if (ll>r[p] || rr<l[p]) return mod;
        if (ll<=l[p] && rr>=r[p]) return mn[p];
        return min(Min(2*p,ll,rr),Min(2*p+1,ll,rr));
    }

    int Max(int p, int ll, int rr)
    {
        if (ll>r[p] || rr<l[p]) return -mod;
        if (ll<=l[p] && rr>=r[p]) return mx[p];
        return max(Max(2*p,ll,rr),Max(2*p+1,ll,rr));
    }
} ST;

int h[200005],gde[200005];
int levo[200005],desno[200005];
int opt[20][200005]; //jumping jacks
int grb[20][200005]; //naivno teranje udesno

const int N=5'000'006;
int val[N],L[N],R[N];
int idx=0;

void add(int p, int q, int l, int r, int s, int x)
{
    val[p]+=x;
    if (l==r) return;
    int mid=(l+r)/2;
    if (s<=mid) L[p]=++idx,R[p]=R[q],add(L[p],L[q],l,mid,s,x);
    else L[p]=L[q],R[p]=++idx,add(R[p],R[q],mid+1,r,s,x);
}

int WalkLower(int p, int q, int l, int r, int x)
{
    if (l>x) return -1;
    if (val[p]-val[q]==0) return -1;
    if (l==r) return r;
    int mid=(l+r)/2;
    int mozda=WalkLower(R[p],R[q],mid+1,r,x);
    if (mozda==-1) return WalkLower(L[p],L[q],l,mid,x);
    return mozda;
}

int WalkUpper(int p, int q, int l, int r, int x)
{
    if (r<x) return -1;
    if (val[p]-val[q]==0) return -1;
    if (l==r) return r;
    int mid=(l+r)/2;
    int mozda=WalkUpper(L[p],L[q],l,mid,x);
    if (mozda==-1) return WalkUpper(R[p],R[q],mid+1,r,x);
    return mozda;
}

int svakislucaj[69]; //pomaze pri definiciji root[-1]...
int root[200005]; //root za svakog xd;

int n;
void init(int N, vector<int> H)
{
    n=N;

    ff(i,0,n) h[i]=H[i],gde[h[i]]=i;

    stack<int> mono;
    ff(i,0,n)
    {
        while (!mono.empty() && h[mono.top()]<h[i]) mono.pop();
        if (mono.empty()) levo[i]=-1; else levo[i]=mono.top();
        mono.push(i);
    }
    while (!mono.empty()) mono.pop();
    bff(i,0,n)
    {
        while (!mono.empty() && h[mono.top()]<h[i]) mono.pop();
        if (mono.empty()) desno[i]=-1; else desno[i]=mono.top();
        mono.push(i);
    }

    ff(i,0,n)
    {
        if (levo[i]==-1) opt[0][i]=desno[i];
        else if (desno[i]==-1) opt[0][i]=levo[i];
        else opt[0][i]=(h[levo[i]]>h[desno[i]]?levo[i]:desno[i]);
    }

    ff(k,1,20) ff(i,0,n)
    {
        if (opt[k-1][i]==-1) opt[k][i]=-1;
        else opt[k][i]=opt[k-1][opt[k-1][i]];
    }

    ff(i,0,n) grb[0][i]=desno[i];
    ff(k,1,20) ff(i,0,n)
    {
        if (grb[k-1][i]==-1) grb[k][i]=-1;
        else grb[k][i]=grb[k-1][grb[k-1][i]];
    }

    ST.Build(H,n);

    root[-1]=0;
    ff(i,0,n) root[i]=++idx,add(root[i],root[i-1],1,n,h[i],1);

    /*ff(i,0,n) cout<<levo[i]<<" "; cout<<endl;
    ff(i,0,n) cout<<desno[i]<<" "; cout<<endl;
    cout<<endl;*/

    /*ff(k,0,20)
    {
        ff(i,0,n) cout<<opt[k][i]<<" ";
        cout<<endl;
    } cout<<endl;*/

    /*ff(k,0,20)
    {
        ff(i,0,n) cout<<grb[k][i]<<" ";
        cout<<endl;
    } cout<<endl;*/
}

bool presisao(int ko, int C, int D, int plafon)
{
    if (ko==-1) return true;
    if (h[ko]>plafon) return true;
    if (ko>=C) return true;
    if (desno[ko]>=C) return true;
    return false;
}

bool staje(int ko, int C, int D)
{
    return C<=ko && ko<=D;
}

int putuj(int ko, int C, int D, int plafon)
{
    int skokovi=0;
    bff(k,0,20) if (!presisao(opt[k][ko],C,D,plafon)) ko=opt[k][ko],skokovi+=(1<<k);

    if (staje(ko,C,D)) return skokovi;
    if (staje(desno[ko],C,D)) return skokovi+1;
    if (desno[ko]!=-1 && staje(desno[desno[ko]],C,D)) return skokovi+2;
    if (levo[ko]!=-1 && staje(desno[levo[ko]],C,D)) return skokovi+2;

    while (levo[ko]!=-1 && h[levo[ko]]<plafon) ko=levo[ko],skokovi++; ///ne bi trebalo da pogorsa slozenost...
    bff(k,0,20) if (grb[k][ko]!=-1 && grb[k][ko]<C) ko=grb[k][ko],skokovi+=(1<<k);
    if (!staje(ko,C,D)) ko=grb[0][ko],skokovi++;
    if (staje(ko,C,D)) return skokovi;

    return mod;
}

int minimum_jumps(int A, int B, int C, int D)
{
    int bridge=ST.Max(1,B+1,C-1);
    int mali=WalkLower(root[B],root[A-1],1,n,bridge);
    int veliki=WalkUpper(root[B],root[A-1],1,n,bridge);
    int ogromni=gde[ST.Max(1,A,B)];
    int plafon=ST.Max(1,C,D);

    if (bridge<0) mali=ST.Min(1,A,B),veliki=mali;

    if (mali==-1) mali=ogromni;
    else mali=gde[mali];
    if (veliki==-1) veliki=ogromni;
    else veliki=gde[veliki];

    int pmali=putuj(mali,C,D,plafon);
    int pveliki=putuj(veliki,C,D,plafon);
    int pogromni=putuj(ogromni,C,D,plafon);

    int ans=mod;
    ans=min(ans,pmali);
    ans=min(ans,pveliki);
    ans=min(ans,pogromni);
    
    fff(i,A,B) ans=min(ans,putuj(i,C,D,plafon));

    if (ans>n) ans=-1;
    return ans;
}

/*

5 1000
1 2 3 4 5
0 0 4 4

7 1000
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2

10 1000
9 1 2 3 4 5 6 7 8 10
0 0 1 1
1 1 5 5
1 1 6 6

*/

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:157:12: warning: array subscript -1 is below array bounds of 'int [200005]' [-Warray-bounds]
  157 |     root[-1]=0;
      |     ~~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Execution timed out 4046 ms 72136 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 0 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 0 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 4 ms 592 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 4 ms 592 KB Output is correct
12 Correct 4 ms 592 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 4 ms 592 KB Output is correct
15 Correct 4 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 4 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 4 ms 592 KB Output is correct
23 Correct 3 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 0 ms 592 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 1 ms 592 KB Output is correct
28 Correct 3 ms 592 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 2 ms 592 KB Output is correct
32 Correct 3 ms 592 KB Output is correct
33 Correct 0 ms 592 KB Output is correct
34 Correct 1 ms 592 KB Output is correct
35 Correct 1 ms 592 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 0 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 0 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 0 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 4 ms 592 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 4 ms 592 KB Output is correct
12 Correct 4 ms 592 KB Output is correct
13 Correct 3 ms 592 KB Output is correct
14 Correct 4 ms 592 KB Output is correct
15 Correct 4 ms 592 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 4 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 4 ms 592 KB Output is correct
23 Correct 3 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 0 ms 592 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 1 ms 592 KB Output is correct
28 Correct 3 ms 592 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 2 ms 592 KB Output is correct
32 Correct 3 ms 592 KB Output is correct
33 Correct 0 ms 592 KB Output is correct
34 Correct 1 ms 592 KB Output is correct
35 Correct 1 ms 592 KB Output is correct
36 Correct 1 ms 592 KB Output is correct
37 Correct 1 ms 592 KB Output is correct
38 Correct 1 ms 592 KB Output is correct
39 Correct 0 ms 592 KB Output is correct
40 Correct 1 ms 592 KB Output is correct
41 Correct 0 ms 592 KB Output is correct
42 Correct 0 ms 592 KB Output is correct
43 Correct 1 ms 592 KB Output is correct
44 Correct 2 ms 592 KB Output is correct
45 Correct 2 ms 592 KB Output is correct
46 Correct 2 ms 592 KB Output is correct
47 Correct 5 ms 592 KB Output is correct
48 Correct 1 ms 592 KB Output is correct
49 Correct 2 ms 592 KB Output is correct
50 Correct 4 ms 592 KB Output is correct
51 Correct 3 ms 592 KB Output is correct
52 Correct 3 ms 592 KB Output is correct
53 Correct 3 ms 592 KB Output is correct
54 Correct 3 ms 592 KB Output is correct
55 Correct 3 ms 592 KB Output is correct
56 Correct 3 ms 592 KB Output is correct
57 Correct 1 ms 592 KB Output is correct
58 Correct 58 ms 1104 KB Output is correct
59 Correct 81 ms 1232 KB Output is correct
60 Correct 12 ms 848 KB Output is correct
61 Correct 90 ms 1232 KB Output is correct
62 Correct 22 ms 592 KB Output is correct
63 Correct 83 ms 1232 KB Output is correct
64 Correct 83 ms 1232 KB Output is correct
65 Correct 101 ms 1232 KB Output is correct
66 Correct 68 ms 1232 KB Output is correct
67 Correct 113 ms 1232 KB Output is correct
68 Correct 82 ms 1232 KB Output is correct
69 Correct 59 ms 1232 KB Output is correct
70 Correct 17 ms 1232 KB Output is correct
71 Correct 0 ms 592 KB Output is correct
72 Correct 1 ms 592 KB Output is correct
73 Correct 3 ms 592 KB Output is correct
74 Correct 3 ms 592 KB Output is correct
75 Correct 3 ms 592 KB Output is correct
76 Correct 3 ms 592 KB Output is correct
77 Correct 3 ms 592 KB Output is correct
78 Correct 0 ms 592 KB Output is correct
79 Correct 1 ms 592 KB Output is correct
80 Correct 2 ms 592 KB Output is correct
81 Correct 3 ms 592 KB Output is correct
82 Correct 3 ms 592 KB Output is correct
83 Correct 3 ms 592 KB Output is correct
84 Correct 3 ms 592 KB Output is correct
85 Correct 3 ms 592 KB Output is correct
86 Correct 1 ms 592 KB Output is correct
87 Correct 1 ms 592 KB Output is correct
88 Correct 1 ms 592 KB Output is correct
89 Correct 1 ms 592 KB Output is correct
90 Correct 1 ms 592 KB Output is correct
91 Correct 0 ms 592 KB Output is correct
92 Correct 1 ms 592 KB Output is correct
93 Correct 1 ms 592 KB Output is correct
94 Correct 6 ms 592 KB Output is correct
95 Correct 92 ms 1324 KB Output is correct
96 Correct 92 ms 1232 KB Output is correct
97 Correct 91 ms 1232 KB Output is correct
98 Correct 79 ms 1232 KB Output is correct
99 Correct 88 ms 1232 KB Output is correct
100 Correct 1 ms 592 KB Output is correct
101 Correct 1 ms 848 KB Output is correct
102 Correct 19 ms 1232 KB Output is correct
103 Correct 11 ms 1232 KB Output is correct
104 Correct 18 ms 1232 KB Output is correct
105 Correct 23 ms 1232 KB Output is correct
106 Correct 18 ms 1232 KB Output is correct
107 Correct 1 ms 592 KB Output is correct
108 Correct 1 ms 848 KB Output is correct
109 Correct 1 ms 1232 KB Output is correct
110 Correct 2 ms 1360 KB Output is correct
111 Correct 1 ms 1232 KB Output is correct
112 Correct 1 ms 1232 KB Output is correct
113 Correct 1 ms 1232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 0 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 81 ms 72152 KB Output is correct
6 Correct 114 ms 88728 KB Output is correct
7 Correct 48 ms 44492 KB Output is correct
8 Correct 108 ms 88648 KB Output is correct
9 Correct 16 ms 12752 KB Output is correct
10 Correct 121 ms 88636 KB Output is correct
11 Correct 90 ms 89544 KB Output is correct
12 Correct 89 ms 89248 KB Output is correct
13 Correct 73 ms 89248 KB Output is correct
14 Correct 100 ms 88616 KB Output is correct
15 Correct 84 ms 89088 KB Output is correct
16 Correct 86 ms 89284 KB Output is correct
17 Correct 92 ms 89276 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 592 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 848 KB Output is correct
27 Correct 1 ms 1232 KB Output is correct
28 Correct 2 ms 1232 KB Output is correct
29 Correct 3 ms 1232 KB Output is correct
30 Correct 3 ms 1232 KB Output is correct
31 Correct 1 ms 1232 KB Output is correct
32 Correct 1 ms 592 KB Output is correct
33 Correct 118 ms 88600 KB Output is correct
34 Correct 143 ms 88668 KB Output is correct
35 Correct 91 ms 89248 KB Output is correct
36 Correct 109 ms 88604 KB Output is correct
37 Correct 79 ms 89064 KB Output is correct
38 Correct 101 ms 89536 KB Output is correct
39 Correct 1 ms 592 KB Output is correct
40 Correct 58 ms 49992 KB Output is correct
41 Correct 115 ms 88608 KB Output is correct
42 Correct 82 ms 89372 KB Output is correct
43 Correct 106 ms 88652 KB Output is correct
44 Correct 80 ms 89080 KB Output is correct
45 Correct 72 ms 89504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 228 ms 39960 KB Output is correct
5 Correct 1371 ms 88640 KB Output is correct
6 Correct 711 ms 14596 KB Output is correct
7 Correct 1234 ms 88684 KB Output is correct
8 Correct 812 ms 30636 KB Output is correct
9 Correct 1234 ms 88672 KB Output is correct
10 Correct 1396 ms 89452 KB Output is correct
11 Correct 1347 ms 89388 KB Output is correct
12 Correct 1500 ms 89388 KB Output is correct
13 Correct 1370 ms 88680 KB Output is correct
14 Correct 1473 ms 89076 KB Output is correct
15 Correct 1319 ms 89456 KB Output is correct
16 Correct 1177 ms 89476 KB Output is correct
17 Correct 0 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 3 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 848 KB Output is correct
27 Correct 19 ms 1232 KB Output is correct
28 Correct 25 ms 1232 KB Output is correct
29 Correct 24 ms 1232 KB Output is correct
30 Correct 23 ms 1232 KB Output is correct
31 Correct 19 ms 1232 KB Output is correct
32 Correct 0 ms 592 KB Output is correct
33 Correct 57 ms 50096 KB Output is correct
34 Correct 101 ms 88616 KB Output is correct
35 Correct 83 ms 89404 KB Output is correct
36 Correct 97 ms 88700 KB Output is correct
37 Correct 69 ms 88980 KB Output is correct
38 Correct 70 ms 89540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 228 ms 39960 KB Output is correct
5 Correct 1371 ms 88640 KB Output is correct
6 Correct 711 ms 14596 KB Output is correct
7 Correct 1234 ms 88684 KB Output is correct
8 Correct 812 ms 30636 KB Output is correct
9 Correct 1234 ms 88672 KB Output is correct
10 Correct 1396 ms 89452 KB Output is correct
11 Correct 1347 ms 89388 KB Output is correct
12 Correct 1500 ms 89388 KB Output is correct
13 Correct 1370 ms 88680 KB Output is correct
14 Correct 1473 ms 89076 KB Output is correct
15 Correct 1319 ms 89456 KB Output is correct
16 Correct 1177 ms 89476 KB Output is correct
17 Correct 0 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 3 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 848 KB Output is correct
27 Correct 19 ms 1232 KB Output is correct
28 Correct 25 ms 1232 KB Output is correct
29 Correct 24 ms 1232 KB Output is correct
30 Correct 23 ms 1232 KB Output is correct
31 Correct 19 ms 1232 KB Output is correct
32 Correct 0 ms 592 KB Output is correct
33 Correct 57 ms 50096 KB Output is correct
34 Correct 101 ms 88616 KB Output is correct
35 Correct 83 ms 89404 KB Output is correct
36 Correct 97 ms 88700 KB Output is correct
37 Correct 69 ms 88980 KB Output is correct
38 Correct 70 ms 89540 KB Output is correct
39 Correct 0 ms 592 KB Output is correct
40 Correct 1 ms 592 KB Output is correct
41 Correct 1 ms 592 KB Output is correct
42 Correct 350 ms 39948 KB Output is correct
43 Correct 1225 ms 88784 KB Output is correct
44 Correct 670 ms 14640 KB Output is correct
45 Correct 1295 ms 88620 KB Output is correct
46 Correct 650 ms 30672 KB Output is correct
47 Correct 1306 ms 88616 KB Output is correct
48 Correct 1199 ms 89544 KB Output is correct
49 Correct 1379 ms 89388 KB Output is correct
50 Correct 1290 ms 89392 KB Output is correct
51 Correct 1583 ms 88640 KB Output is correct
52 Correct 1492 ms 88980 KB Output is correct
53 Correct 1261 ms 89440 KB Output is correct
54 Correct 988 ms 89544 KB Output is correct
55 Correct 0 ms 592 KB Output is correct
56 Execution timed out 4073 ms 88496 KB Time limit exceeded
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Execution timed out 4046 ms 72136 KB Time limit exceeded
4 Halted 0 ms 0 KB -