Segmentation fault:11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
vector<int> solve(vector<int> arr, vector<int> queries)
{
    vector<int> res;
    int n = arr.size();
    int q = queries.size();
    int col = log(n) / log(2) + 1;
    int sparseTabe[n][col];
    int Min, d;
    int k = 1;

    // Sparse Table Generation
    for (int i = 0; i < n; i++)
    {
        sparseTabe[i][0] = i;
    }

    for (int i = 1; i < col; i++)
    {
        for (int j = 0; j < n, j + k < n; j++)
        {
            sparseTabe[j][i] = arr[sparseTabe[j][i - 1]] >= arr[sparseTabe[j + k][i - 1]] ? sparseTabe[j][i - 1] : sparseTabe[j + k][i - 1];
        }
        k = k << 1;
    }

    int p, t1, t2, t, pw, mx;
    for (int i = 0; i < q; i++)
    {
        d = queries[i];
        k = d - 1;
        p = log(k + 1) / log(2); // j+k-j+1 = k+1
        pw = pow(2, p);
        t = (k + 1) - pw;
        Min = INT_MAX;
        for (int j = 0; j <= n - d; j++)
        {
            t1 = arr[sparseTabe[j][p]];
            t2 = arr[sparseTabe[j + t][p]];
            mx = t1 >= t2 ? t1 : t2;
            if (Min > mx)
            {
                Min = mx;
            }
        }
        res.emplace_back(Min);
    }

    return res;
}

I'm not good yet with gdb. Any hack on gdb to help me find the bug will be helpful too.
Last edited on
You're not very good at asking questions, either.
And use code tags when posting code to preserve indentation.
What is the "11" in the title"?
And how can we possibly run your code?
Last edited on
Straining my eyes to look at your code I see you use a VLA, which is not only NOT part of legal C++, but also create the array on the stack, so if the product of n and col is too large you may be blowing the stack. Try replacing it with vector<vector<int>> sparseTabe(n, vector<int>(col));

And there is a log2() function for binary logs.
Last edited on
Thanks it worked :)
I think the problem is at line 19:
for (int j = 0; j < n, j + k < n; j++)
should be
for (int j = 0; j < n && j + k < n; j++)
Thanks it worked :)

Thanks for using code tags. :)

And note that dhayden pointed out another definite problem. As is, the j < n has no effect at all. Your compiler should warn you about that. If not, increase the warning level (in g++ and clang++, adding flags -Wall -W -pedantic gives maximum warnings). And don't ignore warnings!
Topic archived. No new replies allowed.