I am solving http://www.spoj.com/problems/HORRIBLE/ ,which is crashing on entering the first query.I am yet to implement the add function,but update function is not working .
long long int a[10000000];
long long int segtree[450000];
long long int lazy[450000];
void updatetree(int low,int high,int value,int qlow,int qhigh,int pos);
int main()
{
ios::sync_with_stdio(false);
int t,b;
long long int n,c,p,q,v,i;
cin>>t;
while(t--)
{
// memset(lazy,0,sizeof(lazy));
cin>>n>>c;
for(i=0;i<450000;i++)
{
lazy[i]=0;
segtree[i]=0;
}
for(i=0;i<n;i++)
a[i]=0;
for(i=0;i<c;i++)
{
cin>>b;
if(b==0)
{
cin>>p>>q>>v;
updatetree(0,n-1,v,p,q,0);
}
/* else
{
cin>>p>>q;
addtree(p,q);
}*/
}
}
return 0;
}
void updatetree(int low,int high,int value,int qlow,int qhigh,int pos)
{
if(lazy[pos]!=0)
{
segtree[pos]+=lazy[pos];
if(low!=high)
{
lazy[2*pos+1]+=lazy[pos];
lazy[2*pos+2]+=lazy[pos];
}
lazy[pos]=0;
}
if(qlow<=low&&qhigh>=high)
{
segtree[pos]+=value;
if(low!=high)
{
lazy[2*pos+1]+=value;
lazy[2*pos+2]+=value;
}
return ;
}
//if partial overlaps
int mid;
mid=(low+high)/2;
updatetree(low,mid,value,qlow,qhigh,2*pos+1);
updatetree(mid+1,high,value,qlow,qhigh,2+pos+2);
segtree[pos]=min(segtree[2*pos+1],segtree[2*pos+2]);
}