guys i have been stuck in this question for hours …it’s a simple question but i don’t know where am i going wrong.could somebody tell me what’s wrong in the code:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int k[100004];
int tree[300000];
int inp()
{
int n=0;
char ch;
ch=getchar_unlocked();
while(ch>=48 && ch<=57)
{
n=(n<<3)+(n<<1)+ch-'0';
ch=getchar_unlocked();
}
return n;
}
void init(int node,int left,int right)
{
if(left==right)
{
if((k==0)||(k%3==0))
{
tree[node]=1;
}
return;
}
init(2*node,left,(left+right)/2);
init(2*node+1,(left+right)/2+1,right);
tree[node]=tree[2*node]+tree[2*node+1];
}
void update(int node,int left,int right,int a,int b)
{
if(a>right||b<left)
return;
if(left==right)
{
k++;
if(k%3==0)
{
tree[node]=1;
}
else
{
tree[node]=0;
}
return;
}
//cout<<"node "<<node<<" "<<left<<" "<<right<<endl;
update(2*node,left,(left+right)/2,a,b);
update(2*node+1,(left+right)/2+1,right,a,b);
tree[node]=tree[2*node]+tree[2*node+1];
}
int count(int node,int left,int right,int a,int b)
{
if(a>right||b<left)
return -1;
if(left>=a && right<=b)
{
return tree[node];
}
int p1,p2;
p1=count(2*node,left,(left+right)/2,a,b);
p2=count(2*node+1,(left+right)/2+1,right,a,b);
//cout<<"node "<<node<<" "<<p1<<" "<<p2<<endl;
if(p1==-1)
return p2;
if(p2==-1)
return p1;
else
{
return p1+p2;
}
}
int main()
{
int t,c,a,b,n,q,i;
scanf("%d%d",&n,&q);
memset(k,0,sizeof(k));
init(1,0,n-1); //initialize the tree values
/*for(i=1;i<(1<<4);i++)
{
cout<<"tree["<<i<<"] = "<<tree[i]<<endl;
}*/
while(q--)
{
//scanf("%d%d%d",&c,&a,&b);
c=inp();
a=inp();
b=inp();
if(!c)
{
update(1,0,n-1,a,b);
}
else
{
printf("%d\n",count(1,0,n-1,a,b));
/*for(i=1;i<(1<<4);i++)
{
cout<<"tree["<<i<<"] = "<<tree[i]<<endl;
}*/
}
}
return 0;
}