poj3468

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

Source

在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新nSum,再将增量往下传这样更新的复杂度就是O(log(n))。在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到nSum上后将Inc清0,接下来再往下查询。 Inc往下带的过程也是区间分解的过程,复杂度也是O(log(n)) 。

#include <iostream> 
using namespace std;

struct CNode {
    int L, R;
    CNode * pLeft, * pRight;
    long long nSum; //原来的和 
    long long Inc; //增量c的累加
};
CNode Tree[1000000];
int nCount = 0;

int Mid(CNode * pRoot) {
    return (pRoot->L + pRoot->R) / 2;
}

void BuildTree(CNode * pRoot, int L, int R) {
    pRoot->L = L;
    pRoot->R = R;
    pRoot->nSum = 0;
    pRoot->Inc = 0;
    if (L == R)
        return;
    nCount++;
    pRoot->pLeft = Tree + nCount;
    nCount++;
    pRoot->pRight = Tree + nCount;
    BuildTree(pRoot->pLeft, L, (L + R) / 2);
    BuildTree(pRoot->pRight, (L + R) / 2 + 1, R);
}

void Insert(CNode * pRoot, int i, int v) {
    if (pRoot->L == i && pRoot->R == i) {
        pRoot->nSum = v;
        return;
    }
    pRoot->nSum += v;
    if (i <= Mid(pRoot))
        Insert(pRoot->pLeft, i, v);
    else
        Insert(pRoot->pRight, i, v);
}

void Add(CNode * pRoot, int a, int b, long long c) {
    if (pRoot->L == a && pRoot->R == b) {
        pRoot->Inc += c;
        return;
    }
    pRoot->nSum += c * (b - a + 1);
    if (b <= (pRoot->L + pRoot->R) / 2)
        Add(pRoot->pLeft, a, b, c);
    else if (a >= (pRoot->L + pRoot->R) / 2 + 1)
        Add(pRoot->pRight, a, b, c);
    else {
        Add(pRoot->pLeft, a,(pRoot->L + pRoot->R) / 2, c);
        Add(pRoot->pRight,(pRoot->L + pRoot->R) / 2 + 1, b, c);
    }
}

long long QuerynSum(CNode * pRoot, int a, int b) {
    if (pRoot->L == a && pRoot->R == b)
        return pRoot->nSum + (pRoot->R - pRoot->L + 1) * pRoot->Inc;
    pRoot->nSum += (pRoot->R - pRoot->L + 1) * pRoot->Inc;
    Add(pRoot->pLeft, pRoot->L, Mid(pRoot), pRoot->Inc);
    Add(pRoot->pRight, Mid(pRoot) + 1, pRoot->R, pRoot->Inc);
    pRoot->Inc = 0;
    if (b <= Mid(pRoot))
        return QuerynSum(pRoot->pLeft, a, b);
    else if (a >= Mid(pRoot) + 1)
        return QuerynSum(pRoot->pRight, a, b);
    else {
        return QuerynSum(pRoot->pLeft, a, Mid(pRoot)) +
                QuerynSum(pRoot->pRight, Mid(pRoot) + 1, b);
    }
}

int main() {
    int n, q, a, b, c;
    char cmd[10];
    scanf("%d%d", &n, &q);
    int i, j, k;
    nCount = 0;
    BuildTree(Tree, 1, n);
    for (i = 1; i <= n; i++) {
        scanf("%d", &a);
        Insert(Tree, i, a);
    }
    for (i = 0; i < q; i++) {
        scanf("%s", cmd);
        if (cmd[0] == 'C') {
            scanf("%d%d%d", &a, &b, &c);
            Add(Tree, a, b, c);
        }
        else {
            scanf("%d%d", &a, &b);
            printf("%lld\n", QuerynSum(Tree, a, b));
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值