POJ 3468 A Simple Problem with Integers【线段树】【延时标记】

题目传送门: 点击打开链接
A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 129782   Accepted: 40297
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

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi

看似是一道很裸的线段树题目,但是如果直接用线段树裸模板的话会超时。
这时候,我们就需要用到线段树中最常用的一种方法:延时标记。

(以下说明部分转载于 https://blog.csdn.net/u012860063/article/details/38322283)

区间更新是指更新某个区间内的叶子节点的值,因为涉及到的叶子节点不止一个,而叶子节点会影响其相应的非叶父节点,那么回溯需要更新的非叶子节点也会有很多,如果一次性更新完,操作的时间复杂度肯定不是O(lgn),例如当我们要更新区间[0,3]内的叶子节点时,需要更新除了叶子节点3,9外的所有其他节点。为此引入了线段树中的延迟标记概念,这也是线段树的精华所在。

延迟标记:每个节点新增加一个标记,记录这个节点是否进行了某种修改(这种修改操作会影响其子节点),对于任意区间的修改,我们先按照区间查询的方式将其划分成线段树中的节点,然后修改这些节点的信息,并给这些节点标记上代表这种修改操作的标记。在修改和查询的时候,如果我们到了一个节点p,并且决定考虑其子节点,那么我们就要看节点p是否被标记,如果有,就要按照标记修改其子节点的信息,并且给子节点都标上相同的标记,同时消掉节点p的标记。


如此看来,延时标记能大大减少更新操作中的时间损耗。比如线段树的区间为[1,10] 如果我们只想修改区间[1,10]的话,我们只需要对一号节点进行修改,不需要层层更新到单个节点。这样就节省了大量的时间。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN=100005;
LL num[MAXN];
struct Node
{
    int l,r;
    int len;
    LL sum;
    LL late;
} tree[MAXN<<2];
void pushup(int root) ///向上更新
{
    tree[root].sum = tree[root<<1].sum+tree[root<<1|1].sum;
}
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].sum=0;
    tree[root].len=r-l+1;///区间长度
    if(tree[root].l==tree[root].r)
    {
        tree[root].sum=num[l];
        return ;
    }
    int mid = (l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);

    pushup(root);
}
void pushdown(int root)///向下更新
{
    if(tree[root].late)///如果该节点被标记了
    {
        tree[root<<1].sum +=tree[root].late*tree[root<<1].len;
        tree[root<<1|1].sum+=tree[root].late*tree[root<<1|1].len;
        tree[root<<1].late+=tree[root].late;///标记也被子节点继承
        tree[root<<1|1].late+=tree[root].late;///标记也被子节点继承
        tree[root].late = 0;
    }///只向下更新一层
}
void update(int root,int l, int r,LL late)
{
    if(tree[root].l==l&&tree[root].r==r)///如果查询区间与节点区间相同,则直接更新该节点
    {
        tree[root].sum+=(tree[root].len*late);
        tree[root].late+=late;///给节点打上标记
        return ;
    }
    pushdown(root);///否则向下更新
    int mid = (tree[root].l+tree[root].r)>>1;
    if(mid>=r)
    {
        update(root<<1,l,r,late);
    }
    else if(l>mid)
    {
        update(root<<1|1,l,r,late);
    }
    else
    {
        update(root<<1,l,mid,late);
        update(root<<1|1,mid+1,r,late);
    }
    pushup(root);
}
LL query(int root,int l,int r)
{
   // printf("L==%d r==%d\n",l,r);
    if(tree[root].l==l&&tree[root].r==r)
    {
        return tree[root].sum;
    }
    pushdown(root);///查询时也要确定节点是否有标记
    int mid  = (tree[root].l+tree[root].r)>>1;
    if(mid>=r)
    {
        return query(root<<1,l,r);
    }
    else if(l>mid)
    {
        return query(root<<1|1,l,r);
    }
    else
    {
        return (query(root<<1,l,mid)+query(root<<1|1,mid+1,r));
    }
}
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&num[i]);
        }
        getchar();
        build(1,1,n);
        char c1[3];
        int x,y;
        LL c;
        for(int i=0; i<m; i++)
        {
            scanf("%s",&c1);

            if(c1[0]=='Q')
            {
                scanf("%d %d",&x,&y);
                printf("%lld\n",query(1,x,y)) ;
            }
            else
            {
                scanf("%d %d %lld",&x,&y,&c);
                update(1,x,y,c);
            }
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_37405320/article/details/80193490