A Simple Problem with Integers (线段树 ) 区间更新 求和

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

Source


题意
给出n的数和吗中访问
Q a b   求a到b的和,C a b c 也即是在a到b区间的每个数加上c;
对于没个Q输出结果
典型的线段树区间更新 求和的问题;用线段树的模版;

#include <iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;


const int N=1000005;
struct node{
ll x,mark;
}t[N<<2];


void creat(int i,int l,int r)
{
    t[i].mark=0ll;
    if(l==r)
    {
        scanf("%lld",&t[i].x);
        return;
    }
    int mid=(l+r)>>1;
    creat(i<<1,l,mid);
    creat(i<<1|1,mid+1,r);
    t[i].x=t[i<<1].x+t[i<<1|1].x;
}


void pushdown(int i,int l,int r)
{
    if(t[i].mark)
    {
        int mid=(l+r)>>1;
        t[i<<1].mark+=t[i].mark;
        t[i<<1|1].mark+=t[i].mark;
        t[i<<1].x+=t[i].mark*(mid-l+1);
        t[i<<1|1].x+=t[i].mark*(r-mid);
        t[i].mark=0;
    }
}


void update(int i,int l,int r,int x,int y, ll z)
{
    if(r<x||y<l) return ;
    if(x<=l&&r<=y)
    {
        t[i].mark+=z;
        t[i].x+=z*(r-l+1);
        return ;
    }
    pushdown(i,l,r);
    int mid=(l+r)>>1;
    update(i<<1,l,mid,x,y,z);
    update(i<<1|1,mid+1,r,x,y,z);
    t[i].x=t[i<<1].x+t[i<<1|1].x;


}
ll sum(int i,int l,int r,int x,int y)
{
    if(r<x||y<l) return 0;
    if(x<=l&&r<=y) return t[i].x;
    pushdown(i,l,r);
    int mid=(l+r)>>1;
    return sum(i<<1,l,mid,x,y)+sum(i<<1|1,mid+1,r,x,y);
}
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    creat(1,1,n);
    while(m--)
    {
        char c;
        int a,b;
        ll d;
        scanf(" %c",&c);
        if(c=='Q')
        {
            scanf("%d%d",&a,&b);
            printf("%lld\n",sum(1,1,n,a,b));
            continue;
        }
        scanf("%d%d%lld",&a,&b,&d);
        update(1,1,n,a,b,d);
    }
    return 0;
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

じ☆夏妮国婷☆じ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值