Submission #983665

# Submission time Handle Problem Language Result Execution time Memory
983665 2024-05-15T22:12:32 Z shjeong Rainforest Jumps (APIO21_jumps) C++17
4 / 100
1198 ms 228084 KB
#include "jumps.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cstring>
#include <vector>
#include <string>
#include <climits>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
#include <list>
using namespace std;
typedef long long ll;
ll n;
struct segtree{
    vector<pair<ll,ll>> tree;
    segtree(): tree(808080){}
    void init(ll node, ll s, ll e, vector<ll> &v){
        if(s==e)tree[node] = {v[s],s};
        else{
            ll mid = s+e>>1;
            init(node<<1,s,mid,v); init(node<<1|1,mid+1,e,v);
            tree[node] = max(tree[node<<1],tree[node<<1|1]);
        }
    }
    pair<ll,ll> query(ll node, ll s, ll e, ll l, ll r){
        if(l>r)return {-1,-1};
        if(e<l or r<s)return {0,0};
        if(l<=s and e<=r)return tree[node];
        ll mid = s+e>>1;
        return max(query(node<<1,s,mid,l,r), query(node<<1|1,mid+1,e,l,r));
    }
    pair<ll,ll> Q(ll l, ll r){ return query(1,1,n,l,r); }
} seg;
vector<ll> v, Rv;
ll L[202020][22];
ll R[202020][22];
ll M[202020][22];
ll RL[202020][22];
ll RR[202020][22];
ll RM[202020][22];
void init(int N, std::vector<int> H) {
    n=N;
    v.push_back(0); Rv=v;
    for(int i = 1 ; i <= n ; i++)v.push_back(H[i-1]), Rv.push_back(H[n-i]);
    seg.init(1,1,n,v);
    stack<ll> st;
    for(int i = 1 ; i <= n ; i++){
        while(st.size() and v[st.top()] < v[i]){
            R[st.top()][0] = i;
            st.pop();
        }
        st.push(i);
    }
    while(st.size())st.pop();
    for(int i = n ; i >= 1 ; i--){
        while(st.size() and v[st.top()] < v[i]){
            L[st.top()][0] = i;
            st.pop();
        }
        st.push(i);
    }
    for(int i = 1 ; i <= n ; i++){
        M[i][0] = (v[L[i][0]] > v[R[i][0]] ? L[i][0] : R[i][0]);
    }
    for(int i = 1 ; i <= n ; i++){
        RL[i][0] = (L[n-i+1][0]==0?n-L[n-i+1][0]+1:0);
        RR[i][0] = (R[n-i+1][0]==0?n-R[n-i+1][0]+1:0);
        RM[i][0] = (M[n-i+1][0]==0?n-M[n-i+1][0]+1:0);
    }
    for(int j = 1 ; j <= 20 ; j++){
        for(int i = 1 ; i <= n ; i++){
            L[i][j] = L[L[i][j-1]][j-1];
            R[i][j] = R[R[i][j-1]][j-1];
            M[i][j] = M[M[i][j-1]][j-1];
            RL[i][j] = RL[RL[i][j-1]][j-1];
            RR[i][j] = RR[RR[i][j-1]][j-1];
            RM[i][j] = RM[RM[i][j-1]][j-1];
        }
    }
}
ll AB, BC, CD;
ll c;
ll solve_terminal(ll A, ll B, ll C){
    ll D=C;
    ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
    ll ret = 0;
    ll cur = B;
    for(int i = 20 ; i >= 0 ; i--){
        if(L[cur][i]<=0)continue;
        if(L[cur][i]>=A and v[L[cur][i]] < CD)cur = L[cur][i];
    }
    for(int i = 20 ; i >= 0 ; i--){
        if(M[cur][i]<=0)continue;
        if(v[M[cur][i]]<CD and M[cur][i]<C)cur = M[cur][i], ret += (1<<i);
    }
    for(int i = 20 ; i >= 0 ; i--){
        if(R[cur][i]<=0)continue;
        if(R[cur][i]<C)cur = R[cur][i], ret += (1<<i);
    }
    ret++; cur = R[cur][0];
    if(cur<C or cur>D)ret = 1e9;
    return ret;
}
ll solve_terminal_reverse(ll A, ll B, ll C){
    ll D=C;
    ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
    ll ret = 0;
    ll cur = B;
    for(int i = 20 ; i >= 0 ; i--){
        if(RL[cur][i]<=0)continue;
        if(RL[cur][i]>=A and Rv[RL[cur][i]] < CD)cur = RL[cur][i];
    }
    for(int i = 20 ; i >= 0 ; i--){
        if(RM[cur][i]<=0)continue;
        if(Rv[RM[cur][i]]<CD and RM[cur][i]<C)cur = RM[cur][i], ret += (1<<i);
    }
    for(int i = 20 ; i >= 0 ; i--){
        if(RR[cur][i]<=0)continue;
        if(RR[cur][i]<C)cur = RR[cur][i], ret += (1<<i);
    }
    ret++; cur = RR[cur][0];
    if(cur<C or cur>D)ret = 1e9;
    return ret;
}
int minimum_jumps(int A, int B, int C, int D) {
    A++; B++; C++; D++;
    return solve_terminal(A,B,C);
    ll cur = B;
    AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
    ll ret = 1e9;
    if(AB>BC){
        ll cur = B;
        for(int i = 20 ; i >= 0 ; i--){
            if(L[cur][i]<=0)continue;
            if(v[L[cur][i]]<BC)cur = L[cur][i];
        }
        if(v[cur]<BC)cur = L[cur][0];
        cur = R[cur][0];
        if(C<=cur and cur<=D)return 1;
    }
    if(BC>0){
        ll pos = seg.Q(B+1,C-1).second;
        ret = min(ret, solve_terminal(A,B,pos)+1);
    }
    if(seg.Q(1,A-1).first > BC){
        ll cur = A-1;
        for(int i = 20 ; i >= 0 ; i--){
            if(L[cur][i]<=0)continue;
            if(v[L[cur][i]]<BC)cur = L[cur][i];
        }
        if(v[cur]<BC)cur = L[cur][0];
        ret = min(ret, solve_terminal_reverse(n-B+1, n-A+1, n-cur+1)+1);
    }
    if(ret==1e9)ret = -1;
    return ret;
}

Compilation message

jumps.cpp: In member function 'void segtree::init(ll, ll, ll, std::vector<long long int>&)':
jumps.cpp:26:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |             ll mid = s+e>>1;
      |                      ~^~
jumps.cpp: In member function 'std::pair<long long int, long long int> segtree::query(ll, ll, ll, ll, ll)':
jumps.cpp:35:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |         ll mid = s+e>>1;
      |                  ~^~
jumps.cpp: In function 'll solve_terminal(ll, ll, ll)':
jumps.cpp:91:8: warning: unused variable 'AB' [-Wunused-variable]
   91 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |        ^~
jumps.cpp:91:31: warning: unused variable 'BC' [-Wunused-variable]
   91 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |                               ^~
jumps.cpp: In function 'll solve_terminal_reverse(ll, ll, ll)':
jumps.cpp:112:8: warning: unused variable 'AB' [-Wunused-variable]
  112 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |        ^~
jumps.cpp:112:31: warning: unused variable 'BC' [-Wunused-variable]
  112 |     ll AB = seg.Q(A,B).first, BC = seg.Q(B+1,C-1).first, CD = seg.Q(C,D).first;
      |                               ^~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:134:8: warning: unused variable 'cur' [-Wunused-variable]
  134 |     ll cur = B;
      |        ^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB Output is correct
2 Correct 4 ms 21080 KB Output is correct
3 Correct 242 ms 192656 KB Output is correct
4 Correct 1115 ms 228084 KB Output is correct
5 Correct 805 ms 134124 KB Output is correct
6 Correct 1198 ms 227732 KB Output is correct
7 Correct 825 ms 172236 KB Output is correct
8 Correct 1006 ms 227828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 23128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 23128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 23128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB Output is correct
2 Incorrect 4 ms 21076 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB Output is correct
2 Incorrect 4 ms 21076 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB Output is correct
2 Correct 4 ms 21080 KB Output is correct
3 Correct 242 ms 192656 KB Output is correct
4 Correct 1115 ms 228084 KB Output is correct
5 Correct 805 ms 134124 KB Output is correct
6 Correct 1198 ms 227732 KB Output is correct
7 Correct 825 ms 172236 KB Output is correct
8 Correct 1006 ms 227828 KB Output is correct
9 Incorrect 4 ms 23128 KB Output isn't correct
10 Halted 0 ms 0 KB -