前三个数的和
数组是 17、24、15、13、23。
运行下面这段程序:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const vector<long long> A5 = {17, 24, 15, 13, 23};
int lowbit(int x) {
return x & (-x);
}
vector<long long> fen_build(const vector<long long>& a) {
int n = (int)a.size();
vector<long long> t(n + 1, 0);
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += lowbit(j)) t[j] += a[i - 1];
}
return t;
}
long long fen_prefix(const vector<long long>& t, int i) {
long long s = 0;
for (; i > 0; i -= lowbit(i)) s += t[i];
return s;
}
int main() {
vector<long long> t = fen_build(A5);
cout << fen_prefix(t, 3) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论