线段树入门——poj3468 A Simple Problem with Integers

我做的第一道模板题
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 174351 Accepted: 53649
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 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
Hint

The sums may exceed the range of 32-bit integers.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e5+100;
typedef long long ll;
struct node
{
    ll  sum,lazy; //需要什么就维护什么。
}s[maxn<<2];
ll  n,m,w[maxn];
void update(ll root)
{
    s[root].sum=s[root<<1].sum+s[root<<1|1].sum; //用子节点来更新父亲节点
}
void build_tree(ll  root,ll  l,ll  r)
{
    s[root].lazy=0;
    if(r==l){
        s[root].sum=w[l];
        return;
    }
    ll mid=(l+r)>>1;
    build_tree(root<<1,l,mid);
    build_tree(root<<1|1,mid+1,r);
    update(root); //对父亲节点进行更新,就是该点本身;
}
void pushdown(ll root,ll l,ll r)  //下传lazy标记
{
    if(s[root].lazy==0)return;
    ll ch1=root<<1;
    ll ch2=root<<1|1;
    ll mid=(l+r)>>1;
    s[ch1].sum+=s[root].lazy*(mid-l+1);
    s[ch2].sum+=s[root].lazy*(r-mid);
    s[c1].lazy+=s[root].lazy;
    s[ch2].lazy+=s[root].lazy;
    s[root].lazy=0;
}
void change(ll root,ll l,ll r,ll ql,ll qr,ll pos)
{
    if(l==ql&&r==qr){
        s[root].sum+=pos*(r-l+1);
        s[root].lazy+=pos;
        return ;
    }
    pushdown(root,l,r);
    ll mid=(l+r)>>1;
    if(qr<=mid)
        change(root<<1,l,mid,ql,qr,pos);
    else if((ql>mid))
        change(root<<1|1,mid+1,r,ql,qr,pos);
    else{
        change(root<<1,l,mid,ql,mid,pos);
        change(root<<1|1,mid+1,r,mid+1,qr,pos);
    }
    update(root);
}
ll query(ll root,ll l,ll r,ll ql,ll qr)
{
    if(l==ql&&qr==r)
        return s[root].sum;
    ll mid=(l+r)>>1;
    pushdown(root,l,r);
    if(mid>=qr){
        return query(root<<1,l,mid,ql,qr);
    }
    else if(mid<ql){
        return query(root<<1|1,mid+1,r,ql,qr);
    }
    else
        return query(root<<1,l,mid,ql,mid)+query(root<<1|1,mid+1,r,mid+1,qr);
}
int main()
{
    while(~scanf("%lld %lld",&n,&m)){
        for(ll i=1;i<=n;i++){
            scanf("%lld",&w[i]);
        }
        build_tree(1,1,n);
        for(ll i=1;i<=m;i++){
            char q;
            cin>>q;
            if(q=='Q'){
                ll l,r;
                scanf("%lld %lld",&l,&r);
                printf("%lld\n",query(1,1,n,l,r));
            }
            else{
                ll l,r,pos;
                scanf("%lld %lld %lld",&l,&r,&pos);
                change(1,1,n,l,r,pos);
            }
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值