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

119 篇文章 1 订阅
113 篇文章 1 订阅

A Simple Problem with Integers 链接:传送门

You have NN integers, A1,A2,⋯,ANA1,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
There is only one testcase.

The first line contains two numbers NN and QQ. 1≤N,Q≤1000001≤N,Q≤100000.

The second line contains NN numbers, the initial values of A1,A2,⋯,ANA1,A2,⋯,AN. −1000000000≤Ai≤1000000000−1000000000≤Ai≤1000000000.

Each of the next QQ lines represents an operation.

C a b c means adding cc to each of Aa,Aa+1,⋯,AbAa,Aa+1,⋯,Ab. −10000≤c≤10000−10000≤c≤10000.
Q a b means querying the sum of Aa,Aa+1,⋯,AbAa,Aa+1,⋯,Ab.
Output
You need to answer all QQ 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 3232-bit integers.

The data used in this problem is unofficial data prepared by standy. So any mistake here does not imply mistake in the offcial judge data.

思路:较简单的区间更新和区间求和。

#include<stdio.h>
#include<string.h>
#define N 100009

struct node
{
    int l,r;
    long long p;
    long long xia;
} tree[N*3+10];

int a[N];
void build(int num,int l,int r)//建树
{
    tree[num].l=l;
    tree[num].r=r;
    tree[num].xia=0;
    if(l==r)
    {
        tree[num].p=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
    tree[num].p=tree[num<<1].p+tree[num<<1|1].p;
}
void cont(int num)//再次遇见以前要更新的区间时,将左右子区间更新
{
    if(tree[num].xia)
    {
        int l=tree[num].l,r=tree[num].r,mid=(l+r)/2;
        long long add=tree[num].xia;
        tree[num<<1].p+=(mid-l+1)*add;
        tree[num<<1|1].p+=(r-mid)*add;
        tree[num<<1].xia+=add;
        tree[num<<1|1].xia+=add;
        tree[num].xia=0;
    }
}
long long query(int num,int x,int y)//区间求和
{
    int l=tree[num].l,r=tree[num].r,mid=(l+r)/2;
    if(l>=x&&y>=r)
        return tree[num].p;
    cont(num);
    long long sum=0;
    if(mid>=x) sum+=query(num<<1,x,y);
    if(mid<y) sum+=query(num<<1|1,x,y);
    tree[num].p=tree[num<<1].p+tree[num<<1|1].p;
    return sum;
}
void update(int x,int y,long long z,int num)//区间更新
{
    int l=tree[num].l,r=tree[num].r,mid=(l+r)/2;
    if(x<=l&&r<=y)//现将需要更新的区间记录下来,再次遇到时用void cont(int num)函数处理
    {
        tree[num].p+=(r-l+1)*z;
        tree[num].xia+=z;
        return ;
    }
    cont(num);//重要的一步
    if(mid>=x) update(x,y,z,num<<1);
    if(mid<y) update(x,y,z,num<<1|1);
    tree[num].p=tree[num<<1].p+tree[num<<1|1].p;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        char b[2];
        int x,y;
        long long z;
        while(m--)
        {
            scanf("%s",b);
            if(b[0]=='Q')
            {
                scanf("%d%d",&x,&y);
                printf("%lld\n",query(1,x,y));
            }
            else
            {
                scanf("%d%d%lld",&x,&y,&z);
                update(x,y,z,1);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值