POJ3468 线段树 + Lazy Tag (延迟标记)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

#define REP(i, a, b) for (LL i = (a), _end_ = (b); i <= _end_; ++i)
#define DREP(i, a, b) for (LL i = (a), _begin_ = (b); i >= _begin_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (LL((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b){ return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b){ return a < b ? a = b, 1 : 0; }

typedef long long LL;

const LL dmax = 100100 << 2, oo = 0x3f3f3f3f;

LL n, m;

LL a[dmax];

LL c[dmax], add[dmax];

#define left x << 1, l, mid
#define right x << 1 | 1, mid + 1, r

inline LL Mid(LL x, LL y){ return (x + y) >> 1; }

inline void push_up(LL x){  c[x] = c[x << 1] + c[x << 1 | 1]; }


inline void push_down(LL x, LL y)
{
    if (add[x])
    {
        add[x << 1] += add[x];
        add[x << 1 | 1] += add[x];
        c[x << 1] += (y - (y >> 1)) * add[x];
        c[x << 1 | 1] += (y >> 1) * add[x];
        add[x] = 0;
    }
}

void create(LL x, LL l, LL r)
{
    if (l == r)
    {
        scanf("%I64d", &c[x]);
        return;
    }
    LL mid = Mid(l, r);
    create(left);
    create(right);
    push_up(x);
}

void update(LL x, LL l, LL r, LL s, LL t, LL k)
{
    if (l >= s && r <= t)
    {
        add[x] += k;
        c[x] += (LL)k * (r - l + 1);
        return;
    }
    LL mid = Mid(l, r);
    push_down(x, r - l + 1);
    if (s <= mid)
        update(left, s, t, k);
    if (t > mid)
        update(right, s, t, k);
    push_up(x);
}

LL query(LL x, LL l, LL r, LL s, LL t)
{
    if (l >= s && r <= t)
        return c[x];
    push_down(x, r - l + 1);
    LL mid = Mid(l, r);
    LL sum = 0;
    if (s <= mid) sum += query(left, s, t);
    if (mid < t) sum += query(right, s, t);
    return sum;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%I64d%I64d", &n, &m);
    create(1, 1, n);
    getchar();
    REP(i, 1, m)
    {
        LL c = getchar(), x, y;
        scanf("%I64d%I64d", &x, &y);
        if (c == 'Q')
            printf("%I64d\n", query(1, 1, n, x, y));
        else{
            LL z;
            scanf("%I64d", &z);
            update(1, 1, n, x, y, z);
        }
        getchar();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值