コード例 #1
0
// Driver program to test above functions
int main()
{

    lint n,q,i,j;
    char ch;
    scanf("%lld %lld",&n,&q); 
    for(i=0;i<n;i++)
    {
        scanf("%lld",&arr[i]);
    }
    getchar();
    // Build segment tree from given array
    lint *st = constructST(arr, n);
    while(q--)
    {
        scanf("%c %lld %lld",&ch,&i,&j);
        if(ch=='S')
        {
            printf("%lld\n",getSum(st, n, i, j));
        }
        else if(ch=='G')
        {
            updateValue(arr, st, n, arr[i], arr[i]+j);
        }
        else
        {
            updateValue(arr, st, n, arr[i], arr[i]-j);
        }
        getchar();
    }
    return 0;
}
コード例 #2
0
ファイル: segmenttree.cpp プロジェクト: adityavarma1234/zone
// Driver program to test above functions
int main()
{
    long long int n,Q;
    freopen("input.txt","r",stdin);
    //printf("%llu\n",pow(2,20));
    scanf("%lld%lld",&n,&Q);
    //prlong long intf("%d %d\n",n,Q);
    long long int arr[1000007];
    for(int i=0;i<n;i++) scanf("%lld",&arr[i]);
    // Build segment tree from given array
    long long int *st = constructST(arr, n);
    for(long long int i=0;i<Q;i++)
    {
        char a;
        long long int b,c;
        a = getchar();
        while(a=='\n' || a==' ')
        a = getchar();
        scanf("%lld%lld",&b,&c);
        //prlong long intf("%c %d %d\n",a,b,c);
        if(a=='S')
        printf("%llu\n", getSum(st, n, b, c));

    // Update: set arr[1] = 10 and update corresponding segment
    // tree nodes
    else if(a=='G')
        updateValue(arr, st, n, b, c);

    // Find sum after the value is updated
    else
        updateValue(arr, st, n, b, -c);
    }
    return 0;
}
コード例 #3
0
ファイル: jan15_QSET.cpp プロジェクト: 2012ankitkmr/My-works
/*long int next_int()
{
        long int n = 0;
        char c = getchar();
        while (!('0' <= c && c <= '9'))
        {
                c = getchar();
        }
        while ('0' <= c && c <= '9')
        {
                n = (n<<3)+(n<<1) + c - '0';
                c = getchar();
        }
        return n;
}

*/
int main()
{
 ios_base::sync_with_stdio(false);
	char c[100005];
    int arr[100005],count,i,s,j;
	long int n,q;
cin>>n>>q;
	//n=next_int();
    //q=next_int();
	cin>>c;


for(i=0;i<n;i++)
{
	arr[i]=(c[i]-'0');
}
	
	 int *st = constructST(arr, n);

	while(q--)

	{

		long int a,b,d;

	//	a=next_int();
      //  b=next_int();
      //  d=next_int();
	cin>>a>>b>>d;
		if(a==1)

		{
			    updateValue(arr, st, n, b-1, d);

	}
		else
		{
		count=0;	
		for(i=b-1;i<d;i++)
		{
		for(j=i;j<d;j++)	
		{
		s=getSum(st, n, i, j);

if(s%3==0)
count++;
         }
	
	}
		cout<<count<<"\n";
		}
	}
	return 0;
}
コード例 #4
0
main()
{
        int N,i,Q,L,R,j;
        N=fastread();
        //scanf("%d",&N);
        int b[N];
        float C[N],maxm,minm;


        for(i=0; i<N; i++) {
                // scanf("%d",&b[i]);
                b[i]=fastread();
        }

        int *st = constructST(b, N);
        int *m_st=constructST_m(b,N);

        // scanf("%d",&Q);
        Q=fastread();

        for(j=0; j<Q; j++) {
                //scanf("%d",&L);
                //scanf("%d",&R);
                L=fastread();
                R=fastread();


                //call RMQ for fast processing
                minm= RMQ(st, N, L, R);

                for(i=0; i<L; i++) {
                        C[i]= (minm+b[i]);
                }
                for(i=R+1; i<N; i++) {
                        C[i]= (minm+b[i]);
                }

                for(i=L; i<=R; i++) {
                        C[i]= (float)(minm+b[i])/(float)(2);
                }

                maxm=C[0];
                for(i=1; i<N; i++) {
                        if(C[i]>maxm)
                                maxm=C[i];
                }
                printf("%.1f\n",maxm);
        }

        return 0;
}
コード例 #5
0
ファイル: segment_tree.cpp プロジェクト: lshi2004/Montevideo
// Driver program to test above functions
int main()
{
    int arr[] = {1, 3, 2, 7, 9, 11};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    // Build segment tree from given array
    int *st = constructST(arr, n);
 
    int qs = 1;  // Starting index of query range
    int qe = 5;  // Ending index of query range
 
    // Print minimum value in arr[qs..qe]
    printf("Minimum of values in range [%d, %d] is = %d\n",
                           qs, qe, RMQ(st, n, qs, qe));
 
    return 0;
}
int main(){
	 int arr[] = {1, 3, 5, 7, 9, 11};
    int n = sizeof(arr)/sizeof(arr[0]);

    // Build segment tree from given array
    int *st = constructST(arr, n);

    // Print sum of values in array from index 1 to 3
    //printf("Sum of values in given range = %d\n", getSum(st, n, 1, 3));

    // Update: set arr[1] = 10 and update corresponding segment
    // tree nodes
//    updateValue(arr, st, n, 1, 10);

    // Find sum after the value is updated
  //  printf("Updated sum of values in given range = %d\n", getSum(st, n, 1, 3));
	return 0;
}
コード例 #7
0
ファイル: nklineup.c プロジェクト: nphuc/alg
int main(){
    long n,q;
    long *arr;   
    n=getLong();
    q=getLong();
    arr=malloc(sizeof(long)*(n+1));
    register long i;
    for(i=0;i<n;++i){
        arr[i]=getLong();
    }
    long *stmin=constructST(arr,n);
    long *stmax=constructST2(arr,n);
    i=q;
    long a,b;
    while(i--){
        a=getLong();
        b=getLong();
        printf("%ld\n",RMQ2(stmax,n,a-1,b-1)-RMQ(stmin,n,a-1,b-1));
    }
    free(arr);
    free(stmin);
    free(stmax);
    return 0;
}
コード例 #8
0
ファイル: 1.cpp プロジェクト: aka001/codes
}
int arr[1000000]; 
// Driver program to test above functions
int main()
{
    int n,m,i;
    si(n);si(m);
    rep(i,n)
	    si(arr[i]);
    rep(i,m)
    {
	si(a);si(b);si(k);
    	updateValue(arr,a,b,
    }
    // Build segment tree from given array
    int *st = constructST(arr, n);
 
    // Print sum of values in array from index 1 to 3
    printf("Sum of values in given range = %d\n", getSum(st, n, 1, 3));
 
    // Update: set arr[1] = 10 and update corresponding segment
    // tree nodes
    updateValue(arr, st, n, 1, 10);
 
    // Find sum after the value is updated
    printf("Updated sum of values in given range = %d\n",
                                                  getSum(st, n, 1, 3));
 
    return 0;
}
コード例 #9
0
int getMaxArea(int hist[], int n) {
	int* st = constructST(hist, n);

	return getMaxAreaRect(hist, st, n, 0, n - 1);
}