poj-3468 A Simple Problem with Integers-线段树-整段区间增加求和

题目地址:http://poj.org/problem?id=3468

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
  
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


题目大意
N个数m个操作,包括两种,Q操作表示查询 x-y区间内的和,C操作表示把区间x-y中的每个数都加z

解题思路
线段树的应用,和上个文章差不多,都是延迟标记。这里需要注意的是数据范围,答案是 long long 型的。这里WA了好久T-T。

代码
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
long long add[400000];

struct node
{
    int  l,r;
    long long sum;
}tree[400000];

void buildtree(int node,int b,int e)
{
    int mid=(b+e)/2;
    tree[node].l=b;
    tree[node].r=e;
    tree[node].sum=0;
    if(b==e) return;
    if(b<=mid) buildtree(node*2,b,mid);
    if(e>mid) buildtree(node*2+1,mid+1,e);
}
void push_down(int node)
{
    if(add[node])
    {
        add[node*2]+=add[node];//标记左右儿子
        add[node*2+1]+=add[node];
        tree[node*2].sum+=add[node]*(tree[node*2].r-tree[node*2].l+1);//左右儿子的值要改变;
        tree[node*2+1].sum+=add[node]*(tree[node*2+1].r-tree[node*2+1].l+1);
        add[node]=0;//取消这个标记
    }

}

void update(int node,int ql,int qr,long long z)
{
    if(tree[node].l>=ql && tree[node].r<=qr)
    {
        add[node]+=z;
        tree[node].sum += z*(tree[node].r-tree[node].l+1);
        return;
    }
    push_down(node);//把这个标记传下去给左右儿子
    int mid=(tree[node].l+tree[node].r)/2;
    if(ql<=mid) update(node*2,ql,qr,z);
    if(qr>mid) update(node*2+1,ql,qr,z);
    tree[node].sum=tree[node*2].sum+tree[node*2+1].sum;//把当前更改的值传给父亲
}

long long query(int node,int ql,int qr)
{
    long long  ans1=0,ans2=0;
    if(tree[node].l>=ql && tree[node].r<=qr)
    {
        return tree[node].sum;
    }
    push_down(node);//把这个标记传下去给左右儿子
    int mid=(tree[node].l+tree[node].r)/2;
    if(mid>=ql) ans1 = query(node*2,ql,qr);
    if(mid<qr) ans2 =query(node*2+1,ql,qr);
    return ans1+ans2;
}


int main()
{
    int n,m,x,y;
    long long z;
    char c;

    scanf("%d%d",&n,&m);
    buildtree(1,1,n);
    for(int i=1;i<=n;++i)
    {
        cin>>z;
        update(1,i,i,z);
    }

    for(int i=1;i<=m;++i)
    {
        cin>>c;
        if(c == 'Q')
        {
            scanf("%d%d",&x,&y);
            cout<<query(1,x,y)<<endl;
        }
        else if(c=='C')
        {
            scanf("%d%d",&x,&y);
            cin>>z;
            update(1,x,y,z);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值