POJ 3468 A Simple Problem with Integers (线段树 区间增减 区间求和)

题目大意:

中文题面, 就是成段加减, 询问区间和


大致思路:

练习一下懒惰标记...就是一个简单的懒惰标记向下递推


代码如下:

Result  :  Accepted     Memory  :  4256 KB     Time  :  1782 ms

/*
 * Author: Gatevin
 * Created Time:  2015/8/16 19:53:07
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 100010

struct Segment_Tree
{
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    lint val[maxn << 2];
    lint flag[maxn << 2];
    void pushUp(int rt)
    {
        val[rt] = val[rt << 1] + val[rt << 1 | 1];
        return;
    }
    void pushDown(int l, int r, int rt, int mid)
    {
        if(flag[rt])
        {
            flag[rt << 1] += flag[rt];
            flag[rt << 1 | 1] += flag[rt];
            val[rt << 1] += (mid - l + 1)*flag[rt];
            val[rt << 1 | 1] += (r - mid)*flag[rt];
            flag[rt] = 0;
        }
        return;
    }
    
    void build(int l, int r, int rt)
    {
        if(l == r)
        {
            scanf("%I64d", &val[rt]);
            flag[rt] = 0;
            return;
        }
        int mid = (l + r) >> 1;
        flag[rt] = 0;
        build(lson);
        build(rson);
        pushUp(rt);
    }
    void update(int l, int r, int rt, int L, int R, lint value)
    {
        if(l >= L && r <= R)
        {
            val[rt] += (r - l + 1)*value;
            flag[rt] += value;
            return;
        }
        int mid = (l + r) >> 1;
        pushDown(l, r, rt, mid);
        if(mid >= L) update(lson, L, R, value);
        if(mid + 1 <= R) update(rson, L, R, value);
        pushUp(rt);
    }
    lint query(int l, int r, int rt, int L, int R)
    {
        if(l >= L && r <= R)
            return val[rt];
        int mid = (l + r) >> 1;
        pushDown(l, r, rt, mid);
        lint ret = 0;
        if(mid >= L) ret += query(lson, L, R);
        if(mid + 1 <= R) ret += query(rson, L, R);
        return ret;
    }
};

Segment_Tree ST;

int main()
{
    int n, Q;
    while(~scanf("%d %d", &n, &Q))
    {
        ST.build(1, n, 1);
        char op[4];
        while(Q--)
        {
            scanf("%s", op);
            int a, b, c;
            if(op[0] == 'Q')
            {
                scanf("%d %d", &a, &b);
                printf("%I64d\n", ST.query(1, n, 1, a, b));
            }
            else
            {
                scanf("%d %d %d", &a, &b, &c);
                ST.update(1, n, 1, a, b, c);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值