0

I am trying to implement Segment tree for modify and range product query(product of all elements in range). Following is my code :

using namespace std;

const int N = 1e4;  
long long int n;  // array size
int t[2 * N];

void build() {  // build the tree
    for (int i = n - 1; i > 0; --i) t[i] = t[i<<1] * t[i<<1|1];
}

void modify(int p, int value) {  // set value at position p
    for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
}

int query(int l, int r) {  // sum on interval [l, r)
    int res = 1;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l&1) res *= t[l++];
        if (r&1) res *= t[--r];
    }
    return res;
}

int main()
{
    int t;
    scanf("%d", &t);
    cout<<"t="<<t<<"\n";
    while(t--)
    {
        long long int  q,p;
        scanf("%lld %lld %lld", &n, &q, &p);
        cout<<"n="<<n<<"q="<<q<<"p="<<p<<"\n"; 

        for (int i = 0; i < n; ++i)
        scanf("%d", t + n + i);

        build();

        while(q--)
        {
            //do queries
        }
    }
    return 0;
}

I tired to debug it. The following statements are problematic :

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

for (int i = 0; i < n; ++i)
    scanf("%d", t + n + i);

This code is giving runtime error When I commented out the t+n+i the code was running successfully or when I don't scan the n the code runs fine. ButI need to scan both of them.

Can someone help me out??

ilim
  • 4,477
  • 7
  • 27
  • 46
Buckster
  • 41
  • 12
  • 1
    What do you expect `scanf("%d", t + n + i);` to do? Assign a value to *the sum* of three variables? Where would the value go? – Biffen Jan 17 '17 at 09:24
  • Moreover, you should have gotten a warning from your compiler regarding the type of the second argument to `scanf()`. Enable more compiler warnings, and read them. – Biffen Jan 17 '17 at 10:55
  • @Biffen That `scanf()` is for storing array elements. This line of code is taken directly from codeforces tutorial on segment trees. How else can I do it as `cin>>t[i]` doesn't work. – Buckster Jan 17 '17 at 13:32
  • You'll have to pass a *pointer* to `scanf()`. Do you have a link to that tutorial? If that's what they're telling you to do then I suggest looking for a different tutorial. – Biffen Jan 17 '17 at 13:34
  • @Biffen Can you provide the full line of code ? And here's the link : http://codeforces.com/blog/entry/18051? – Buckster Jan 17 '17 at 13:38
  • There are a few differences between that code and yours. For instance, you have `int t;` in your `main()`, which causes this particular problem. You might want to start with a verbose copy of their code and get it to compile and run. *Then* you can start making changes, but only ones that you fully understand yourself. – Biffen Jan 17 '17 at 13:40
  • @Biffen Thanks. I got it. – Buckster Jan 17 '17 at 13:50
  • Post a *new* question here on SO (after having searched for duplicates, naturally). But make sure it's well-written, preferably including a [mcve]. – Biffen Jan 17 '17 at 13:52
  • @Biffen Thanks, should I delete this question ? – Buckster Jan 17 '17 at 13:54
  • I am pretty much not sure how it works but I faced a similar problem which gave me SIGABRT in Hackerearth when I kept my segment tree array size equal to 2n+1, which I kinda guessed should be enough. It gave me an AC on changing it to 4n.
    Why dont you try the same?
    – krithick santhosh Aug 01 '18 at 18:59

1 Answers1

0

I think the size of your array t, representing the segment tree, is not large enough. Try to use 4*N and see if that works.

int t[4 * N];