UESTC Training for Data Structures——M

Problem  M

Problem 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

 

/*用线段树维护,使用懒操作*/

#include<iostream>
#include<cstring>
#include<cmath>
#include<stdio.h>
#define N 100005
using namespace std;
struct data
{
    int be,en;
    int left,right;
    __int64 bj,sum;
}tree[4*N];
int a[N],tot;
void make_tree(int st,int en)
{
    int now=++tot;
    tree[now].be=st,tree[now].en=en;
    if(st==en) tree[now].sum=a[st];
    else
    {
        int mid=(st+en)/2;
        tree[now].left=tot+1;
        make_tree(st,mid);
        tree[now].right=tot+1;
        make_tree(mid+1,en);
        tree[now].sum=tree[tree[now].left].sum+tree[tree[now].right].sum;
    }
}
void clear(int v)  //传递父亲的标记给孩子
{
    tree[tree[v].left].bj+=tree[v].bj;
    tree[tree[v].right].bj+=tree[v].bj;
    tree[v].sum+=((__int64)(tree[v].en-tree[v].be+1))*tree[v].bj;
    tree[v].bj=0;
}
void add(int v,int st,int en,__int64 k)   //更新区间
{
    if(tree[v].be>=st && tree[v].en<=en)
    {
        tree[v].bj+=k;
        return;
    }
    if(tree[v].bj!=0) clear(v);
    if(tree[v].be==tree[v].en) return;
    int mid=(tree[v].be+tree[v].en)>>1;
    if(st>mid) add(tree[v].right,st,en,k);
    else if(en<=mid) add(tree[v].left,st,en,k);
    else
    {
        add(tree[v].right,st,en,k);
        add(tree[v].left,st,en,k);
    }
    if(tree[tree[v].left].bj) clear(tree[v].left);
    if(tree[tree[v].right].bj) clear(tree[v].right);
    tree[v].sum=tree[tree[v].left].sum+tree[tree[v].right].sum;
}
__int64 qusum(int v,int st,int en)  //取和
{

    if(st<=tree[v].be && en>=tree[v].en)
    {
        if(tree[v].bj!=0) clear(v);
        return tree[v].sum;
    }
    if(tree[v].bj!=0) clear(v);
    if(tree[v].be==tree[v].en) return 0;
    int mid=(tree[v].be+tree[v].en)>>1;
    if(en<=mid) return qusum(tree[v].left,st,en);
    if(st>mid) return qusum(tree[v].right,st,en);
    return qusum(tree[v].left,st,mid)+qusum(tree[v].right,mid+1,en);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    memset(a,0,sizeof(a));
    memset(tree,0,sizeof(tree));
    tot=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    make_tree(1,n);
    char s[10]; getchar(); //gets(s);
    for(int i=1;i<=m;i++)
    {
        char ch; __int64 c; int k,m;
        scanf("%c%d%d",&ch,&k,&m);
        if(ch=='Q') printf("%I64d\n",qusum(1,k,m));
        else
        {
            scanf("%I64d",&c);
            add(1,k,m,c);
        }
        getchar();
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值