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;
}