zoukankan      html  css  js  c++  java
  • poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:http://poj.org/problem?

    id=3468

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 83959   Accepted: 25989
    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.

    Source


    题目大意:给定n个数,m个操作。Q a b表示输出【a。b】这段区间内的和,C a b c表示将【a。b】这段区间的全部值都加上c。经过一系列变化,依照要求进行输出。

    解题思路:标准的线段树,建树、更新树以及查找树。

    对于更新树是为了避免改动到最底下而导致超时问题。所以每次改动仅仅改动相相应的区间就可以。然后记录一个add。下次更新或者查询的时候,假设查到该节点,就把add直接加到子节点上去,在将add变为0,避免下次还会反复加。这样仅仅更新到查询的子区间,不须要再往下找了,所以时间复杂度为O(n),更新树和查询树都须要这样。

    由于add不为0,该add从根一直加到了该节点,之前的都加过了,假设更新到时候不加到子节点。还要通过子节点更新当前节点,当前节点的sum值里面含有的add就会被“抹掉”,就不能保证正确性了。还须要注意的就是要用__int64。


    详见代码。

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    #define LL __int64
    
    struct node
    {
        int l,r;
        LL sum;
        LL add;
        //int flag;//用来表示有几个加数
    } s[100000*4];
    
    void InitTree(int l,int r,int k)
    {
        s[k].l=l;
        s[k].r=r;
        s[k].sum=0;
        s[k].add=0;
        if (l==r)
            return ;
        int mid=(l+r)/2;
        InitTree(l,mid,2*k);
        InitTree(mid+1,r,2*k+1);
    }
    
    void UpdataTree(int l,int r,LL add,int k)
    {
    
        if (s[k].l==l&&s[k].r==r)
        {
            s[k].add+=add;
            s[k].sum+=add*(r-l+1);
            return ;
        }
        if (s[k].add!=0)//加数为0就不须要改变了
        {
            s[2*k].add+=s[k].add;
            s[2*k+1].add+=s[k].add;
            s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
            s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
            s[k].add=0;
        }
        int mid=(s[k].l+s[k].r)/2;
        if (l>mid)
            UpdataTree(l,r,add,2*k+1);
        else if (r<=mid)
            UpdataTree(l,r,add,2*k);
        else
        {
            UpdataTree(l,mid,add,2*k);
            UpdataTree(mid+1,r,add,2*k+1);
        }
        s[k].sum=s[2*k].sum+s[2*k+1].sum;
    }
    
    LL SearchTree(int l,int r,int k)
    {
        if (s[k].l==l&&s[k].r==r)
            return s[k].sum;
        if (s[k].add!=0)
        {
            s[2*k].add+=s[k].add;
            s[2*k+1].add+=s[k].add;
            s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
            s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
            s[k].add=0;
        }
        int mid=(s[k].l+s[k].r)/2;
        if (l>mid)
            return SearchTree(l,r,2*k+1);
        else if (r<=mid)
            return SearchTree(l,r,2*k);
        else
            return SearchTree(l,mid,2*k)+SearchTree(mid+1,r,2*k+1);
    }
    
    
    int main()
    {
        int n,q;
        LL w;
        while (~scanf("%d%d",&n,&q))
        {
            InitTree(1,n,1);
            for (int i=1; i<=n; i++)
            {
                scanf("%lld",&w);
                UpdataTree(i,i,w,1);
            }
            for (int i=1; i<=q; i++)
            {
                char ch;
                int a,b;
                LL c;
                getchar();
                scanf("%c%d%d",&ch,&a,&b);
                if (ch=='C')
                {
                    scanf("%lld",&c);
                    UpdataTree(a,b,c,1);
                }
                else if (ch=='Q')
                {
                    LL ans=SearchTree(a,b,1);
                    printf ("%lld
    ",ans);
                }
            }
        }
        return 0;
    }
    




  • 相关阅读:
    CF 120F Spider 树的直径 简单题
    POJ 1155 TELE 背包型树形DP 经典题
    CF 219D Choosing Capital for Treeland 树形DP 好题
    POJ 3162 Walking Race 树形DP+线段树
    HDU 2196 Computer 树形DP 经典题
    __str__()、__repr__()和__format__()
    item系列方法
    getattribute
    isinstance和issubclass
    继承方式完成包装
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7103517.html
Copyright © 2011-2022 走看看