[bzoj3212][Pku3468][线段树]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 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.

题解

你拍一我拍一大家一起刷水题
区间修改区间求和线段树搞定

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
struct node
{
    int lc,rc,l,r;LL c,lazy;
    bool ch;
}tr[210000];int trlen;
void upd(int now)
{
    int lc=tr[now].lc,rc=tr[now].rc;
    tr[lc].c+=(LL)tr[now].lazy*(tr[lc].r-tr[lc].l+1);tr[lc].lazy+=tr[now].lazy,tr[lc].ch=true;
    tr[rc].c+=(LL)tr[now].lazy*(tr[rc].r-tr[rc].l+1);tr[rc].lazy+=tr[now].lazy,tr[rc].ch=true;
    tr[now].lazy=0;tr[now].ch=false;
}
void bt(int l,int r)
{
    int now=++trlen;
    tr[now].l=l;tr[now].r=r;
    tr[now].lc=tr[now].rc=-1;tr[now].c=tr[now].lazy=0;
    tr[now].ch=false;
    if(l<r)
    {
        int mid=(l+r)/2;
        tr[now].lc=trlen+1;bt(l,mid);
        tr[now].rc=trlen+1;bt(mid+1,r);
    }
}
void change(int now,int l,int r,LL c)
{
    if(tr[now].l==l && tr[now].r==r)
    {
        tr[now].c+=(LL)c*(tr[now].r-tr[now].l+1);
        tr[now].lazy+=c;tr[now].ch=true;
        return ;
    }
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(tr[now].ch)upd(now);
    if(r<=mid)change(lc,l,r,c);
    else if(mid+1<=l)change(rc,l,r,c);
    else change(lc,l,mid,c),change(rc,mid+1,r,c);
    tr[now].c=tr[lc].c+tr[rc].c;
}
LL findsum(int now,int l,int r)
{
    if(tr[now].l==l && tr[now].r==r)return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(tr[now].ch)upd(now);
    if(r<=mid)return findsum(lc,l,r);
    else if(mid+1<=l)return findsum(rc,l,r);
    else return findsum(lc,l,mid)+findsum(rc,mid+1,r);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);bt(1,n);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        change(1,i,i,x);
    }
    while(m--)
    {
        char ss[10];
        int u,v,c;
        scanf("%s%d%d",ss+1,&u,&v);
        if(ss[1]=='Q')printf("%lld\n",findsum(1,u,v));
        else 
        {
            scanf("%d",&c);
            change(1,u,v,c);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值