求优化(qwq)
查看原帖
求优化(qwq)
744288
zwb15350226668楼主2024/7/13 21:08
#include<iostream>
#include<cstring>
using namespace std;

int lowbit(int a) {
    return a & (-a);
}

void update(int c[], int n, int x, int val) {
    while (x <= n) {
        c[x] += val;
        x += lowbit(x);
    }
}

int query(int c[], int x) {
    int sum = 0;
    while (x > 0) {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}

int main() {
    int n = 2, m = 1;
    int a[n + 1], c[n + 1];
    memset(c, 0, sizeof(c));

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        update(c, n, i, a[i]);
    }

    for (int i = 1; i <= m; i++) {
        int x = 1, y = 2; // 求 a[1] + a[2] 的和
        int s1 = query(c, x - 1);
        int s2 = query(c, y);
        cout << s2 - s1 << endl; // 输出
    }

    return 0;
}

2024/7/13 21:08
加载中...