junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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


题意:C表示更新区间值,Q表示查询区间和,因为C和Q混杂,需要用线段树维护(若先C后Q,可以改用差分数列)。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# define MAXN 100000
# define lson l, m, id<<1
# define rson m+1, r, id<<1|1
# define LL long long
using namespace std;

LL sum[MAXN<<2], lazy[MAXN<<2];

void build(int l, int r, int id)
{
    lazy[id] = 0;
    if(l == r)
    {
        scanf("%lld",&sum[id]);
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    sum[id] = sum[id<<1] + sum[id<<1|1];
}

void pushdown(int id, int len)
{
    if(lazy[id])
    {
        lazy[id<<1] += lazy[id];
        lazy[id<<1|1] += lazy[id];
        sum[id<<1] += lazy[id]*(len-(len>>1));
        sum[id<<1|1] += lazy[id]*(len>>1);
        lazy[id] = 0;
    }
}

LL query(int L, int R, int l, int r, int id)
{
    if(L <= l && R >= r)
        return sum[id];
    pushdown(id, r-l+1);//不能放在上面。
    LL ret = 0;
    int m = (l+r)>>1;
    if(L <= m)
        ret += query(L, R, lson);
    if(R > m)
        ret += query(L, R, rson);
    return ret;
}

void update(int L, int R, int k, int l, int r, int id)
{
    if(L <= l && r <= R)
    {
        lazy[id] += k;
        sum[id] += (r-l+1)*k;
        return;
    }
    pushdown(id, r-l+1);//★因为下面要更新sum[id],所以要先更新下子区间。
    int m = (l+r)>>1;
    if(L <= m)
        update(L, R, k, lson);
    if(R > m)
        update(L, R, k, rson);
    sum[id] = sum[id<<1] + sum[id<<1|1];
}

int main()
{
    int n, m, l, r, k;
    char c;
    while(~scanf("%d%d",&n,&m))
    {
        build(1, n, 1);
        while(m--)
        {
            getchar();
            c = getchar();
            if(c == 'C')
            {
                scanf("%d%d%d",&l,&r,&k);
                update(l, r, k, 1, n, 1);
            }
            else
            {
                scanf("%d%d",&l,&r);
                printf("%lld\n",query(l, r, 1, n, 1));
            }
        }
    }
    return 0;
}


posted on 2017-03-28 21:30  junior19  阅读(98)  评论(0编辑  收藏  举报