码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers

时间:2019-07-29 21:44:54      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:out   body   inpu   name   desc   def   res   pac   oid   

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 163977   Accepted: 50540
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.
 
题意:  Q查询区间和;C,将区间[x,y]的数都加上z
 
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#define ll long long
using namespace std;
ll tree[8000050], lazy[8000050], len[8000050];//tree[num]存的是节点num所在区间的区间和
void pushdown(ll num)
{
    if (lazy[num] != 0)
    {
        tree[num * 2] = tree[num * 2] + lazy[num] * len[num * 2];
        tree[num * 2 + 1] = tree[num * 2 + 1] + lazy[num] * len[num * 2 + 1];
        lazy[num * 2] = lazy[num * 2] + lazy[num];
        lazy[num * 2 + 1] = lazy[num * 2 + 1] + lazy[num];
        lazy[num] = 0;
    }
}

void build(ll num, ll le, ll ri)
{
    len[num] = ri - le + 1;//区间长度
    if (le == ri)
    {
        scanf("%lld", &tree[num]);
        return;
    }
    ll mid = (le + ri) / 2;
    build(num * 2, le, mid);
    build(num * 2 + 1, mid + 1, ri);
    tree[num] = tree[num * 2] + tree[num * 2 + 1];
}

void update(ll num, ll le, ll ri, ll x, ll y, ll z)
{
    if (x <= le && ri <= y)
    {
        lazy[num] = lazy[num] + z;
        tree[num] = tree[num] + len[num] * z;//更新区间和
        return;
    }
    pushdown(num);
    ll mid = (le + ri) / 2;
    if (x <= mid)
        update(num * 2, le, mid, x, y, z);
    if (y > mid)
        update(num * 2 + 1, mid + 1, ri, x, y, z);
    tree[num]=tree[num*2]+tree[num*2+1];
}

ll query(ll num, ll le, ll ri, ll x, ll y)
{
    if (x <= le && ri <= y)//查询区间在num节点所在区间内
        return tree[num];
    pushdown(num);
    ll mid = (le + ri) / 2;
    ll ans = 0;
    if (x <= mid)
        ans = ans + query(num * 2, le, mid, x, y);
    if (y > mid)
        ans = ans + query(num * 2 + 1, mid + 1, ri, x, y);
    return ans;
}
int main()
{
    ll n, m;
    scanf("%lld%lld", &n, &m);
    build(1, 1, n);
    while (m--)
    {
        char c[15];
        scanf("%s", c);
        if (c[0] == Q)
        {
            ll x, y;
            scanf("%lld%lld", &x, &y);
            printf("%lld\n", query(1, 1, n, x, y));
        }
        else
        {
            ll x, y, z;
            scanf("%lld%lld%lld", &x, &y, &z);
            update(1, 1, n, x, y, z);
        }
    }
    return 0;
}

 

POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers

标签:out   body   inpu   name   desc   def   res   pac   oid   

原文地址:https://www.cnblogs.com/-citywall123/p/11266535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!