Poj 3468 线段树的区间更新

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

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+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

很典型的线段树区间更新,第一题关于线段树更新的。
对于线段树的初步认识。利用二分的方法,锁定一个区间,进行一些操作,使得本来的遍历由于数大次数多,所以非常慢,利用一个数组进行维护以后很明显的提升其速度。这个维护的数组和你想要做的操作有关 如果你想求区间和,那么这个数组就是存储的区间的和,又由于二分的方法,所以的在“谨慎的操作下”是没有疏漏的,但像我这样的初学者,代码弱,操作起来出错的几率也比较大,多做点题好好熟悉下,而且即使是同一种线段树,代码风格也是有很多种,来回切换的话不利于熟悉其中的一种(才做三道题。。。三种不同的写法。) 也考虑可以用结构体维护,感觉结构体会简单点。其实用二叉树的思维会更好的理解。

#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 111111;
LL sum[N<<2],add[N<<2]; //add为节点的和,sum为区间的和
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1]; //向上更新,一般维护最大值这样的,用pushpu维护区间的最大值
                                     //可以维护到二叉树的树根。
}
void build(int l,int r,int rt)
{
    add[rt]=0;
    if(l==r) 
    {
        scanf("%I64d",&sum[rt]);  //这种二叉树的建立方法,我感觉更好,比全部初始为0再逐一更新要好很多。
                                  //毕竟树的叶子节点顺序和输入顺序一致,另外在线段树里面只有叶子节点保存的是真正的值
                                  //与数组里的一致
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    PushUp(rt);
}

void PushDown(int rt,int m)
{
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];            //向下更新,
        sum[rt<<1]+=add[rt]*(m-(m>>1));   //由于(r+l)<<1 所以其实左子树的节点>=右子树的节点
        sum[rt<<1|1]+=add[rt]*(m>>1);
        add[rt]=0;                      //由于add 代表要增加的值,所以每次向下一层的增值更新以后 当前节点的add清零
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&R>=r)             //如果当前节点区间在需要更新的区间范围内,那么更新add的值
    {                          //代表当前区间下的每一个节点有这个隐藏属性(增值add),而且当其遍历到下一层的
        add[rt]+=c;            //时候会自动增加,这是一种lazy的思想
        sum[rt]+=(r-l+1)*(LL)c;   
        return ;
    }
    PushDown(rt,r-l+1);        //由于没有被返回,即到了当前这一层还没有终止,所以先往下更新。
    int m=(l+r)>>1;             
    if(L<=m) update(L,R,c,l,m,rt<<1);
    if(R>m) update(L,R,c,m+1,r,rt<<1|1);
    PushUp(rt);                 //往下更新完了,为了以后的查询的方便 而且要保证底层的值和顶层的区间和一致
}                               //所以要向上更新,不然害怕出现底层比顶层大的情况
LL Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt];        //当前节点的区间在需要查询的区间内,所以直接提取这个区间和
    }
    PushDown(rt,r-l+1);       //如果这个区间不在需要查询的空间,先向下更新下一层,因为update
    int m=(l+r)>>1;           //只更新到一定深度就不更新了,而查询有可能查得比这个深度更深所以还是需要更新
    LL res=0;
        if(L<=m) res+=Query(L,R,l,m,rt<<1);
    if(R>m) res+=Query(L,R,m+1,r,rt<<1|1);
    return res;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);


        for(int i=0;i<m;i++)
        {
            char ch[2];
            scanf(" %s",ch);
            if(ch[0]=='Q') 
            {
                int a,b;
                scanf("%d%d",&a,&b);
                printf("%I64d\n",Query(a,b,1,n,1));
            }
            if(ch[0]=='C')
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
        }
    }
}

线段树的一个精髓就是它的改变和更新,只要有改变,必须有更新。改变的更新的需要维护的那个数组,要你根据题目需要进行处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值