void updateST(int node, int a,int b, int i, int j, int value) //Updating array in [i,j] hence update the SegTree
	{//Refer similar commands documentation in RSumQ

		if(lazy[node]!=0)
		{
			st[node]+=lazy[node];
			if(a!=b)
			{
				lazy[2*node]+=lazy[node];
				lazy[2*node+1]+=lazy[node];
			}
			lazy[node]=0;
		}
		if(i>b || j<a || a>b)//Not overlapping
			return;

		if(a>=i && b<=j)//Overlapping
		{
			st[node] +=value;//Updating values
			if(a!=b)
			{//Flagging children
				lazy[2*node]+=value;
				lazy[2*node +1]+=value;
			}
			return;
		}

		updateST(2*node, a, (a+b)/2, i,j,value);
		updateST(2*node+1, 1+ (a+b)/2, b, i,j,value);
		st[node]=(st[2*node]+st[2*node+1]);
	}
Esempio n. 2
0
void updateST(int node,int l,int r,int index,int newvalue)
{
    if(l>r || r<index || l>index)
        return;
    if(l==r && l==index)
    {
        segment[node].subs = 0;
        segment[node].sum = newvalue;
        if(newvalue%3==0)
            segment[node].subs = 1;
        return;
    }

    int c=2*node+1, m = (l+r)/2;

    updateST(c,l,m,index,newvalue);
    updateST(c+1,m+1,r,index,newvalue);

    segment[node].sum = segment[c].sum + segment[c+1].sum;
    segment[node].subs = segment[c].subs + segment[c+1].subs;

        if(segment[node].sum%3==0)
            segment[node].subs++;
}
Esempio n. 3
0
int main()
{
    int n,m,i,a,b,c;
    scanf("%d %d",&n,&m);
    scanf("%s",input);
    buildST(0,0,n-1);
                                for(i=0;i<3*n;i++)
                                    printf("%lld ",segment[i].subs);
    while(m--)
    {
        scanf("%d %d %d",&a,&b,&c);
        if(a==1)
        {
            updateST(0,0,n-1,b-1,c);
            input[b-1] = c+48;
        }
        else printf("%lld\n",query(0,0,n-1,b-1,c-1));
    }
    return 0;
}