Segment tree using array

Why is my code showing runtime error when i am submitting it in question QUERIES https://www.codechef.com/problems/ARHN09

#include <bits/stdc++.h>
using namespace std;

int n,q;
int main() 
{	
	ios::sync_with_stdio(false);
	cin >> n >> q;	
	int a[2*n];
	memset(a,0,sizeof(a));
	for (int i = n; i < 2*n; i++)
	{
	    cin >> a[i];
	}
	
	for (int i = n-1; i > 0; i--)
	{
	    a[i] = a[i*2]+a[i*2+1];
	}
	
	while (q --)
	{
	    int l,r;
	    cin >> l >> r;
	    --l;--r;
	    r ++;
	    l += n;
	    r += n;
	    int s=0;
	    
	    while (l < r)
		{
	        if (l%2==1)
			{
	            s+=a[l];
	            l ++;
	        }
	        if (r%2==1)
			{
	            r --;
	            s+=a[r];
	        }
	        l /= 2;
	        r /= 2;
	    }
	    cout << s << endl;
	}
}

If I recall correctly, the size of segment tree is always ~4*n+4 . And the question is quite poorly worded, like does the queries are of format “sum i j” or is it just “i j” in input.

If you are just starting segment tree, its better to solve the 2 questions from SPOJ, they will clear your concept nicely (I think it was GSS1 or something.)

//