poj3468 线段树+lazy标记

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 92921 Accepted: 28910
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.
题目大意:
给你n个数,然后会进行q次操作,Q a b为查询区间[a,b]所有数的和sum,
C a b c ,为将区间[a,b]所有数整体加c。
思路分析:如果只是改变某一个数的值,我们直接用一个裸的线段树来维护就行,但是这个题是
区间的值整体发生变化,如果再进行和以前一样的操作,则update的复杂度变成了LlogL(L为
区间长度)很不高效,势必超时,因此我们可以采用lazy标记的方法,用空间换时间,开一个lazy
数组,每次不必访问到根节点,只需要到更新的区间所在的节点就可以,大大提高了程序效率,只需要
明确lazy标记什么时候往儿子传就可以,一个是要往子节点更新,另一个是即将要查询子节点,都要将
lazy标记向下传一层,传给自己的儿子,增值会爆int,需要用long long
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=100000+100;
typedef long long ll;
ll tree[3*maxn];
ll lazy[3*maxn];
ll num[maxn];
void build (int p,int l,int r)
{
    if(l==r) {tree[p]=num[l];return;}
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build((p<<1)|1,mid+1,r);
    tree[p]=tree[p<<1]+tree[(p<<1)|1];
}
void pushdown(int p,int m)//把lazy标记下放到儿子节点
{
    if(lazy[p])
    {
        lazy[p<<1]+=lazy[p];
        lazy[(p<<1)|1]+=lazy[p];
        tree[p<<1]+=(m-(m>>1))*lazy[p];
        tree[(p<<1)|1]+=(m>>1)*lazy[p];
        lazy[p]=0;
    }
}
void update(int p,int l,int r,int x,int y,int v)
{
    if(x<=l&&y>=r)
    {
        lazy[p]+=v;
        tree[p]+=(ll)v*(r-l+1);//区间长度
        return;
    }
    pushdown(p,r-l+1);
    int mid=(l+r)>>1;
    if(y<=mid) update(p<<1,l,mid,x,y,v);
    else if(x>mid) update((p<<1)|1,mid+1,r,x,y,v);
    else {update(p<<1,l,mid,x,mid,v),update((p<<1)|1,mid+1,r,mid+1,y,v);}
    tree[p]=tree[p<<1]+tree[(p<<1)|1];
}
ll find(int p,int l,int r,int x,int y)
{
    if(x<=l&&y>=r){return tree[p];}
    pushdown(p,r-l+1);
    int mid=(l+r)>>1;
    ll ans=0;
    if(y<=mid) ans=find(p<<1,l,mid,x,y);
    else if(x>mid) ans=find((p<<1)|1,mid+1,r,x,y);
    else ans=find(p<<1,l,mid,x,mid)+find((p<<1)|1,mid+1,r,mid+1,y);
    return ans;
}
int main()
{
     int n,q;
     char s[5];
     int a,b,c;
    while(~scanf("%d%d",&n,&q))
    {
        memset(tree,0,sizeof(tree));
        memset(lazy,0,sizeof(lazy));
        for(int i=1;i<=n;i++)
            scanf("%lld",&num[i]);
        build(1,1,n);
        while(q--)
        {
            scanf("%s",s);
            if(s[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                printf("%lld\n",find(1,1,n,a,b));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                update(1,1,n,a,b,c);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/xuejianye/p/5692325.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值