Skip to content

Instantly share code, notes, and snippets.

@Sd-Invol
Last active August 29, 2015 14:06
Show Gist options
  • Save Sd-Invol/10764399eb4d4d3c352a to your computer and use it in GitHub Desktop.
Save Sd-Invol/10764399eb4d4d3c352a to your computer and use it in GitHub Desktop.
struct CHU_2_BIT {
int n;
LL B[N] , C[N];
void init(int size) {
n = size;
memset(B , 0 , n + 1 << 3);
memset(C , 0 , n + 1 << 3);
}
CHU_2_BIT() {}
CHU_2_BIT(int size) {
init(size);
}
inline LL _sum(LL* c , int x) {
LL res = 0;
for ( ; x > 0 ; x -= x & -x)
res += c[x];
return res;
}
void add(int l , int r , LL w) {
for (int i = l ; i <= n ; i += i & -i)
B[i] += w , C[i] += w * l;
++ r;
for (int i = r ; i <= n ; i += i & -i)
B[i] -= w , C[i] -= w * r;
}
LL sum(int l , int r) {
LL res = 0; -- l;
res += (r + 1) * _sum(B , r) - _sum(C , r);
res -= (l + 1) * _sum(B , l) - _sum(C , l);
return res;
}
}T;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment