poj 3468 A Simple Problem with Integers(区间更新 区间求和)

本文介绍了一种使用线段树解决区间加法与查询问题的方法。通过对线段树的懒惰传播优化,实现了高效的区间更新与求和操作。适用于处理大规模数据集上的区间操作。

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

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 80012 Accepted: 24687
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.



给出N个数的初始值  两种操作 一种是把一个区间内所有的所有的数都加c 一种是求出一个区间的和

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct node
{
    ll l,r;
    ll lazy,num;
};
node no[MAXN*4];
ll a[MAXN];

void build(ll l,ll r,ll k)
{
    if(l==r)
    {
        no[k].l=l;
        no[k].r=r;
        no[k].num=a[l];
        no[k].lazy=0;
        return;
    }
    ll mid=(l+r)/2;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
    no[k].l=l;
    no[k].r=r;
    no[k].lazy=0;
    no[k].num=no[k*2].num+no[k*2+1].num;
}

void pushdown(ll k)
{
    if(no[k].lazy)
    {
        no[k*2].lazy+=no[k].lazy;
        no[k*2].num+=(no[k*2].r-no[k*2].l+1)*no[k].lazy;
        no[k*2+1].lazy+=no[k].lazy;
        no[k*2+1].num+=(no[k*2+1].r-no[k*2+1].l+1)*no[k].lazy;
        no[k].lazy=0;
        return;
    }
}

void update(ll l,ll r,ll num,ll k)
{
    if(no[k].l>=l&&no[k].r<=r)
    {
        ll len=no[k].r-no[k].l+1;
        no[k].num+=len*num;
        no[k].lazy+=num;
        return;
    }
    pushdown(k);
    ll mid=(no[k].r+no[k].l)/2;
    if(r<=mid) update(l,r,num,k*2);
    else if(l>mid) update(l,r,num,k*2+1);
    else
    {
        update(l,mid,num,k*2);
        update(mid+1,r,num,k*2+1);
    }
    no[k].num=no[k*2].num+no[k*2+1].num;
}

ll ans;
void query(ll l,ll r,ll k)
{
    if(no[k].l>=l&&no[k].r<=r)
    {
        ans+=no[k].num;
        return;
    }
    pushdown(k);
    int mid=(no[k].l+no[k].r)/2;
    if(r<=mid) query(l,r,k*2);
    else if(l>mid) query(l,r,k*2+1);
    else
    {
        query(l,mid,k*2);
        query(mid+1,r,k*2+1);
    }
    no[k].num=no[k*2].num+no[k*2+1].num;
}

int main()
{
//    fread;
    ll n,q;
    while(scanf("%I64d%I64d",&n,&q)!=EOF)
    {
//        cout<<n<<q<<endl;
        for(ll i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        build(1,n,1);
        while(q--)
        {
            char ch[10];
            ll l,r,num;
            scanf("%s",ch);
            if(ch[0]=='C')
            {
                scanf("%I64d%I64d%I64d",&l,&r,&num);
                update(l,r,num,1);
            }
            else
            {
                scanf("%I64d%I64d",&l,&r);
//                cout<<l<<"  "<<r<<endl;
                ans=0;
                query(l,r,1);
                printf("%I64d\n",ans);
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值