2017.8.23-------树状数组---区间更新+区间查询

来源:互联网 发布:雷神笔记本怎么样知乎 编辑:程序博客网 时间:2024/06/06 02:07

下面是转载+原创的一篇文章:

 

 

【问题引入】

对于区间修改、区间查询这样的简单问题,打一大堆线段树确实是不划算,今天来介绍一下区间查询+区间修改的树状数组

【一些基础】

树状数组的基本知识不再介绍,请自行百度

我们假设sigma(r,i)表示r数组的前i项和,调用一次的复杂度是log2(i)

设原数组是a[n],差分数组c[n],c[i]=a[i]-a[i-1],那么明显地a[i]=sigma(c,i),如果想要修改a[i]到a[j](比如+v),只需令c[i]+=v,c[j+1]-=v

【今天的主要内容】

我们可以实现NlogN时间的“单点修改,区间查询”,“区间修改,单点查询”,其实后者就是前者的一个变形,要明白树状数组的本质就是“单点修改,区间查询”

怎么实现“区间修改,区间查询”呢?

观察式子:
a[1]+a[2]+...+a[n]

= (c[1]) + (c[1]+c[2]) + ... + (c[1]+c[2]+...+c[n]) 

= n*c[1] + (n-1)*c[2] +... +c[n]

= n * (c[1]+c[2]+...+c[n]) - (0*c[1]+1*c[2]+...+(n-1)*c[n])    (式子①)

那么我们就维护一个数组c2[n],其中c2[i] = (i-1)*c[i]

每当修改c的时候,就同步修改一下c2,这样复杂度就不会改变

那么

式子①

=n*sigma(c,n) - sigma(c2,n)

于是我们做到了在O(logN)的时间内完成一次区间和查询

一件很好的事情就是树状数组的常数比其他NlogN的数据结构小得多,实际上它的计算次数比NlogN要小很多,再加上它代码短,是OI中的利器

 

 

下面用一道例题来说明:

Problem T

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 73   Accepted Submission(s) : 17
Problem Description

You have N integers, 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
<p>The first line contains two numbers <i>N</i> and <i>Q</i>. 1 ≤ <i>N</i>,<i>Q</i> ≤ 100000.<br>The second line contains <i>N</i> numbers, the initial values of <i>A</i><sub>1</sub>, <i>A</i><sub>2</sub>, ... , <i>A<sub>N</sub></i>. -1000000000 ≤ <i>A<sub>i</sub></i> ≤ 1000000000.<br>Each of the next <i>Q</i> lines represents an operation.<br>"C <i>a</i> <i>b</i> <i>c</i>" means adding <i>c</i> to each of <i>A<sub>a</sub></i>, <i>A<sub>a</sub></i><sub>+1</sub>, ... , <i>A<sub>b</sub></i>. -10000 ≤ <i>c</i> ≤ 10000.<br>"Q <i>a</i> <i>b</i>" means querying the sum of <i>A<sub>a</sub></i>, <i>A<sub>a</sub></i><sub>+1</sub>, ... , <i>A<sub>b</sub></i>.</p>
 


 

Output
<p>You need to answer all <i>Q</i> commands in order. One answer in a line.</p>
 


 

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

 

样例的意思是C:修改区间(3,6),使每一个数都增加3

q是查询区间的和。

 

 

下面是我的代码:

 

#include<iostream>
#include<stdio.h>
#define M 100100
using namespace std;
long long c[M],c2[M],a[M];


long long lowbit(long long i)
{
    return i&(-i);
}

 

long long sum(long long z,long long i)
{
    long long he=0;
    while(i>0)
    {
        if(z==1)
        he+=c[i];
        else he+=c2[i];
        i-=lowbit(i);
    }
    return he;
}

 

void add(long long wm,long long i,long long z)
{
    if(wm==1)
    while(i<=M)
    {
        c[i]+=z;
        i+=lowbit(i);
    }
    else
    while(i<=M)
    {
        c2[i]+=z;
        i+=lowbit(i);
    }

}


int main()
{
    long long t,m,xx,yy,kk,k;
    char s;
    cin>>t>>m;
    for(int i=1;i<=t;i++)
    {
        scanf("%lld",&a[i]);
        k=a[i]-a[i-1];
        add(1,i,k);
        add(2,i,(i-1)*k);
    }
    while(m--)
    {
        cin>>s;
        if(s=='Q')
        {
            //cin>>xx>>yy;
            scanf("%lld%lld",&xx,&yy);
            cout<<yy*sum(1,yy)-(xx-1)*sum(1,xx-1)-(sum(2,yy)-sum(2,xx-1))<<endl;
             //cout<<(y*sum(1,y)-(x-1)*sum(1,x-1))-(sum(2,y)-sum(2,x-1))<<endl;
        }
        else
        {
            //cin>>xx>>yy>>kk;
            scanf("%lld%lld%lld",&xx,&yy,&kk);
            add(1,xx,kk);
            add(2,xx,(xx-1)*kk);
            add(1,yy+1,-kk);
            add(2,yy+1,-yy*kk);
        }
    }
    return 0;

}

 

 


 

原创粉丝点击