A Simple Problem with Integers (线段树模板)

 3468 -- A Simple Problem with Integers

A Simple Problem with Integers

Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 204955Accepted: 63007
Case Time Limit: 2000MS

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
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int inf = 100010;
int n , m ;
ll ly[inf<<2] , tree[inf<<2] ;
//用longlong类型,数较大时相加的和较大
//tree[] 用来实现完全二叉树 需要四倍空间
void pushup(int u)
{
    tree[u] = tree[u<<1] + tree[u<<1 | 1]; //更新结点 左右儿子的和
}
void buildtree(int l , int r , int u ) //用完全二叉树建一个线段树
{
//从根开始往下建树
    ly[u]=0;
    if( l == r ) //最底层叶子
    {
        cin>>tree[u];
        return ;
    }
    int mid = (l + r) >>1 ;
    buildtree(l , mid , u<<1); //左子树
    //右子树
    buildtree(mid + 1 , r , u<<1 | 1); //u<<1|1 <--> u*2+1 位运算计算更快
    //更新父节点
    pushup(u);
}
void pushdown(int u,int len)//下传lazy
{
    if(ly[u]){
        ly[u<<1] += ly[u];//父节点lazy下传到左儿子
        ly[u<<1 | 1] += ly[u];//到右儿子
        tree[u<<1] += (len - (len>>1)) * ly[u]; //左根节点加上(儿子每个数加上c的和)
        tree[u<<1 | 1] += (len>>1) * ly[u];
        ly[u] = 0;
    }
}

void update (int a , int b , int c , int l , int r , int u)//更新区间
{
    int len = r - l + 1;
    if( a <= l && b >= r){//区间已覆盖,直接计算
        tree[u] += len* c;
        ly[u] += c; //lazy标记加上c
        return ;
    }
    pushdown(u , len);//lazy下传
    int mid = (l + r) >>1 ;
    if(mid >= a) update(a , b , c , l , mid , u<<1);//更新左子树
    if(mid < b) update(a , b , c , mid+1 , r , u<<1 | 1);//更新右子树
    pushup(u);//更新父节点
}

ll query(int a, int b, int l, int r, int u)//计算区间和
{
    if(a <= l && b >= r) return tree[u];//满足lazy , 直接返回值
    int len = r - l + 1;
    pushdown(u , len);//下传
    int mid = (l + r) >>1 ;
    ll sum =0;
    if(mid >= a ) sum += query(a, b, l , mid , u<<1);
    if(mid < b) sum += query(a , b , mid+1 , r , u<<1 | 1);
    return sum;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    buildtree(1,n,1);
    while(m--)
    {
        char str;
        int a , b , c;
        cin>>str;
        if(str == 'C'){
            cin>>a>>b>>c;
            update(a,b,c,1,n,1);
        }else if(str == 'Q'){
            cin>>a>>b;
            cout<<query(a,b,1,n,1)<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值