线段树成段更新 POJ 3468 A Simple Problem with Integers 水题

题目


A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 116048 Accepted: 36055
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


题意:

题意:题目给你n个数,m个操作,接下来一行给你这n个数,接下的几行给出m个操作,Q a b 表示查询区间[a,b]里的数和和。U a b c 表示把区间[a,b]里的数都加上c。


做法:

线段树成段更新(延迟标记),对于当前区间,如果范围包含于更新的区间,那么停止访问子结点(如果以后都不用到子结点的值,则可以这样),并且在当前区间进行延迟标记(此题对当前区间的lazy值进行+=操作)。
如果在更新或遍历时,发现当前区间并不完全是所需的区间(还需继续遍子结点),那么为了保证子结点的取值正确,那么必须进行pushDown操作,即把当前结点的lazy值更新到子结点,并在子结点上进行延迟标记。


代码

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
#define lson  (ind<<1)
#define rson  (ind<<1|1)
#define MID   int mid=(le+ri)>>1
typedef long long ll;
//const int INF = 0x3f3f3f3f ;
const int maxn=100000;
int a[maxn+5];
int n,Q;
struct Node
{
    int le,ri,lazy;
    ll sum;

    void fun(int add)
    {
        sum+= (ll)(ri-le+1)*add;
        lazy+=add;
    }
};
struct SegMent
{
    Node C[4*maxn];
    void build(int ind,int le,int ri)
    {
        C[ind].le=le,C[ind].ri=ri;
        C[ind].lazy=0;
        if(le==ri)
        {
            C[ind].sum=a[le];
            return;
        }
        MID;
        build(lson,le,mid);
        build(rson,mid+1,ri);
        C[ind].sum=C[lson].sum+C[rson].sum;
    }

    void pushDown(int ind)
    {
        if(C[ind].lazy)
        {
            C[lson].fun(C[ind].lazy);
            C[rson].fun(C[ind].lazy);
            C[ind].lazy=0;
        }
    }
    void update(int ind,int L,int R,int add)
    {
        int le=C[ind].le,ri=C[ind].ri;

        if(L<=le&&ri<=R)//该区域符合条件,sum值正确,不用pushdown操作
        {
            C[ind].fun(add);
            return;
        }
        pushDown(ind);//因为后面要访问子结点,必须pushdown

        MID;
        if(L<=mid) update(lson,L,R,add);
        if(R>mid)  update(rson,L,R,add);

        C[ind].sum=C[lson].sum+C[rson].sum;
    }
    ll query(int ind,int L,int R)
    {
        int le=C[ind].le,ri=C[ind].ri;
        if(L<=le&&ri<=R)
        {
            return C[ind].sum;
        }
        pushDown(ind);
        MID;
        ll ans=0;
        if(L<=mid) ans+=query(lson,L,R);
        if(R>mid)  ans+=query(rson,L,R);
        return ans;
    }
}sgt;
int main()
{
    std::ios::sync_with_stdio(false);
    while(cin>>n>>Q)
    {
        for1(i,n) cin>>a[i];
        sgt.build(1,1,n);

        char op;
        int x,y,z;
        for1(i,Q)
        {
            cin>>op;
            if(op=='Q')
            {
                cin>>x>>y;
                printf("%lld\n",sgt.query(1,x,y));
            }
            else
            {
                cin>>x>>y>>z;
                sgt.update(1,x,y,z);
            }
        }

    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值