poj-3468 A Simple Problem with Integers(线段树区间更新模板题)

本文介绍了一种使用线段树进行区间加法更新和区间求和查询的高效算法。该算法适用于处理大规模数据集上的批量操作,能够显著提高计算效率。

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 AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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的数组,C时在下标a,b内加c,Q时求小标a,b内数的和;

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=1e5+5;
typedef long long ll;
ll a[MAX];
struct Node
{
    ll add;
    int l;
    int r;
    ll val;
}tree[4*MAX];
void pushUp(int root)
{
    tree[root].val=tree[root*2].val+tree[root*2+1].val;
}
void build(int root,int L,int R)
{
    tree[root].add=0;
    tree[root].l=L;
    tree[root].r=R;
    if(L==R)
    {
        tree[root].val=a[L];
        return;
    }
    int mid=(L+R)/2;
    build(root*2,L,mid);
    build(root*2+1,mid+1,R);
    pushUp(root);
}
void pushDown(int root)
{
    if(tree[root].add!=0)
    {
        tree[root*2].add+=tree[root].add;
        tree[root*2+1].add+=tree[root].add;
        tree[root*2].val+=tree[root].add*(tree[root*2].r-tree[root*2].l+1);
        tree[root*2+1].val+=tree[root].add*(tree[root*2+1].r-tree[root*2+1].l+1);
        tree[root].add=0;
    }
}
void updateSome(int root,int L,int R,int addNum)
{
    if(L<=tree[root].l&&R>=tree[root].r)
    {
        tree[root].add+=addNum;
        tree[root].val+=addNum*(tree[root].r-tree[root].l+1);
        return ;
    }
    pushDown(root);
    int mid=(tree[root].l+tree[root].r)/2;
    if(L<=mid)updateSome(root*2,L,R,addNum);
    if(R>mid)updateSome(root*2+1,L,R,addNum);
    pushUp(root);
    return ;
}
ll query(int root,int L,int R)
{
    if(L<=tree[root].l&&R>=tree[root].r)
    {
        return tree[root].val;
    }
    if(R<tree[root].l||L>tree[root].r)
        return 0;
    pushDown(root);
    int mid=(tree[root].l+tree[root].r)/2;
    ll ans=0;
    if(L<=mid)ans+=query(root*2,L,R);
    if(R>mid)ans+=query(root*2+1,L,R);
    return ans;
}
int main()
{
    int n,q;
    while(cin>>n>>q)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        build(1,1,n);
        while(q--)
        {
            char que;
            int x,y;
            cin>>que>>x>>y;
            if(que=='Q')
            {
               cout<<query(1,x,y)<<endl;
            }else
            {
                ll addNum;
                cin>>addNum;
                updateSome(1,x,y,addNum);
            }
        }
    }
    return 0;
}
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll n,q;
ll a[maxn];
struct Node
{
     ll sum;
     ll add;
     ll l;
     ll r;
     Node()
     {
         sum=0;
         add=0;
     }
}tree[4*maxn];
void _sum(ll root)
{
    tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}
void _down(ll root)
{
    if(tree[root].add!=0)
    {
        tree[root*2].sum+=(tree[root*2].r-tree[root*2].l+1)*tree[root].add;
        tree[root*2].add+=tree[root].add;
        tree[root*2+1].sum+=(tree[root*2+1].r-tree[root*2+1].l+1)*tree[root].add;
        tree[root*2+1].add+=tree[root].add;
        tree[root].add=0;
    }
    return ;
}
void build(ll root,ll L,ll R)
{
    tree[root].l=L;
    tree[root].r=R;
    if(L==R)
    {
        tree[root].sum=a[L];
        return ;
    }
    ll mid=(L+R)/2;
    build(root*2,L,mid);
    build(root*2+1,mid+1,R);
    _sum(root);
}
void update(ll root,ll L,ll R,ll addNum)
{
    if(L<=tree[root].l&&R>=tree[root].r)
    {

        tree[root].sum+=(tree[root].r-tree[root].l+1)*addNum;
        tree[root].add+=addNum;
        return ;
    }else if(R<tree[root].l||L>tree[root].r)return ;
    else
    {
        _down(root);
        update(root*2,L,R,addNum);
        update(root*2+1,L,R,addNum);
        _sum(root);
    }
    return ;
}
ll query(ll root,ll L,ll R)
{
    if(L<=tree[root].l&&R>=tree[root].r)
    {
       return tree[root].sum;
    }else if(L>tree[root].r||R<tree[root].l)return 0;
    else
    {
        _down(root);
        ll temp=0;
        temp+=query(root*2,L,R);
        temp+=query(root*2+1,L,R);
        return temp;
    }
}
int main()
{

    while(~scanf("%lld%lld",&n,&q))
    {
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        build(1,1,n);

        while(q--)
        {
            getchar();
            char op;
            ll x,y;
            scanf("%c %lld %lld",&op,&x,&y);
            if(op=='Q')printf("%lld\n",query(1,x,y));
            else
            {
                ll addNum;
                scanf("%lld",&addNum);
                update(1,x,y,addNum);
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值