I have a question on Interval Trees to solve and I know basicaly the algorithm, but I have a problem in my code when my functions returns a value to it's main.
The very question I am having is finding the maximum value between certain indexes, and updating some value in the array. So there is an initial array with n numbers, and m operations. If the operation starts with 0, I should perfom an interogation on the maximum value between the index x
. If the operation starts with 1, I should update the value on index x
on the initial vector with y
.
The problem is that at some interogations it retrives in the file the correct answer, and at some it just gives some "random" numbers.
I did some printf during the code so that I can monitorize the answer and I saw that at the end, in function, before returning the value it is perfectly correct, and when I check it in main right after the function it gives me the result I told you.
This is the input I am testing on:
5 5
4 3 5 6 1
0 1 3
1 3 2
0 2 3
1 4 2
0 1 5
Code:
#include <stdio.h>
FILE *fin, *fout;
int pos, val, m, n, *arb;
bool f;
int max(int a, int b);
void pun(int nod, int st1, int dr1);
int interogare(int nod, int st1, int dr1, int st2, int dr2);
int main()
{
fin = fopen("arbori.in", "r");
fout = fopen("arbori.out", "w");
fscanf(fin, "%d%d", &n, &m);
arb = new int[2*n];
for(int i = 1; i<= 2*n - 1; i++) arb[i] = 0;
for(int i =1; i<= n; i++)
{
pos = i;
fscanf(fin, "%d", &val);
pun(1, 1, n);
}
for(int i =1; i<= m; i++)
{
fscanf(fin, "%d", &f);
if(f == 1)
{
fscanf(fin, "%d%d", &pos, &val);
pun(1, 1, n);
}
else
{
fscanf(fin, "%d%d", &pos, &val);
fprintf(fout, "%d\n", interogare(1, 1, n, pos, val));//if i check it here it's not good
}
}
return 0;
}
int max(int a, int b)
{
return (a> b)?a:b;
}
void pun(int nod, int st1, int dr1)//update function - working properly
{
if(st1 == dr1)
{
arb[nod] = val;
return ;
}
int mij = (st1 + dr1)/2;
if(pos <= mij) pun(2*nod, st1, mij);
else pun(2*nod+1, mij +1, dr1);
arb[nod] = max(arb[2*nod + 1], arb[2*nod]);
return ;
}
int interogare(int nod, int st1, int dr1, int st2, int dr2)//interogation function
{
if(st1 == st2 && dr1 == dr2) return arb[nod];//if i check my result here it is the expected one
int mij = (st1 + dr1)/2;
if(st2 > mij) interogare(2*nod+1, mij+1, dr1, st2, dr2);
if(dr2 <= mij) interogare(2*nod, st1, mij, st2, dr2);
if(st2 <= mij && dr2 > mij) return max(interogare(2*nod, st1, mij, st2, mij),interogare(2*nod+1, mij + 1, dr1, mij + 1, dr2));
}
Sorry for the long post and long code, if I omitted something please remind me.
Thank You in advance!
When you call interogare
recursively, you don't return the result:
int interogare(int nod, int st1, int dr1, int st2, int dr2)//interogation function
{
if(st1 == st2 && dr1 == dr2) return arb[nod];//if i check my result here it is the expected one
int mij = (st1 + dr1)/2;
if(st2 > mij) return interogare(2*nod+1, mij+1, dr1, st2, dr2);
if(dr2 <= mij) return interogare(2*nod, st1, mij, st2, dr2);
if(st2 <= mij && dr2 > mij) return max(interogare(2*nod, st1, mij, st2, mij),interogare(2*nod+1, mij + 1, dr1, mij + 1, dr2));
}