0

i have an 2d vector of m*3 size, of which first col : lower range, second col:upper range,third col:value.and i have and initial 1-D array of size n(m

void build_tree(long long int *tree,long long int index,long long int s,long long int e)
{
    if(s==e)
    {
        tree[index]=0;
        return;
    }
    if(s>e)
        return;
    long long int mid=(s+e)/2;
    build_tree(tree,2*index,s,mid);
    build_tree(tree,2*index+1,s,mid);

    tree[index]=max(tree[2*index],tree[2*index+1]);
    return;
}
void update_range(long long int *tree,long long int index,long long int s,long long int e,long long int lower,long long int upper,int v)
{
  if(s>upper || e<lower)
  {
      return;
  }
  if(s==e && s>=lower && e<=upper)
  {
      tree[index]=tree[index]+v;
      return;
  }
  if(s>=e)
    return;

 long long int mid=(s+e)/2;
 update_range(tree,2*index,s,mid,lower,upper,v);
 update_range(tree,2*index+1,mid+1,e,lower,upper,v);

 tree[index]=max(tree[2*index],tree[2*index+1]);
 return;

}
long arrayManipulation(int n, vector<vector<int>> queries) {

 /*int a[10000000];
   // vector<int>a;
    for(int i=0;i<n;i++)
        a[i]=0;*/
    long long int *tree=new long long int(4*n+1);
    build_tree(tree,1,0,n-1);

    for(int i=0;i<queries.size();i++)
    {
        update_range(tree,1,0,n-1,queries[i][0]-1,queries[i][1]-1,queries[i][2]);
    }
    return tree[1];
}

for input n=10000000 and m=100000 i am getting segmentation fault, i have tried long long int instead of int ,but its still giving me segmentation fault.

expected output is long.

  • 1
    I don’t see m or 3*m in your code. – ManLaw Aug 03 '19 at 11:55
  • 4
    And when you ran your program in a debugger, what was the reason for the segmentation fault, that you were able to determine with your debugger? Do you know how to use a debugger? Knowing how to effectively use a debugger is a required skill for every C++ developer. If you don't know how to use a debugger, now is an excellent opportunity for you to learn this, so that you will then be able to figure out bugs in your own code, all by yourself. – Sam Varshavchik Aug 03 '19 at 11:55
  • @Sam Varshavchik , but how can i learn debugging –  Aug 03 '19 at 18:40
  • 1
    The same way you learn C++. A good book. – Sam Varshavchik Aug 03 '19 at 19:06

1 Answers1

1

You didn't show an MCVE, you should.

Without one, it's hard to tell exactly what is going on, but I suspect that the problem comes from this (currently commented out) code:

int a[10000000];

How big is this array? On a typical system, it's 40,000,000 bytes long. On a typical Linux system, the stack is limited to 8MiB, which is smaller than the size of the array, resulting in stack overflow. Your question is therefore duplicate of this one.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362