【poj3468-A Simple Problem with Integers】-线段树区间更新

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 122589 Accepted: 38023
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , 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

题意:给你n个数字,对区间内的所有数字进行增减,并查询区间和

分析题意非常简单易懂,如果考虑暴力修改每个区间的和的话,时间复杂度将达到O(NQ),完美超时.....而树状数组或者是线段树的话则可以将时间复杂度将至O(nlogn)的级别,下面给出线段树的ac代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ls (root<<1)
#define rs (root<<1|1)

using namespace std;
const int N = 200000;

struct node
{
    long long l,r;
    long long sum;
    long long add;//lazy标记
    long long mid()
    {
        return (l+r)>>1;
    }
}seg[N<<2];
long long num[N];
void pushup(int root)
{
    seg[root].sum  = seg[ls].sum + seg[rs].sum;
}
void build(int l,int r,int root)
{
    seg[root].l=l;
    seg[root].r=r;
    seg[root].add=0;
    if(l == r)
    {
        seg[root].sum  = num[l];
        return ;
    }
    long long mid = seg[root].mid();
    build(l,mid,ls);
    build(mid+1,r,rs);
    pushup(root);
}

void pushdown(int root,int m)
{
    if(seg[root].add)
    {
        if(seg[root].l==seg[root].r)//如果是叶子节点则不继续下传标记
        {
            seg[root].add=0;
            return ;
        }
        ///区间累加
        seg[ls].add  += seg[root].add;
        seg[rs].add  += seg[root].add;
        ///区间和修改
        seg[ls].sum += (m-(m>>1)) * seg[root].add;
        seg[rs].sum += (m>>1)     * seg[root].add;
        seg[root].add=0;
    }
}

void update_sum(int l,int r,long long add,int root)//区间和更新
{
    if(l <= seg[root].l && r >= seg[root].r)//当前节点区间包含在更新区间内
    {
        seg[root].add += add;
        seg[root].sum += add*(seg[root].r - seg[root].l + 1);
        return;
    }
    pushdown(root,seg[root].r-seg[root].l+1); //延迟标记向下传递
    //更新左右孩子节点
    long long mid = seg[root].mid();
    if(l <= mid)  update_sum(l,r,add,ls);
    if(r > mid)   update_sum(l,r,add,rs);
    pushup(root);
}

long long query_sum(int l,int r,int root)//(L,R)为需要查询的区间,(l,r)为当前区间
{
    if(l <= seg[root].l && r >= seg[root].r)  return seg[root].sum;
    pushdown(root,seg[root].r-seg[root].l+1);
    long long mid = seg[root].mid();
    long long ret = 0;
    if(l <= mid)   ret += query_sum(l,r,ls);
    if(r > mid)    ret += query_sum(l,r,rs);
    return ret;
}

int main()
{
    int n, m, a, b, c;
    scanf("%d%d",&n, &m);
    for(int i = 1;i <= n; i++)
    {
        scanf("%lld",&num[i]);
    }
    build(1,n,1);
    char ch;
    for(int i = 1;i <= m; i++)
    {
        scanf(" %c",&ch);
        if(ch == 'Q')
        {
            scanf("%d%d",&a,&b);
            printf("%lld\n",query_sum(a,b,1));
        }
        else
        {
            scanf("%d%d%d",&a,&b,&c);
            update_sum(a,b,c,1);
        }
    }
    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值