A Simple Problem with Integers(线段树 / 树状数组)

题目链接: A Simple Problem with Integers

大致题意

有两种操作:
​ ①给区间[l, r]所有元素增加c
​ ②查询区间[l, r]中所有元素值的和

解题思路

线段树区间修改 + 区间查询, 维护的数值为区间中的元素和

同样本题也提供一种树状数组的做法.

AC代码(线段树做法)

#include <iostream>
#include <cstdio>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
int w[N];
struct node {
    int l, r;
    ll val;
    ll lazy;
}t[N << 2];
void pushdown(node& op, ll lazy) {
    op.val += lazy * (op.r - op.l + 1);
    op.lazy += lazy;
}
void pushdown(int x) {
    if (!t[x].lazy) return;
    pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
    t[x].lazy = 0;
}

void pushup(int x) {
    t[x].val = t[x << 1].val + t[x << 1 | 1].val;
}

void build(int l, int r, int x = 1) {
    t[x] = { l, r, w[l], 0 };
    if (l == r) return;
    int mid = l + r >> 1;
    build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
    pushup(x);
}

void modify(int l, int r, int c, int x = 1) {
    if (l <= t[x].l && r >= t[x].r) { pushdown(t[x], c); return; }
    pushdown(x);
    int mid = t[x].l + t[x].r >> 1;
    if (l <= mid) modify(l, r, c, x << 1);
    if (r > mid) modify(l, r, c, x << 1 | 1);
    pushup(x);
}

ll ask(int l, int r, int x = 1) {
    if (l <= t[x].l && r >= t[x].r) return t[x].val;
    pushdown(x);
    int mid = t[x].l + t[x].r >> 1;
    ll res = 0;
    if (l <= mid) res += ask(l, r, x << 1);
    if (r > mid) res += ask(l, r, x << 1 | 1);
    return res;
}

int main()
{
    int n, m; cin >> n >> m;
    rep(i, n) scanf("%d", &w[i]);
    build(1, n);
    
    while (m--) {
        char op[2]; int l, r; scanf("%s %d %d", op, &l, &r);
        
        if (*op == 'Q') printf("%lld\n", ask(l, r));
        else {
            int c; scanf("%d", &c);
            modify(l, r, c);
        }
    }
    return 0;
}

AC代码(树状数组)

#include <iostream>
#include <cstdio>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
int n, m;
ll t1[N], t2[N]; //t1[i]维护b[i], 而 t2[i]维护i*b[i]  其中b表示差分数组
int lowbit(int x) { return x & -x; }
void add(ll t[], int x, ll c) {
    for (int i = x; i <= n; i += lowbit(i)) t[i] += c;
}

ll ask(ll t[], int x) {
    ll res = 0;
    for (int i = x; i; i -= lowbit(i)) res += t[i];
    return res;
}

void change(int l, int r, ll c) {
    add(t1, l, c), add(t1, r + 1, -c);
    add(t2, l, l * c), add(t2, r + 1, (r + 1) * -c);
}

ll query(int x) { return (x + 1) * ask(t1, x) - ask(t2, x); }

int main()
{
    cin >> n >> m;
    rep(i, n) {
        int x; scanf("%d", &x);
        change(i, i, x);
    }
    
    while (m--) {
        char op[2]; int l, r; scanf("%s %d %d", op, &l, &r);
        if (*op == 'Q') printf("%lld\n", query(r) - query(l - 1));
        else {
            int c; scanf("%d", &c);
            change(l, r, c);
        }
    }
    return 0;
}

本题需要注意的点是: 虽然单个位置的值不会超过2E9, 但是涉及到区间求和, 所以用ll比较保险, 还可以省去部分类型转换的代码.

kuangbin线段树专题点这里!!!

END

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值