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??
Why dont you try the same? – krithick santhosh Aug 01 '18 at 18:59