poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 45133 Accepted: 13234
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.
 
题意:若干个数的区间更新,区间询问(询问区间和)。
思路:法一:线段树,lazy思想。
 
AC代码:
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cmath>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
using namespace std;

const int  N= 100005;
int num[N];
int n;
struct node
{
    int l,r;
    long long sum,add;
}tree[4*N];

void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].add=0;
    if(l==r)
    {
        tree[rt].sum=num[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,L(rt));
    build(mid+1,r,R(rt));
    tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum;
}
void update(int l,int r,int rt,int val)
{
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*val;
        tree[rt].add+=val;
        return;
    }
    if(tree[rt].add)
    {
        tree[L(rt)].sum+=(tree[L(rt)].r-tree[L(rt)].l+1)*tree[rt].add;
        tree[L(rt)].add+=tree[rt].add;
        tree[R(rt)].sum+=(tree[R(rt)].r-tree[R(rt)].l+1)*tree[rt].add;
        tree[R(rt)].add+=tree[rt].add;
        tree[rt].add=0;
    }
    if(r<=tree[L(rt)].r) update(l,r,L(rt),val);
    else if(l>=tree[R(rt)].l) update(l,r,R(rt),val);
    else
    {
        update(l,tree[L(rt)].r,L(rt),val);
        update(tree[R(rt)].l,r,R(rt),val);
    }
    tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum;
}
long long query(int l,int r,int rt)
{
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        return tree[rt].sum;
    }
    if(tree[rt].add)
    {
        tree[L(rt)].sum+=(tree[L(rt)].r-tree[L(rt)].l+1)*tree[rt].add;
        tree[L(rt)].add+=tree[rt].add;
        tree[R(rt)].sum+=(tree[R(rt)].r-tree[R(rt)].l+1)*tree[rt].add;
        tree[R(rt)].add+=tree[rt].add;
        tree[rt].add=0;
    }
    if(r<=tree[L(rt)].r) return query(l,r,L(rt));
    else if(l>=tree[R(rt)].l) return query(l,r,R(rt));
    else return query(l,tree[L(rt)].r,L(rt))+query(tree[R(rt)].l,r,R(rt));
}
int main()
{
    int q,a,b,c;
    char s[5];
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    scanf("%d",&num[i]);
    build(1,n,1);
    while(q--)
    {
        scanf("%s",s);
        if(s[0]=='C')
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,1,c);
        }
        else
        {
            scanf("%d%d",&a,&b);
            printf("%I64d\n",query(a,b,1));
        }
    }
    return 0;
}

法二:树状数组区间更新。

 树状数组天生用来动态维护数组前缀和,其特点是每次更新一个元素的值,查询只能查数组的前缀和,

但这个题目求的是某一区间的数组和,而且要支持批量更新某一区间内元素的值,怎么办呢?实际上,

还是可以把问题转化为求数组的前缀和。

 

    首先,看更新操作update(s, t, d)把区间A[s]...A[t]都增加d,我们引入一个数组delta[i],表示

A[i]...A[n]的共同增量,n是数组的大小。那么update操作可以转化为:

1)令delta[s] = delta[s] + d,表示将A[s]...A[n]同时增加d,但这样A[t+1]...A[n]就多加了d,所以

2)再令delta[t+1] = delta[t+1] - d,表示将A[t+1]...A[n]同时减d

 

    然后来看查询操作query(s, t),求A[s]...A[t]的区间和,转化为求前缀和,设sum[i] = A[1]+...+A[i],则

                            A[s]+...+A[t] = sum[t] - sum[s-1],

那么前缀和sum[x]又如何求呢?它由两部分组成,一是数组的原始和,二是该区间内的累计增量和, 把数组A的原始

值保存在数组org中,并且delta[i]对sum[x]的贡献值为delta[i]*(x+1-i),那么

                            sum[x] = org[1]+...+org[x] + delta[1]*x + delta[2]*(x-1) + delta[3]*(x-2)+...+delta[x]*1

                                         = org[1]+...+org[x] + segma(delta[i]*(x+1-i))

                                         = segma(org[i]) + (x+1)*segma(delta[i]) - segma(delta[i]*i),1 <= i <= x

这其实就是三个数组org[i], delta[i]和delta[i]*i的前缀和,org[i]的前缀和保持不变,事先就可以求出来,delta[i]和

delta[i]*i的前缀和是不断变化的,可以用两个树状数组来维护。

 
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll __int64
#define eps 1e-6

using namespace std;

const int INF = 1000000007;
const int maxn = 100005;

ll c1[maxn], c2[maxn], a[maxn];
int n, m;
int lowbit(int x)
{
    return x & (-x);
}
void add(ll *c, int x, int d)
{
    for(int i = x; i <= n; i += lowbit(i))
    c[i] += d;
}
ll sum(ll *c, int x)
{
    ll ret = 0;
    for(int i = x; i > 0; i -= lowbit(i))
    ret += c[i];
    return ret;
}
int main()
{
    int s, t, d;
    char op[3];
    while(~scanf("%d%d", &n, &m))
    {
        memset(c1, 0, sizeof(c1));
        memset(c2, 0, sizeof(c2));
        a[0] = 0;
        for(int i = 1; i <= n; i++)
        {
           scanf("%I64d", &a[i]);
           a[i] += a[i - 1];
        }
        while(m--)
        {
            scanf("%s", op);
            if(op[0] == 'Q')
            {
                scanf("%d%d", &s, &t);
                ll ans = a[t] + (t + 1) * sum(c1, t) - sum(c2, t);
                ans -= (a[s - 1] + s * sum(c1, s - 1) - sum(c2, s - 1));
                printf("%I64d\n", ans);
            }
            else
            {
                scanf("%d%d%d", &s, &t, &d);
                add(c1, s, d);
                add(c1, t + 1, -d);
                add(c2, s, d * s);
                add(c2, t + 1, -d * (t + 1));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值