Poj 3468 segment tree segment update

Source: Internet
Author: User

Http://poj.org/problem? Id = 3468

A simple problem with Integers
Time limit:5000 Ms   Memory limit:131072 K
Total submissions:58132   Accepted:17704
Case time limit:2000 ms

Description

You haveNIntegers,A1,A2 ,...,An. You need to deal with two kinds of operations. one type of operation is to add some given number to each number in a given interval. the other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbersNAndQ. 1 ≤N,Q≤ 100000.
The second line containsNNumbers, the initial valuesA1,A2 ,...,An.-1000000000 ≤AI≤ 1000000000.
Each of the nextQLines represents an operation.
"CA B C"Means addingCTo eachAA,AA+ 1 ,...,AB.-10000 ≤C≤ 10000.
"QA B"Means querying the sumAA,AA+ 1 ,...,AB.

Output

You need to answer allQCommands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample output

455915

Hint

The sums may exceed the range of 32-bit integers.

Bytes -----------------------------------------------------------------------------------------------------------------------

Life is so hard! Fail courage and not care the result!

 

It's not easy to update segments. I 've been watching it for two or three days, but it's still like it's in the fog. I don't understand it!

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#define Maxx 100010int n,m;int str[Maxx];long long ans;struct Node{    int left,right;    long long add,sum;}tree[Maxx*4];void build(int l,int r,int rt){    tree[rt].left=l;    tree[rt].right=r;    tree[rt].add=0;        if(l == r)    {        tree[rt].sum=str[l];        return ;       }    int mid=(l+r)/2;    build(l,mid,2*rt);    build(mid+1,r,2*rt+1);    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;}void update(int l,int r,int add,int rt){    if(tree[rt].left>=l && tree[rt].right<=r)    {        tree[rt].sum+=(tree[rt].right-tree[rt].left+1)*add;        tree[rt].add+=add;        return ;       }        if(tree[rt].left>r || tree[rt].right<l)    {        return ;       }    if(tree[rt].add)    {        tree[2*rt].sum += (tree[2*rt].right-tree[2*rt].left+1)*tree[rt].add;        tree[2*rt].add += tree[rt].add;        tree[2*rt+1].sum += (tree[2*rt+1].right-tree[2*rt+1].left+1)*tree[rt].add;        tree[2*rt+1].add += tree[rt].add;        tree[rt].add = 0;       }    update(l,r,add,rt*2);    update(l,r,add,rt*2+1);    tree[rt].sum =tree[rt*2].sum+tree[rt*2+1].sum;}void query(int l,int r,int rt){    if(tree[rt].left>r || tree[rt].right<l)    {            return ;       }    if(tree[rt].left>=l && tree[rt].right<=r)    {        ans+=tree[rt].sum;        return ;       }    if(tree[rt].add)    {        tree[2*rt].sum += (tree[2*rt].right-tree[2*rt].left+1)*tree[rt].add;        tree[2*rt].add += tree[rt].add;        tree[2*rt+1].sum += (tree[2*rt+1].right-tree[2*rt+1].left+1)*tree[rt].add;        tree[2*rt+1].add += tree[rt].add;        tree[rt].add = 0;       }    query(l,r,rt*2);    query(l,r,rt*2+1);    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;}int main(){    int i,j,k,t,a,b,c;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(i=1;i<=n;i++)        {            scanf("%d",&str[i]);           }        build(1,n,1);        char st[2];        for(i=1;i<=m;i++)        {                   scanf("%s",st);                 if(st[0] == ‘Q‘)                {                    scanf("%d%d",&a,&b);                    ans=0;                    query(a,b,1);                    printf("%lld\n",ans);                   }                else                {                    scanf("%d%d%d",&a,&b,&c);                    update(a,b,c,1);                   }           }           }    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.