zoukankan      html  css  js  c++  java
  • poj3468 A Simple Problem with Integers (树状数组做法)

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

    题意:给一个数列,Q个操作,遇到C则往[l,r]中加上x,遇到Q则求[l,r]的和
    题解:树状数组做法
    树状数组基本功能是实现单点更新和求前缀和,本题是要实现快速实现区间的更新。
    首先,我们来看,在[l,r]上加上x的话,i<l时,所求的前缀和不变,l<=i<=r时,所求的前缀和增加了x*(i-l+1)=x*i-x*(l-1);
    i>r时,增加了x*(r-l+1)=x*r-x*(l-1);
    这时我们定义两个树状数组
    bit1:前缀和树状数组
    bit0:加法树状数组
    从上面的前缀和增加量来看,从大于L后的前缀和都会增加一个东西,就是-x*(l-1),那么维护这个东西就是updata(bit0,l,-x*(l-1));
    这样就实现了l以后的前缀和都会加上这个东西,然后我们发现x*r这个也可以提前维护,同理就是updata(bit0,r,x*r);但是这里还差
    一个x*i;因为i是变量,所以这个只能在求和的时候去加上,而加法数组中{updata(bit1,l,x);updata(bit1,r,x);}我们发现这个刚
    好实现了在[l,r]的前缀和中加上x,那么对于每个所求的i,在求前缀和的时候就*i就行了
    则sum[i]=sum(bit1,i)*i+sum(bit0,i);
    代码:
    #include<iostream>
    #include<string.h>
    using namespace std;
    typedef long long ll;
    #define MAX 100005
    int n,q;
    int a[MAX];
    ll bit0[MAX],bit1[MAX];
    void updata(ll *b,int i,int val)
    {
        while(i<=n)
        {
            b[i]+=val;
            i+=i&-i;
        }
    }
    ll query(ll *b,int i)
    {
        ll res=0;
        while(i>0)
        {
            res+=b[i];
            i-=i&-i;
        }
        return res;
    }
    int main()
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        while(cin>>n>>q)
        {
            memset(bit0,0,sizeof(bit0));
            memset(bit1,0,sizeof(bit1));
            for(int i=1;i<=n;i++)
            {
               cin>>a[i];
               updata(bit0,i,a[i]);
            }
            while(q--)
            {
                int l,r,x;
                char ch;
                cin>>ch;
                cin>>l>>r;
                if(ch=='C')
                {
                    cin>>x;
                    updata(bit0,l,-x*(l-1));
                    updata(bit1,l,x);
                    updata(bit0,r+1,x*r);
                    updata(bit1,r+1,-x);
                }
                else
                {
                    ll sum=0;
                    sum+=query(bit0,r)+query(bit1,r)*r;
                    sum-=query(bit0,l-1)+query(bit1,l-1)*(l-1);
                    cout<<sum<<endl;
                }
            }
        }
    }

    参考博客:https://www.cnblogs.com/detrol/p/7586083.html

                  https://blog.csdn.net/Puppet__/article/details/79098936

  • 相关阅读:
    BZOJ3899 仙人掌树的同构(圆方树+哈希)
    BZOJ3590 SNOI2013Quare(状压dp)
    BZOJ2178 圆的面积并(simpson积分)
    BZOJ3724 PA2014Final Krolestwo(欧拉回路+构造)
    BZOJ3589 动态树(树链剖分+容斥原理)
    BZOJ3453 XLkxc(拉格朗日插值)
    BZOJ4650 NOI2016优秀的拆分(后缀数组)
    Luogu5058 ZJOI2004嗅探器(割点)
    shutil模块---文件,文件夹复制、删除、压缩等处理
    面向过程---通过查找字符串,找到相应的文件路径
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9559542.html
Copyright © 2011-2022 走看看