zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    最小生成树Prim算法
    哈夫曼树与哈夫曼编码
    二叉树的非递归遍历
    浅谈C++中指针和引用的区别
    poj2406 Power Strings
    (收藏)KMP算法的前缀next数组最通俗的解释
    HDU 1556 Color the ball
    Floyd算法
    最短路Dijkstra和Flyod
    编程中无穷大常量的设定技巧
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3845152.html
Copyright © 2011-2022 走看看