POJ 3468 A Simple Problem with Integers 线段树成段更新

线段树功能  

update:成段更新  query:区间求和

#include <iostream>
#include <string>
#include <cstdio>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;

typedef long long ll;
const int MAXN = 111111;
ll sum[MAXN<<2], add[MAXN<<2];

void push_up(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void push_down(int rt, int len)
{
    if(add[rt] == 0) return;
    add[rt<<1] += add[rt];
    add[rt<<1|1] += add[rt];
    sum[rt<<1] += (len - (len >> 1)) * add[rt];
    sum[rt<<1|1] += (len >> 1) * add[rt];
    add[rt] = 0;
}

void build(int l, int r, int rt)
{
    add[rt] = 0;
    if(l == r)
    {
        scanf("%I64d", &sum[rt]);
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int L, int R, int c, int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        sum[rt] += c * (r - l + 1);
        add[rt] += c;
        return;
    }
    push_down(rt, r-l+1);
    int m = (l + r) >> 1;
    if(L <= m) update(L, R, c, lson);
    if(R > m) update(L, R, c, rson);
    push_up(rt);
}

ll query(int L, int R, int l, int r, int rt)
{
    if(L <= l && r <= R) return sum[rt];
    push_down(rt, r-l+1);
    int m = (l + r) >> 1;
    ll ret = 0;
    if(L <= m) ret += query(L, R, lson);
    if(R > m) ret += query(L, R, rson);
    return ret;
}

int main()
{
//    freopen("in.txt", "r" ,stdin);
    int N, Q;
    while(~scanf("%d%d", &N, &Q))
    {
        build(1, N, 1);
        while(Q--)
        {
            int a, b, c;
            char s[3];
            scanf("%s", s);
            scanf("%d%d", &a, &b);
            if(s[0] == ‘Q‘) printf("%I64d\n", query(a, b, 1, N, 1));
            else
            {
                scanf("%d", &c);
                update(a, b, c, 1, N, 1);
            }
        }
    }
    return 0;
}
时间: 2024-08-28 21:33:45

POJ 3468 A Simple Problem with Integers 线段树成段更新的相关文章

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

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

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

A Simple Problem with Integers 线段树 成段更新

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3468 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

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

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

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

POJ 3468 A Simple Problem with Integers //线段树的成段更新

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