Submission #1047898

# Submission time Handle Problem Language Result Execution time Memory
1047898 2024-08-07T17:09:16 Z Maite_Morale Rainforest Jumps (APIO21_jumps) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vll vector<ll>
#define MAX 2005
#define oo 1e18
#define pll pair<ll,ll>
#define F first
#define S second

const ll bts=20;
pll p[MAX],s[bts+10][MAX],t[bts+10][MAX];
vll v[MAX];
ll d[MAX],N,pass[MAX][MAX];
void bfs(ll x){
   queue<ll> q;
   q.push(x);pass[x][x]=0;
   while(!q.empty()){
      ll u=q.front();q.pop();
      for(auto w : v[u]){
        if(pass[x][w]!=-1)continue;
        q.push(w);
        pass[x][w]=pass[x][u]+1;
      }
   }
}
pll query(ll x,ll y){
  if(x>y)return {0,N};
  ll c=log2l(abs(x-y)+1);
return max(t[c][x],t[c][y-(1LL<<c)+1]);
}
void init(int n, std::vector<int> H) {
    H.push_back(0);N=n;pll rt={0,n};
    for(int i=0;i<=n;i++){
      t[0][i]={H[i],i};
      if(i!=n)rt=max(rt,t[0][i]);
    }
    for(int i=1;i<=bts;i++){
      for(int j=0;j<=n;j++){
        t[i][j]=max(t[i-1][j],t[i-1][j+(1LL<<(i-1))]);
      }
    }
    queue<pair<ll,pll>> q;
    q.push({rt.S,{0,n-1}});
    d[n]=0;s[0][n]={n,n};
    d[rt.S]=1;s[0][rt.S]={n,n};
    while(!q.empty()){
        pair<ll,pll> u=q.front();q.pop();
        pair<ll,pll> h1={query(u.S.F,u.F-1).S,{u.S.F,u.F-1}};
        if(h1.F!=n){
          p[h1.F]={h1.S.F-1,h1.S.S+1};
          v[h1.F].push_back(p[h1.F].F);
          v[h1.F].push_back(p[h1.F].S);
          q.push(h1);
        }
        pair<ll,pll> h2={query(u.F+1,u.S.S).S,{u.F+1,u.S.S}};
        if(h2.F!=n){
          p[h2.F]={h2.S.F-1,h2.S.S+1};
          v[h2.F].push_back(p[h2.F].F);
          v[h2.F].push_back(p[h2.F].S);
          q.push(h2);
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            pass[i][j]=-1;
        }
        bfs(i);
    }
}
int minimum_jumps(int A, int B, int C, int D) {
    ll r=oo;
    for(int i=A;i<=B;i++){
        for(int j=C;j<=D;j++){
            if(pass[i][j]!=-1){
                r=min(r,pass[i][j]);
            }
        }
    }
    if(r==oo)return -1;
return r;
}

/*
7 4
3 2 1 6 4 5 7
4 4 6 6
1 1 5 5
0 0 2 2
5 5 6 6 
*/

int main() {
  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));
  }
  return 0;
}
/*
7 4
3 2 1 6 4 5 7
4 4 6 6
1 1 5 5
0 0 2 2
5 5 6 6 
*/

Compilation message

/usr/bin/ld: /tmp/ccVkMtzD.o: in function `main':
stub.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccOQOA0F.o:jumps.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status