POJ3468,A Simple Problem with Integers,线段树

题目链接:

POJ3468

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

题意:
输入N个数,有Q个操作,Q a b 表示查询区间[a,b]内所有数的和;C a b c 表示区间[a,b]内所有数都加上c

题解:
线段树区间更新,需要注意数的范围,得用long long

//bei
#include
#include
#include
#include
using namespace std;
#define lson l,m,rt<<1 // 树的左儿子
#define rson m+1,r,rt<<1|1 // 树的右儿子
#define Maxn 1111111
typedef long long ll;

ll sum[Maxn],col[Maxn]; // 数组得开大一点,不然会RE的

void PushUp(ll rt) // 向上更新
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void build(ll l,ll r,ll rt) // 建立线段树
{
    if (l == r)
    {
        scanf("%lld",&sum[rt]);
        return ;
    }
    ll m = (l+r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void PushDown(ll rt, ll m) // 线段树延迟标记
{
    if (col[rt]) // 向下标记的时候,col[rt]的值可能是负数,所以这里如果写 col[rt] > 0 就gg了
    {
        // 下面的四条语句都是 += ,不是 = ,因为题目说的是区间[a,b]内所有数加上c
        col[rt<<1] += col[rt]; // 如果不是很理解 col 为什么也是 += 的话,可以举个例子加以理解
        col[rt<<1|1] += col[rt];
        sum[rt<<1] += (m-(m>>1)) * col[rt];
        sum[rt<<1|1] += (m>>1) * col[rt];
        col[rt] = 0;
    }
}

void update(ll L, ll R, ll c, ll l, ll r, ll rt) // 区间更新
{
    if (L <= l && r <= R)
    {
        // 下面两条语句也必须是 +=
        col[rt] += c;
        sum[rt] += c * (r-l+1);
        return ;
    }
    PushDown(rt,r-l+1);
    ll m = (l+r) >> 1;
    if (L <= m)
    {
        update(L,R,c,lson);
    }
    if (R > m)
    {
        update(L,R,c,rson);
    }
    PushUp(rt);
}

ll query(ll L, ll R, ll l, ll r, ll rt) // 区间查询
{
    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    PushDown(rt,r-l+1); // 查询的时候也必须得向下标记,这条语句不能少!不明白的话,可以举个例子加以理解
    ll m = (l+r) >> 1;
    ll ans = 0;
    if (L <= m)
        ans += query(L,R,lson);
    if (R > m)
        ans += query(L,R,rson);
    return ans;
}
int main()
{
    ll N,Q,a,b,c;
    char ch[3];
    while (~scanf("%lld%lld",&N,&Q))
    {
        memset(sum,0,sizeof(sum));
        memset(col,0,sizeof(col));
        build(1,N,1);
        while (Q--)
        {
            scanf("%s",ch);
            if (ch[0] == 'C')
            {
                scanf("%lld%lld%lld",&a,&b,&c);
                update(a,b,c,1,N,1);
            }
            else if (ch[0] == 'Q')
            {
                scanf("%lld%lld",&a,&b);
                printf("%lld\n",query(a,b,1,N,1));
            }
        }
    }
    return 0;
}




你可能感兴趣的:(数据结构,线段树,POJ3468,A,Simple,Problem,wit,线段树)