Skip to content

Instantly share code, notes, and snippets.

@sauyon
Last active August 29, 2015 14:02
Show Gist options
  • Save sauyon/dd7d80776d6b8e5cb263 to your computer and use it in GitHub Desktop.
Save sauyon/dd7d80776d6b8e5cb263 to your computer and use it in GitHub Desktop.
BIT
#include <vector>
class BIT {
private:
vector<int> ft;
int size;
int lastone(int S) {
return S & (-S);
}
public:
BIT(int n) : ft(n), size(n) {}
int sum(int b) {
int sum = 0;
for (; b; b -= lastone(b)) sum += ft[b];
return sum;
}
int sum(int a, int b) {
return sum(b) - (a == 1 ? 0 : sum(a - 1));
}
void update(int k, int v) {
for (; k <= size; k += lastone(k)) ft[k] += v;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment