• poj 3468 线段树成段更新


    http://poj.org/problem?id=3468

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 58132   Accepted: 17704
    Case Time Limit: 2000MS

    Description

    You have N integers, A1A2, ... , 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 numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample Input

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

    Sample Output

    4
    55
    9
    15

    Hint

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

    -----------------------------------------------------------------------------------------------------------------------

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

     

    成段更新不容易啊,看了两三天了,仍像云里雾里,不懂啊!

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    
    #define Maxx 100010
    
    int 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
    ",ans);
                       }
                    else
                    {
                        scanf("%d%d%d",&a,&b,&c);
                        update(a,b,c,1);
                       }
               }    
           }
        return 0;
    }
  • 相关阅读:
    Codevs 1283 等差子序列
    Codevs 1282 约瑟夫问题
    Codevs 1228 苹果树
    Codevs 1191 数轴染色
    洛谷P1080 国王游戏
    Codevs 1523 地精部落
    2014-7-17 NOIP模拟赛
    Codevs 3409 搬运礼物
    Codevs 1425 最长公共子串
    TyvjP1863 [Poetize I]黑魔法师之门(2014-8-27)
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3845152.html
Copyright © 2020-2023  润新知