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

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;
}

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

时间: 2024-10-07 18:50:56

POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers的相关文章

【成端更新线段树模板】POJ3468-A Simple Problem with Integers

http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲一下pushdown和pushup出现的几个位置. pushup: (1)build的结尾,当叶子节点分别有对应的值后,它的父亲们就等于它们求和. (2)update的结尾,因为此时当前根的左右孩子已经更新了,故它也需要更新. pushdown(延迟标记): *pushdown会出现在一切要从当前结

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

poj 3468 A Simple Problem with Integers (线段树区间更新入门)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 70442   Accepted: 21723 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(线段树 区间更新)

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. In

Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers

对应POJ 题目:点击打开链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 72765   Accepted: 22465 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operati

POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 86780   Accepted: 26950 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

A Simple Problem with Integers 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