Last active
August 29, 2015 14:02
-
-
Save sauyon/dd7d80776d6b8e5cb263 to your computer and use it in GitHub Desktop.
BIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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