poj 3468 线段树区间更新

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 108802 Accepted: 33885
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , 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.


对线段树区间更新慢慢理解了...

还是去网上搜了一下  套用了别人的一个模板 个人觉得这个模板很好用

模板链结

#include<cstdio>
#include<iostream>
#include<cstring>
#define MAX  8005
using namespace std;
typedef long long ll;
struct node{
ll l,r;
ll val;
ll add;
}nd[100000*4];
ll ans=0;
void build(ll l,ll r,ll k)
{
    nd[k].l=l;
    nd[k].r=r;
    nd[k].add=0;
    if(l==r)
    {
        scanf("%lld",&nd[k].val);
        return ;
    }
    ll mid=(l+r)/2;
    build(l,mid,2*k);
    build(mid+1,r,2*k+1);
    nd[k].val=nd[k*2].val+nd[k*2+1].val;
}
void pushdown(ll k)
{
    nd[2*k].add+=nd[k].add;
    nd[2*k+1].add+=nd[k].add;
    nd[2*k].val+=(nd[2*k].r-nd[2*k].l+1)*nd[k].add;
    nd[2*k+1].val+=(nd[2*k+1].r-nd[2*k+1].l+1)*nd[k].add;
    nd[k].add=0;
}
void update(ll l,ll r,ll c,ll k)
{
    if(nd[k].l>r||nd[k].r<l)
    return ;
    if(nd[k].l>=l&&nd[k].r<=r)
    {
        nd[k].add+=c;
        nd[k].val+=(nd[k].r-nd[k].l+1)*c;
        return ;
    }
    if(nd[k].add)
    {
        pushdown(k);
    }
    ll mid=(nd[k].l+nd[k].r)/2;
    if(r<=mid)update(l,r,c,k*2);
    else if(l>mid)update(l,r,c,2*k+1);
    else
    {
        update(l,mid,c,2*k);
        update(mid+1,r,c,2*k+1);
    }
    nd[k].val=nd[2*k].val+nd[2*k+1].val;  //所谓的pushup()
}
void query(ll l,ll r,ll k)
{
    if(nd[k].l>r||nd[k].r<l)
        return ;
    if(nd[k].l>=l&&nd[k].r<=r)
    {
        ans+=nd[k].val;
        return ;
    }
    if(nd[k].add)
    {
        pushdown(k);
    }
    int mid=(nd[k].r+nd[k].l)/2;
    if(r<=mid)query(l,r,2*k);
    else if(l>mid)query(l,r,2*k+1);
    else
    {
        query(l,mid,2*k);
        query(mid+1,r,2*k+1);
    }

}
int main()
{
    ll i,j,k,m,n;
    ll Q;
    char s1[5];
    while(scanf("%lld%lld",&n,&Q)==2)
    {
        build(1,n,1);
        ll a,b,c;
        while(Q--)
        {
            scanf("%s",s1);
            if(s1[0]=='C')
            {
                scanf("%lld%lld%lld",&a,&b,&c);
                update(a,b,c,1);
            }
            else
            {
                ans=0;
                scanf("%lld%lld",&a,&b);
                query(a,b,1);
                printf("%lld\n",ans);
            }
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值