void buildST(ll PN,ll pr_start,ll pr_end)
{
	if(pr_start==pr_end)
		ST[PN]=A[pr_start];
	else
	{
		ll mid=(pr_start+pr_end)/2;
		buildST(PN*2,pr_start,mid);
		buildST(PN*2+1,mid+1,pr_end);
		ST[PN]=maxVal(ST[PN*2],ST[PN*2+1]);
	}
}
	void buildST(int pos, int lo, int hi, int *a) //Building Segment Tree
	{
	    if(lo>hi) return;//Wrong values
	    //Left Child: 2*pos, Right child: 2*pos+1
	    if(lo==hi)
	    {
	        st[pos]=a[lo]; //Getting the array element at the position of the leaf to be inserted 
	        return;
	    }
	    buildST(2*pos,lo,(lo+hi)/2, a); //Recursively Building Left child
	    buildST(2*pos+1,1+(lo+hi)/2,hi ,a); //Recursively building Right Child
	    st[pos]=(st[2*pos]+st[1+ 2*pos]); //setting the parent node as the max of the children node
	}
    STNode* buildST(vector<int> &A, int s, int e){
        if(s > e)
            return NULL;
        if(s == e)
           return new STNode(s, e, A[s]);

        STNode* t = new STNode(s, e, 0);
        int mid = s + (e - s)/2;
        STNode* l = buildST(A, s, mid);
        STNode* r = buildST(A, mid + 1, e);
        t->left = l;
        t->right = r;
        t->sum = l->sum + r->sum;
        return t;
    }
 /**
  *@param A, queries: Given an integer array and an query list
  *@return: The result list
  */
 vector<long long> intervalSum(vector<int> &A, vector<Interval> &queries) {
     // write your code here
     STNode* root = buildST(A, 0, (int)A.size() - 1);
     vector<long long> res;
     if(!root)
         return res;
     for(int i = 0; i < (int)queries.size(); i++){
         res.push_back(queryST(root, queries[i].start, queries[i].end));	
     }
     return res;
 }
Exemplo n.º 5
0
void buildST(int node,int l,int r)
{
    int m = (l+r)/2, c = 2*node+1,x;
    if(l > r)
        return;
    if(l==r)
        {
            segment[node].sum = input[l] - 48;
            if(segment[node].sum%3==0)
                segment[node].subs = 1;
        }
    else
    {
        buildST(c,l,m);
        buildST(c+1,m+1,r);

        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++;
    }
}
Exemplo n.º 6
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;
}
int main()
{
	inputAndStore();
	ll M,x,y,l,r,i;
	ll sum=0;
	scanf("%lld%lld%lld",&M,&x,&y);
	buildST(1,0,N-1);
	l=minVal(x,y);
	r=maxVal(x,y);
	sum=query(l,r,1,0,N-1);
	//printf("%lld\n",sum);
	for(i=2;i<=M;i++)
	{
		x=(x+7)%(N-1);
		y=(y+11)%N;
		l=minVal(x,y);
		r=maxVal(x,y);
		sum+=query(l,r,1,0,N-1);
	}
	printf("%lld\n",sum);
	return 0;
}