进阶计划辣鸡学员,问一个问题
查看原帖
进阶计划辣鸡学员,问一个问题
919410
Pollococido楼主2025/2/6 18:24

为什么下面树状数组代码把

cout << query(2);

改成

cout << query(2) - query(0);

就会 WA * 1, AC * 2, RE * 7 呢?鄙人不才,求 dalao 解答。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n = 2, lst;
int a[3], t[3]; 

int lowbit(int x) {
    return x & (-x);
}
void modify(int x, int num) {
    for (; x <= n; x += lowbit(x)) t[x] += num;
}
int query(int x) {
    int ret = 0;
    for (; x; x -= lowbit(x)) ret += t[x];
    return ret;
}

signed main() {
    for (int i = 1; i <= 2; i++) {
        cin >> a[i];
        modify(i, a[i]);
    }
    cout << query(2);
    return 0;
}
2025/2/6 18:24
加载中...