Zzulioj 1827: Stone Full Mix (segment tree to find interval and)

Source: Internet
Author: User
Tags mixed
1827: Stone Full MixTime limit:1 Sec Memory limit:128 MB
Submit:4 Solved:2

Submit Status Web Board Description

Stone all mixed up to a magical restaurant, which has a variety of delicious menus, but the rule of the restaurant is that it can only be ordered on a menu of successive dishes. Stone all mix heart naturally is want the menu on all point a part, but touch pocket found money not enough. Since the calculation of the total price is too troublesome, please write a program, every time the stone pot mixed Select the starting point and the end of the point immediately tell him how much it costs to order. Input

Multi-instance

N,m (n is the number of dishes on the menu <100000,m is the total mix of stone pot selection of the number of times <10000)
Next Line n number A1 A2 ... an for the price of each dish (ai<10000)
The next M-line two numbers for the start and end point Output of his choice

Output a line total for each query sample Input 3 1 1 2 3 1 3 sample output 6

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 100010
using namespace std;
struct ZZ
{
	int l;
	int R;
	int m;
} q[n<<2];
int a[n];
void build (int gen,int l,int r)
{
	q[gen].l=l;
	Q[gen].r=r;
	if (l==r)
	{
		q[gen].m=a[l];
		return;
	}
	int mid= (L+R)/2;
	Build (Gen<<1,l,mid);
	Build (gen<<1|1,mid+1,r);
	q[gen].m=q[gen<<1].m+q[gen<<1|1].m;
}
int query (int gen,int l,int r)
{
	if (L<=Q[GEN].L&&R>=Q[GEN].R)
		return q[gen].m;
	int mid= (Q[GEN].L+Q[GEN].R)/2;
	int cnt=0;
	if (l<=mid)
		cnt+=query (gen<<1,l,r);
	if (r>mid)
		cnt+=query (gen<<1|1,l,r);
	return cnt;
}
int main ()
{
	int n,m;
	int x, y;
	while (scanf ("%d%d", &n,&m)!=eof)
	{
		for (int i=1;i<=n;i++)
			scanf ("%d", &a[i]);
		Build (1,1,n);
		while (m--)
		{
			scanf ("%d%d", &x,&y);
			printf ("%d\n", Query (1,x,y));
		}
	}
	return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.