【poj3468】A Simple Problem with Integers

题面

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.

题解

线段树模板

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100010;
typedef long long LL;

LL a[maxn];
struct node{
    int l, r;
    LL val, addmark;
}sgt[maxn<<2];
void build(int p, int l, int r){
    sgt[p].l = l, sgt[p].r = r;
    if(l == r){
        sgt[p].val = a[l];
    }else{
        int m = (l+r)/2;
        build(p*2,l,m);
        build(p*2+1,m+1,r);
        sgt[p].val = sgt[p*2].val+sgt[p*2+1].val;
    }
}
void pushdown(int p){
    if(sgt[p].addmark != 0){
        LL t = sgt[p].addmark;
        sgt[p].addmark = 0;
        sgt[p*2].addmark += t;
        sgt[p*2+1].addmark += t;
        sgt[p*2].val += (sgt[p*2].r-sgt[p*2].l+1)*t;
        sgt[p*2+1].val += (sgt[p*2+1].r-sgt[p*2+1].l+1)*t;
    }
}
void add(int p, int l, int r, LL v){
    if(l <= sgt[p].l && sgt[p].r <= r){
        sgt[p].val += (sgt[p].r-sgt[p].l+1)*v;
        sgt[p].addmark += v;
        return ;
    }
    pushdown(p);
    int m = (sgt[p].l+sgt[p].r)/2;
    if(l <= m)add(p*2,l,r,v);
    if(r > m)add(p*2+1,l,r,v);
    sgt[p].val = sgt[p*2].val+sgt[p*2+1].val;
}
LL query(int p, int l, int r){
    if(l <= sgt[p].l && sgt[p].r <= r)return sgt[p].val;
    pushdown(p); //pushdown
    LL m = (sgt[p].l+sgt[p].r)/2, ans = 0;
    if(l <= m)ans += query(p*2,l,r);
    if(r > m)ans += query(p*2+1,l,r);
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= n; i++)cin>>a[i];
    build(1,1,n);
    for(int i = 1; i <= m; i++){
        char ch;  cin>>ch;
        if(ch == 'Q'){
            LL x, y;  cin>>x>>y;
            cout<<query(1,x,y)<<"\n";
        }else{
            LL x, y, z;  cin>>x>>y>>z;
            add(1,x,y,z);
        }
    }
    return 0;
}

关于线段树2*n空间表示法。
推荐博客http://www.cppblog.com/MatoNo1/archive/2015/05/05/195857.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小哈里

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

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

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

打赏作者

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

抵扣说明:

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

余额充值