what's wrong with my code for matchsticks??

minimum
query
range

#1

can anybody tell me what’s wrong with mine code …i tried to implement range min query algo.
but i can’t figure out what’s wrong it seems correct to me…

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int A[100003],M[300007],C[300007];
 void initializem(int node, int b, int e, int M[], int A[], int N)
  {
      if (b == e)
          M[node] = b;
      else
       {
  
          initializem(2*node, b, (b + e)/2, M, A, N);
          initializem(2*node + 1, (b + e)/2 + 1,e, M, A, N);
 
          if (A[M[2 * node]] <= A[M[2 * node + 1]])
              M[node] = M[2 * node];
          else
              M[node] = M[2 * node + 1];
      }
  }

  int minquery(int node, int b, int e, int M[], int A[], int i, int j)
  {
      int p1, p2;



  
      if (i > e || j < b)
          return -1;

  
      if (b >= i && e <= j)
          return M[node];

  
      p1 = minquery(2*node, b, (b + e)/2, M, A, i, j);
      p2 = minquery(2*node + 1, (b + e)/2 + 1, e, M, A, i, j);

 
      if (p1 == -1)
          return M[node] = p2;
      if (p2 == -1)
          return M[node] = p1;
      if (A[p1] <= A[p2])
          return M[node] = p1;
      return M[node] = p2;


  }

  void initializec(int node, int b, int e, int C[], int A[], int N)
  {
      if (b == e)
          C[node] = b;
      else
       {
 
          initializec(2*node, b, (b + e)/2, C, A, N);
          initializec(2*node + 1, (b + e)/2 + 1, e, C, A, N);
  
          if (A[C[2*node]] >= A[C[2*node + 1]])
              C[node] = C[2*node];
          else
              C[node] = C[2*node + 1];
      }
  }

  int maxquery(int node, int b, int e, int C[], int A[], int i, int j)
  {
      int p1, p2;



  
      if (i > e || j < b)
          return -1;

  
      if (b >= i && e <= j)
          return C[node];

 
      p1 = maxquery(2*node, b, (b + e)/2, C, A, i, j);
      p2 = maxquery(2*node + 1, (b + e)/2 + 1, e, C, A, i, j);

 
      if (p1 == -1)
          return C[node] = p2;
      if (p2 == -1)
          return C[node] = p1;
      if (A[p1] >= A[p2])
          return C[node] = p1;
      return C[node] = p2;


  }


int main()
{
int n,q,l,r,i,j,k,p;
double b,m1,m;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&A*);
}
j=4*n;
for(i=0;i<j;i++)
{
 M*=-1;
 C*=-1;
}
initializem(1,0,n-1,M,A,n);
initializec(1,0,n-1,C,A,n);
scanf("%d",&q);


while(q--)
{
  scanf("%d%d",&l,&r);
   i=minquery(1,0,n-1,M,A,l,r);

   j=maxquery(1,0,n-1,C,A,0,l-1);

   k=maxquery(1,0,n-1,C,A,r+1,n-1);

   m=(A[j]>=A[k])?A[j]:A[k];

   m+=A*;

   p=maxquery(1,0,n-1,C,A,l,r);
b=double(A*)+double(A[p]-A*)/2;
m1=(b>m)?b:m;
    printf("%.1f

",m1);

}
return 0;
}

#2

small question: try l=0,r=n-1;

j=maxquery(1,0,n-1,C,A,0,l-1);

k=maxquery(1,0,n-1,C,A,r+1,n-1);

m=(A[j]>=A[k])?A[j]:A[k];

there might be a issue here as j=-1,k=-1 and so some ambiguous operations


#3

in addition to what @aashish_iitm said, while returning from the functions don’t do return M[node] = p1;
just do return p1;