sto provando a risolvere muraglia con un segment tree e sembra funzionare tutto tranne la query, se riesco a risolvere quella per guardare a destra sono abbastanza sicuro di riuscire a fare facilmente anche quella verso sinistra ma non riesco a capire perchè non funziona ora.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("input.txt");
ofstream fout ("output.txt");
struct segmentTree {
int N;
vector<long long>st;
long long build(int i,int L, int R, vector<long long> &A)
{
if(R - L == 1)
{
return st[i]=A[L];
}
int mid= (L+R)/2;
st[i]= max(build(i*2,L,mid,A),
build(i*2+1,mid,R,A));
return st[i];
}
segmentTree (vector<long long> &A)
{
N=A.size();
st.resize(N*4);
build(1,0,N,A);
}
void update (int i, int L, int R, int pos, long long val)
{
if(R-L == 1)
{
st[i]=val;
return;
}
int mid= (R+L)/2;
if(pos<mid)
{
update(2*i,L,mid,pos,val);
}else{
update(2*i+1,L,mid,pos,val);
}
st[i]=max(st[2*i],st[2*i+1]);
}
void update(int pos,long long val)
{
update(1,0,N,pos,val);
}
int guardaADestra(int i,int tl,int tr,int l,int r,int val)
{
//se non è possibile trovare un caso utile return -1
if(tl>r || tr<l)return -1;
if(st[i]<=val)return -1;
if(tl==tr)return tl;
int tm= tl + (tr-tl)/2;
int L=guardaADestra(2*i,tl,tm,l,r,val);
if(L!=-1)return L;
int R=guardaADestra(2*i+1,tm+1,tr,l,r,val);
return R;
}
int guardaADestra(int pos,int val)
{
return guardaADestra(1,0,N-1,pos+1,N,val);//non sono sicuro dei valori, ho provato a cambiarli ma non cambia il risultato
}
};
int main()
{
//primo testcase
int N=14;
vector<long long>A(N);
for(int i=0;i<14;i++)fin>>A[i];
segmentTree st(A);
cout<<st.guardaADestra(4,A[4]);
}