Help

I need a help, I don't know what to write in int main() in this program to get array and sum of frist r elements:
struct FenwickTree {
vector<int> bit;
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
Last edited on
First, please use code tags. They make posted code easier to read. See http://www.cplusplus.com/articles/jEywvCM9/

What do you mean by "get array and sum of frist r elements"?
How does this "tree" relate to your task?

You have no apparent need for member n. Therefore:
1
2
3
4
5
6
7
8
9
10
11
12
struct FenwickTree {
  vector<int> bit;

  FenwickTree(int n) : bit(n, 0) {
  }

  FenwickTree( const vector<int>& a ) : bit(a.size(), 0) {
    for ( size_t i = 0; i < a.size(); ++i ) add(i, a[i]);
  }

  // other members
};
Topic archived. No new replies allowed.