Mine Code
This is my code for the problem FUNNY MARBELS . For some reason i am getting SIGSTP or a TLE error for update function but the query function is working fine.
What could be the possible errors in this code?
#include<iostream>
using namespace std;
long long n,q,arr[2000005]={0},BIT[2000005]={0},l,r;
void update(long long idx,long long val){
for(idx;idx<=n;idx+=idx&(-idx))
{
BIT[idx]+=val;
}
}
long long query(long long l,long long r){
r++;
long long lsum=0,rsum=0;
for(;l>0;l-=l&(-l))
lsum+=BIT[l];
for(;r>0;r-=r&(-r))
rsum+=BIT[r];
return rsum-lsum;
}
int main(){
cin>>n>>q;
for(long long i=1;i<=n;i++)cin>>arr[i];
for(long long i=1;i<=n;i++){
update(i,arr[i]);
}
//for(int i=1;i<=n;i++)cout<<BIT[i]<<endl;
for(long long i=0;i<q;i++){
string t;cin>>t;
cin>>l>>r;
if(t=="S"){
cout<<query(l,r)<<endl;
}
else if(t=="G"){
update(++l,r);
}
else{
update(++l,-r);
}
}
}
I did some changes in your code.
1.Your BIT size was less than constrain.
2.In update function you were doing l++ that is wrong do ++l because ++l increments first and l++ increments after operation.
3. Take all variables in long long because if a[i] is 2000 then for n=1000000 prefix sum will cross int limit.
Hope so points will be clear for you.
I just realized , I should have done ++ l and not l++ or should have made the increment within the update function .
Thanks a lot !