POJ 3468 A Simple Problem with Integers 线段树 区间更新

原题: http://poj.org/problem?id=3468

题目:

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 78207 Accepted: 24097
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.

思路:

直接套模版。
注意数据会int溢出。

代码:

#include <iostream>
#include"cstdio"
#include"string.h"
using namespace std;
typedef long long int lint;
const int N= 100005;
struct node
{
    int left;
    int right;
    lint add;
    lint sum;
} tree[N*4];

int n,m;
lint a[N];

void init()
{
    memset(tree,0,sizeof(tree));
    memset(a,0,sizeof(a));
}

//将下面更新的值返回上一层
void pushup(int id)
{
    tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;
}

//将标记区间向下移动
void pushdown(int id)
{
    tree[id*2].add=tree[id*2].add+tree[id].add;
    tree[id*2].sum=tree[id*2].sum+(tree[id*2].right-tree[id*2].left+1)*tree[id].add;
    tree[id*2+1].add=tree[id*2+1].add+tree[id].add;
    tree[id*2+1].sum=tree[id*2+1].sum+(tree[id*2+1].right-tree[id*2+1].left+1)*tree[id].add;
    tree[id].add=0;
}

//建树的时候每次建完下一层要向上层赋值
void build(int id,int l,int r)
{
    tree[id].left=l;
    tree[id].right=r;
    tree[id].add=0;
    tree[id].sum=0;
    if(l==r)
    {
        tree[id].sum=a[l];
        return;
    }
    int mid=(l+r)/2;
    build(id*2,l,mid);
    build(id*2+1,mid+1,r);
    pushup(id);
}


void add(int id,int l,int r,lint val)
{
    //如果当前区间完全被包含,只更新当前区间的总和
    //并对该区间做增加标记
    if(tree[id].left>=l&&tree[id].right<=r)
    {
        tree[id].add=tree[id].add+val;
        tree[id].sum=tree[id].sum+(tree[id].right-tree[id].left+1)*val;
        return ;
    }
    //如果当前区间没有被包含
    if(tree[id].right<l||tree[id].left>r)   return ;
    //如果当前区间部分被包含,标记下移
    if(tree[id].add)    pushdown(id);
    //更新左右子区间
    add(id*2,l,r,val);
    add(id*2+1,l,r,val);
    //更新完再把结果返回上层
    pushup(id);
}


lint ans;
void query(int id,int l,int r)
{
    //查询区间在该区间外
    if(tree[id].left>r||tree[id].right<l)   return ;
    //该区间完全被包含
    if(tree[id].left>=l&&tree[id].right<=r)
    {
        ans=ans+tree[id].sum;
        return ;
    }
    //查询部分该区间,标记下移
    if(tree[id].add)    pushdown(id);
    //左右查找
    int mid=(tree[id].left+tree[id].right)/2;
    if(l<=mid)  query(id*2,l,r);
    if(r>mid)   query(id*2+1,l,r);
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init();
        char s[10];
        for(int i=1; i<=n; i++)
            scanf("%I64d",&a[i]);
        build(1,1,n);
        for(int i=1; i<=m; i++)
        {
            int a,b,c;
            scanf("%s",s);
            if(s[0]=='Q')
            {
                ans=0;
                scanf("%d %d",&a,&b);
                query(1,a,b);
                printf("%I64d\n",ans);
            }
            if(s[0]=='C')
            {
                scanf("%d %d %d",&a,&b,&c);
                add(1,a,b,c);
            }
        }
    }
    //fclose(stdin);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值