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
    评论
Here is a C++ program that constructs a max heap with integers and prints it in the rotated form: ```cpp #include <iostream> #include <vector> using namespace std; // function to swap two integers void swap(int& a, int& b) { int temp = a; a = b; b = temp; } // function to heapify the given vector void heapify(vector<int>& arr, int n, int i) { int largest = i; // initialize largest as root int left = 2*i + 1; // left child index int right = 2*i + 2; // right child index // if left child is larger than root if (left < n && arr[left] > arr[largest]) largest = left; // if right child is larger than largest so far if (right < n && arr[right] > arr[largest]) largest = right; // if largest is not root if (largest != i) { // swap the root with largest element swap(arr[i], arr[largest]); // recursively heapify the affected sub-tree heapify(arr, n, largest); } } // function to build max heap void buildMaxHeap(vector<int>& arr, int n) { // start from the last non-leaf node and heapify each node for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); } // function to print the heap in the rotated form void printRotatedHeap(vector<int>& arr, int n) { int height = log2(n) + 1; // height of the heap int index = 0; // current index in the heap int spaces = pow(2, height - 1) - 1; // number of spaces before the first element of the current level // print each level of the heap in the rotated form for (int i = 0; i < height; i++) { // print the spaces before the first element of the current level for (int j = 0; j < spaces; j++) cout << " "; // print the elements of the current level for (int j = 0; j < pow(2, i) && index < n; j++) { cout << arr[index++] << " "; // print the spaces between elements of the current level for (int k = 0; k < 2 * spaces + 1; k++) cout << " "; } // move to the next line and adjust the number of spaces for the next level cout << endl; spaces /= 2; } } int main() { int n; cout << "Enter the number of elements: "; cin >> n; vector<int> arr(n); cout << "Enter the elements: "; for (int i = 0; i < n; i++) cin >> arr[i]; // build max heap buildMaxHeap(arr, n); // print the heap in the rotated form cout << "Max heap in the rotated form:\n"; printRotatedHeap(arr, n); return 0; } ``` In this program, we first define a `swap` function to swap two integers, and a `heapify` function to heapify the sub-tree rooted at a given index `i` in the given vector `arr`. We then define a `buildMaxHeap` function to build the max heap from the given vector `arr`. Finally, we define a `printRotatedHeap` function to print the max heap in the rotated form. In the `main` function, we first read the number of elements and the elements themselves from the user using `cin`. We then build the max heap using `buildMaxHeap` function, and print the heap in the rotated form using `printRotatedHeap` function. The `printRotatedHeap` function uses the height of the heap to determine the number of levels, and the number of spaces before the first element of each level. It then prints each level of the heap in the rotated form, by printing the elements of the level followed by the spaces between elements.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逍遥Fau

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

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

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

打赏作者

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

抵扣说明:

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

余额充值