1

I am trying to build a segment tree to get to calculate sub array with max sum in given interval of an array... (for each query)

I am getting segmentation fault in the function "void build()" I have checked with array size....this works fine when size are small...but segmentation fault occurs for larger array size..

thanks in advance:)


success test case:
3 
-1 2 3
1
1 2
2

seg fault test case:
90
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
324 3 23 -234 32 -4 324 435 -5775
9
1 4
3 5
5 9
1 40
2 50
5 90
10 40
50 90
30 50
<pre><code>
#include<iostream>
#include<limits.h>
#include<math.h>
using namespace std;

typedef struct node node;
struct node{
    long int sum;
    long int lbest;
    long int rbest;
    long int max;
};


void build(node *tree , long int n , long int start , long int end , long int *ar ){
    if(start == end){
        tree[n].sum = tree[n].lbest = tree[n].rbest = tree[n].max = ar[start];
        return ;
    }
    else{
    long int mid = (start + end)/2;
    build(tree , (2*n) +1 , start , mid , ar);
    build(tree , (2*n) +2 , mid+1 , end , ar);

    tree[n].sum = tree[2*n+1].sum + tree[2*n+2].sum;
    tree[n].lbest = max(tree[2*n+1].lbest , tree[2*n+1].sum + tree[2*n+2].lbest);
    tree[n].rbest = max(tree[2*n+2].rbest , tree[2*n+2].sum + tree[2*n+1].rbest);
    tree[n].max = max(tree[n].sum , max(tree[n].lbest , tree[n].rbest));
 //     cout<<start<<" "<<end<<" -- "<<n<<" ";
 //     cout<<" else "<<endl;

    }

}

long int query(node *tree , long int  n , long int l , long int r , long int start , long int end){
    if(l > end || r < start)return INT_MIN;
    if(start >= l && end <= r)return tree[n].max;
    long int mid = (start + end)/2;
    return max(query(tree, (n*2)+1 , l , r , start , mid) , query(tree , 2*n+2 , l , r, mid +1 , end));
}




int main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);


    long int n , q;
    scanf("%ld" , &n);

    long int ar[n];
    for(long int i = 0 ;i< n ; i++)cin>>ar[i];

    long int nn = n*2;
    node tree[nn+1];
    build(tree, 0 ,0,n-1 ,ar);

    scanf("%ld" , &q);

    while(q--){
        long a , b ;
        scanf("%ld %ld" , &a ,&b);
            a-- ;b--;
        printf("%ld\n" ,query(tree, 0 , a , b, 0 , n-1));
    }



    return 0;
}




</code></pre>
2
  • 4
    Unrelated to your problem, but C++ doesn't really have variable-length arrays. Use std::vector instead. Commented Jun 9, 2018 at 17:51
  • Thanks :).. wil use vector and check.... but I simply made some changes in program and run it as 'C' program still it throws segmentation fault.. :(..any suggestions for it.. Commented Jun 9, 2018 at 18:10

1 Answer 1

1

The minimum size of segment tree must be 2^(ceil(log(n))+1)-1 where n is number of elements in array .


Just increase the segment tree size to 3*n .

Source : https://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.