You are not logged in. Please login at www.codechef.com to post your questions!

×

Constructor of a Struct in C++ Causes Non-Zero Exit Code

When I call the constructor for my SegTree struct in the code below, I keep getting a non-zero exit code. Can someone explain why this is happening and how to fix my code?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <vector>
#include <string.h>

using namespace std;

struct SegTree{
    int N;
    long long tree [1<<20], arr [1<<20];
    SegTree(int x){ N = x; }
    int left(int p){ return p<<1; }
    int right(int p){ return (p<<1) + 1; }
    void build(int p, int L, int R, bool isOR){
        if(L == R) tree[p] = arr[L];
        else{
            build(left(p), L, (L+R)/2, !isOR); build(right(p), (L+R)/2+1, R, !isOR);
            if(isOR) tree[p] = tree[left(p)] | tree[right(p)];
            else tree[p] = tree[left(p)] ^ tree[right(p)];
        }
    }
    void update(int p, int L, int R, bool isOR, int place, long long val){
        if(L > R || L > place || R < place) return;
        if(L == R && L == place){
            tree[p] = val;
            return;
        }
        update(left(p), L, (L+R)/2, !isOR, place, val); update(right(p), (L+R)/2+1, R, !isOR, place, val);
        if(isOR) tree[p] = tree[left(p)] | tree[right(p)];
        else tree[p] = tree[left(p)] ^ tree[right(p)];
    }
};

int len, numQueries;
bool start;

int main(){
    scanf("%d %d", &len, &numQueries); start = len%2 == 1;
    SegTree st(len);
    /*for(int i = 1; i <= len; i++){
        long long temp; scanf("%d", &temp);
        st.arr[i] = temp;
    }
    st.build(1, 1, len, start);
    for(int i = 0; i < numQueries; i++){
        int x; long long y; scanf("%d %d", &x, &y);
        st.update(1, 1, len, start, x, y);
        cout << st.tree[1] << endl;
    }*/
    return 0;
}

Thanks, vmaddur

asked 27 Jun '17, 23:37

vmaddur's gravatar image

2★vmaddur
112
accept rate: 0%


1<<20 = 1048576; changing it to 104857 worked for me. it seems that the size of that array is too huge which is causing this problem.
You can try dynamically allocating those arrays like this:

long long *tree, *arr;
SegTree(int x){
    N = x;
    tree = new long long[1<<20];
    arr = new long long[1<<20];
    }

This worked well on my machine. If you have any more problems feel free to comment.

link

answered 28 Jun '17, 15:18

divyansh_gaba7's gravatar image

4★divyansh_gaba7
76818
accept rate: 23%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,914
×14
×3

question asked: 27 Jun '17, 23:37

question was seen: 333 times

last updated: 28 Jun '17, 15:18