POJ 3468 A Simple Problem with Integers (树状数组)

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

Hint

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

题意:

给定一个数列A1,A2,…An以及Q个操作,按顺序执行这些操作。操作分2种

*给定l,r,x,对Al,Al+1,…,Ar同时加上x

*给出l,r,求Al,Al+1,…,Ar

分析:

和线段树的做法类似,树状数组也可以通过在每个节点上维护两个数据,高效地进行上述操作.

如果给区间[l, r)同时再加上x的话,每个节点的值将会如何变化呢?如果令


下面记sum(bit,i)为树状数组bit的前i项和。我们构建两个树状数组bit0和bit1,并且设

那么在[l,r)区间上同时加上x就可以看成是

*在bit0的l位置加上-x(l - 1)

*在bit1的l位置加上x

*在bit0的r+1位置加上xr

*在bit0的r+1位置加上-x

这四个操作。因此,查询和更新操作都可以在O(logn)时间里完成。

更一般的,如果操作得到的结果可以用i的n次多项式表示,那么就可以使用n+1个树状数组来进行维护了。

*T是操作的种类。第i个操作的T[i]是C的话,就是给区间同时加一个值,是Q的话则是查询一段的和。

*A,L,R都是以1为下标起点的。

#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 10;

int N, Q;
ll A[maxn];
char T[maxn];
int L[maxn], R[maxn], X[maxn];

// BIT
ll bit0[maxn], bit1[maxn];

ll sum(ll *b, int i)
{
    ll s = 0;
    while (i > 0){
        s += b[i];
        i -= i & -i;
    }
    return s;
}

void add(ll *b, int i, int v)
{
    while (i <= N){
        b[i] += v;
        i += i & -i;
    }
}

void solve()
{
    for (int i = 1; i <= N; i++){
        scanf("%lld", &A[i]);
        add(bit0, i, A[i]);
    }
    for (int i = 0; i < Q; i++){
        getchar();
        scanf("%c", &T[i]);
        if (T[i] == 'C'){
            scanf("%d%d%d", &L[i], &R[i], &X[i]);
            add(bit0, L[i], -X[i] * (L[i] - 1));
            add(bit1, L[i], X[i]);
            add(bit0, R[i] + 1, X[i] * R[i]);
            add(bit1, R[i] + 1, -X[i]);
        }
        else {
            scanf("%d%d", &L[i], &R[i]);
            ll res = 0;
            res += sum(bit0, R[i]) + sum(bit1, R[i]) * R[i];
            res -= sum(bit0, L[i] - 1) + sum(bit1, L[i] - 1) * (L[i] - 1);
            printf("%lld\n", res);
        }
    }
}

int main()
{
    while (scanf("%d%d", &N, &Q) != EOF){
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值