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

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页