ExFenwickTree
ExFenwickTree
这是可支持区间加区间求和的树状数组
首先设\(delta[i]\)为\([i,n]\)的共同增量.
所以将\([l,r]\)共同加上\(x\)就相当于\(delta[l]+=x,delta[r+1]-=x\)
然后求\(pre[i]\),\([1,i]\)的和
显然\(pre[i]\)等于\(\sum_{j=1}^{i}delta[j]\times (i-j+1)\)
拆开来得到\((i+1)\times \sum_{j=1}^i delta[j]-\sum_{j=1}^i delta[j]\times j\)
所以维护两个前缀就好啦.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=100000;
const int MAXM=100000;
int n,C1[MAXN+10],C2[MAXN+10];
int lowbit(const int x){return x&(-x);}
void update(int x,int w)
{
int now=x;
while(now<=n)
{
C1[now]+=w;
C2[now]+=w*x;
now+=lowbit(now);
}
}
void update(int l,int r,int w){update(l,w);update(r+1,-w);}
int query(int x)
{
int now=x,res=0;
while(now)
{
res+=C1[now]*(x+1)-C2[now];
now-=lowbit(now);
}
return res;
}