lowbit(12) 是多少
运行下面这段程序:
本节模型:lowbit(x) = x & (-x)、bit_build(a) 建树状数组、bit_query(tree, i) 求前缀和 a[0..i-1]。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int lowbit(int x) {
// x 的二进制里最低位的 1 所代表的值
return x & (-x);
}
vector<long long> bit_build(const vector<int>& a) {
// 建树状数组(下标 1..n)
int n = (int)a.size();
vector<long long> tree(n + 1, 0);
for (int i = 1; i <= n; i++) {
tree[i] += a[i - 1];
int j = i + lowbit(i);
if (j <= n) tree[j] += tree[i];
}
return tree;
}
long long bit_query(const vector<long long>& tree, int i) {
// 前缀和 a[0..i-1](i 从 1 到 n)
long long s = 0;
while (i > 0) {
s += tree[i];
i -= lowbit(i);
}
return s;
}
int main() {
cout << lowbit(12) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论