제출 #1200974

#제출 시각아이디문제언어결과실행 시간메모리
1200974guagua0407밀림 점프 (APIO21_jumps)C++20
0 / 100
4021 ms28032 KiB
#include<bits/stdc++.h>
using namespace std;
#ifndef local
#include "jumps.h"
#endif
#define F first
#define S second
#define double long double
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
const int mxn = 2e5 + 5;
int n, h[mxn], l[20][mxn], r[20][mxn];
void init(int N, std::vector<int> H) {
    n = N;
    h[0] = 2e9, h[n + 1] = 2e9;
    for(int i = 1; i <= n; i++){
        h[i] = H[i - 1];
    }
    vector<int>st;
    st.pb(0);
    for(int i = 1; i <= n ; i++){
        while(sz(st) and h[st.back()] < h[i]) st.pop_back();
        l[0][i] = st.back();
        st.pb(i);
    }
    st.clear();
    st.pb(n + 1);
    r[0][n + 1] = n + 1;
    l[0][n + 1] = n + 1;
    for(int i = n ; i > 0; i--){
        while(sz(st) and h[st.back()] < h[i]) st.pop_back();
        r[0][i] = st.back();
        st.pb(i);
    }
    for(int i = 1; i < 20; i++){
        for(int j = 0; j <= n + 1; j++){
            l[i][j] = min(l[i - 1][l[i - 1][j]], l[i - 1][r[i - 1][j]]);
            r[i][j] = max(r[i - 1][r[i - 1][j]], r[i - 1][l[i - 1][j]]);
            //cout << i << ' ' << j << ' ' << l[i][j] << ' ' << r[i][j] << '\n';
        }
    }
}
int minimum_jumps(int A, int B, int C, int D) {
    A++, B++, C++, D++;
    int mn=1e9;
    for(int t=A;t<=B;t++){
        int L = t, R = t, ans = 0;
        for(int i = 19; i >= 0; i--){
            int nl = min(l[i][L], l[i][R]);
            int nr = max(r[i][L], r[i][R]);
            //cout << nl << ' ' << nr << '\n';
            if(max(h[nl],h[nr]) < h[C]){
                ans += (1 << i);
                L = nl, R = nr;
            }
        }
        for(int i = 19; i >= 0; i--){
            int nl = min(l[i][L], l[i][R]);
            int nr = max(r[i][L], r[i][R]);
            //cout << nl << ' ' << nr << '\n';
            if(h[nr] < h[C]){
                ans += (1 << i);
                L = nl, R = nr;
            }
        }
        //cout << r[0][R] << ' ' << r[0][L] << '\n';;
        if((r[0][R] >= C and r[0][R]<=D) or (r[0][L] >= C and r[0][L]<=D) ){
            mn=min(mn,ans+1);
        }
    }
    return (mn==1e9?-1:mn);
}
#ifdef local
void CHECK(){
    cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    function<bool(string,string)> compareFiles = [](string p1, string p2)->bool {
        std::ifstream file1(p1);
        std::ifstream file2(p2);
        if(!file1.is_open() || !file2.is_open()) return false;
        std::string line1, line2;
        int cnt = 0;
        while (getline(file1, line1) && getline(file2, line2)) {
            if (line1 != line2){
                return false;
            }
            cnt++;
        }
        int cnta = 0, cntb = 0;
        while(getline(file1,line1))cnta++;
        while(getline(file2,line2))cntb++;
        return cntb - cnta <= 1;
    };
    bool check = compareFiles("output.txt","expected.txt");
    if(check) cerr<<"ACCEPTED\n";
    else cerr<<"WRONG ANSWER!\n";
}
#else
#endif
#ifdef local
int main() {
    freopen("/Users/iantsai/cpp/input.txt","r",stdin);
    freopen("/Users/iantsai/cpp/output.txt","w",stdout);
  int N, Q;
  assert(2 == scanf("%d %d", &N, &Q));
  std::vector<int> H(N);
  for (int i = 0; i < N; ++i) {
    assert(1 == scanf("%d", &H[i]));
  }
  init(N, H);

  for (int i = 0; i < Q; ++i) {
    int A, B, C, D;
    assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
    printf("%d\n", minimum_jumps(A, B, C, D));
  }
  CHECK();
  return 0;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...