A Simple Problem with Integers (线段树应用类型三)【区间增加 区间查询】【模板基础题】

本文介绍了一种处理区间加法和区间求和的算法实现,通过构建线段树来优化大量查询和修改操作,适用于数据范围大的场景。文章提供了完整的C++代码示例,包括线段树的建立、更新和查询过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=3468

参考博客:https://blog.youkuaiyun.com/u013480600/article/details/22202711

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

 题意:增加给定区间的值,查询区间和。(注意数据大小)

//POJ 3468 区间add,区间查询
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

//每当有add加到i节点上,直接更新i节点的sum.
//也就是说如果要查询区间[1,n]的sum值,直接sum[1]即可,不用再去考虑1的addv[1]值.
const int MAXN=100000+100;
#define LL long long
#define lson i*2,l,m
#define rson i*2+1,m+1,r
LL sum[MAXN*4];
LL addv[MAXN*4];
void PushDown(int i,int num)
{
    if(addv[i])
    {
        sum[i*2] +=addv[i]*(num-(num/2));
        sum[i*2+1] +=addv[i]*(num/2);
        addv[i*2] +=addv[i];
        addv[i*2+1]+=addv[i];
        addv[i]=0;
    }
}
void PushUp(int i)
{
    sum[i]=sum[i*2]+sum[i*2+1];
}
void build(int i,int l,int r)
{
    addv[i]=0;
    if(l==r)
    {
        scanf("%lld",&sum[i]);
        return ;
    }
    int m=(l+r)/2;
    build(lson);
    build(rson);
    PushUp(i);
}
void update(int ql,int qr,int add,int i,int l,int r)
{
    if(ql<=l&&r<=qr)
    {
        addv[i]+=add;
        sum[i] += (LL)add*(r-l+1);
        return ;
    }
    PushDown(i,r-l+1);
    int m=(l+r)/2;
    if(ql<=m) update(ql,qr,add,lson);
    if(m<qr) update(ql,qr,add,rson);
    PushUp(i);
}
LL query(int ql,int qr,int i,int l,int r)
{
    if(ql<=l&&r<=qr)
    {
        return sum[i];
    }
    PushDown(i,r-l+1);
    int m=(l+r)/2;
    LL res=0;
    if(ql<=m) res+=query(ql,qr,lson);
    if(m<qr) res+=query(ql,qr,rson);
    return res;
}
int main()
{
        int n,q;
        scanf("%d%d",&n,&q);
        build(1,1,n);

        char s1[5];
        for(int i=1;i<=q;i++)
        {
            scanf("%s",s1);
            if(s1[0]=='Q')
            {
                int a,b;
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(a,b,1,1,n));
            }
            else
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,1,n);
            }
        }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会敲代码的小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值