# #130. 树状数组 1 ：单点修改，区间查询

• 1 i x：给定 i,x
• 2 l r：给定 l,r

• 1 i x：给定 i,x
• 2 l r：给定 l,r

#### 样例输入

3 2
1 2 3
1 2 0
2 1 3

#### 样例输出

6

#### 数据范围与提示

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6;
int n,m;
ll sum[maxn];
int lowbit(int x){return x&(-x);}
while(x<=maxn){
sum[x]+=val;
x+=lowbit(x);
}
}
ll getsum(int x){
ll res=0;
while(x){
res+=sum[x];
x-=lowbit(x);
}
return res;
}
int main(){
scanf("%d%d",&n,&m);
ll c;
for(int i=1;i<=n;i++){
scanf("%lld",&c);
}
while(m--){
int id,x,y;
scanf("%d%d%d",&id,&x,&y);
else
cout<<getsum(y)-getsum(x-1)<<endl;
}
return 0;
}

分块解法：

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
ll a[maxn],sum[maxn];
int n,q,num,block,l[maxn],r[maxn],belong[maxn];
void build(){
block=sqrt(n);
num=n/block;
if(n%block)num++;
for(int i=1;i<=num;i++)
l[i]=(i-1)*block+1,r[i]=i*block;
r[num]=n;
for(int i=1;i<=n;i++)
belong[i]=(i-1)/block+1;

for(int i=1;i<=num;i++)
for(int j=l[i];j<=r[i];j++)
sum[i]+=a[j];
}
ll query(int x,int y){
ll res=0;
if(belong[x]==belong[y]){
for(int i=x;i<=y;i++)
res+=a[i];
return res;
}
for(int i=x;i<=r[belong[x]];i++) res+=a[i];
for(int i=belong[x]+1;i<belong[y];i++)res+=sum[i];
for(int i=l[belong[y]];i<=y;i++)res+=a[i];
return res;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build();
while(q--){
int id,x,y;
scanf("%d%d%d",&id,&x,&y);
if(id==1){
a[x]+=y;
sum[belong[x]]+=y;
}
else
printf("%lld\n",query(x,y));
}
return 0;
}
View Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
int n,q;
ll tree[maxn*4],a[maxn];
void pushup(int rt){
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void build(int l,int r,int rt){
if(l==r){
tree[rt]=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int pos,ll val,int l,int r,int rt){
if(l==r){
tree[rt]+=val;
return;
}
int mid=(l+r)>>1;
if(pos<=mid) update(pos,val,l,mid,rt<<1);
else update(pos,val,mid+1,r,rt<<1|1);
pushup(rt);
}
ll ask(int L,int R,int l,int r,int rt){
ll res=0;
if(l>=L&&r<=R) return tree[rt];
pushup(rt);
int mid=(l+r)>>1;
return res;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
while(q--){
int id,x,y;
scanf("%d%d%d",&id,&x,&y);
if(id==1) update(x,y,1,n,1);
}
return 0;
}
View Code

# #131. 树状数组 2 ：区间修改，单点查询

#### 题目描述

• 1 l r x：给定 l,r,x
• 2 i：给定 i

#### 输入格式

• 1 l r x：对于所有 i\in[l,r]
• 2 i：给定 i

#### 样例输入

3 2
1 2 3
1 1 3 0
2 2

#### 样例输出

2

#### 数据范围与提示

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
ll sum[maxn];
int n,q;
int lowbit(int x){return x&(-x);}
while(x<=n){
sum[x]+=val;
x+=lowbit(x);
}
}
ll res=0;
while(x){
res+=sum[x];
x-=lowbit(x);
}
return res;
}
int main(){
cin>>n>>q;
ll tmp=0;
for(int i=1;i<=n;i++){
int x;
cin>>x;
tmp=x;
}
while(q--){
int id,l,r,x;
cin>>id;
if(id==1){
cin>>l>>r>>x;
}
else{
cin>>x;
}
}
return 0;
}

分块解法：

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
int n,q,block,num;
ll a[maxn],lazy[maxn],l[maxn],r[maxn],belong[maxn];
void build(){
block=sqrt(n);
num=n/block;
if(n%block)num++;
for(int i=1;i<=num;i++)
l[i]=(i-1)*block+1,r[i]=block*i;
for(int i=1;i<=n;i++) belong[i]=(i-1)/block+1;
}
void update(int x,int y,int z){
if(belong[x]==belong[y]){
for(int i=x;i<=y;i++)a[i]+=z;
return;
}
for(int i=x;i<=r[belong[x]];i++)a[i]+=z;
for(int i=belong[x]+1;i<belong[y];i++)lazy[i]+=z;
for(int i=l[belong[y]];i<=y;i++)a[i]+=z;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build();
while(q--){
int id,x,y,z;
scanf("%d",&id);
if(id==1){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z);
}
else{
scanf("%d",&x);
printf("%lld\n",a[x]+lazy[belong[x]]);
}
}
return 0;
}
View Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
int n,q;
ll a[maxn],tree[maxn*4],lazy[maxn*4];
void pushup(int rt){tree[rt]=tree[rt<<1]+tree[rt<<1|1];}
void pushdown(int l,int r,int rt){
if(lazy[rt]){
tree[rt<<1]+=lazy[rt]*l;
tree[rt<<1|1]+=lazy[rt]*r;
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
lazy[rt]=0;
}
}
void build(int l,int r,int rt){
if(l==r){
tree[rt]=a[l];
return;
}
int mid=l+r>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,int val,int l,int r,int rt){
if(l>=L&&r<=R){
tree[rt]+=val*(r-l+1);
lazy[rt]+=val;
return ;
}
int mid=l+r>>1;
pushdown(mid-l+1,r-mid,rt);
if(mid>=L) update(L,R,val,l,mid,rt<<1);
if(mid<R) update(L,R,val,mid+1,r,rt<<1|1);
pushup(rt);
}
ll ask(int pos,int l,int r,int rt){
if(l==pos&&l==r)
return tree[rt];
int mid=l+r>>1;
pushdown(mid-l+1,r-mid,rt);
ll ans;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
while(q--){
int id,x,y,z;
scanf("%d",&id);
if(id==1){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,1,n,1);
}
else{
scanf("%d",&x);
}
}
return 0;
}
View Code

# #132. 树状数组 3 ：区间修改，区间查询

#### 题目描述

• 1 l r x：给定 l,r,x
• 2 l r：给定 l,r

#### 输入格式

• 1 l r x：对于所有 i\in[l,r]
• 2 l r：输出 \sum_{i=l}^ra[i]

#### 样例输入

5 10
2 6 6 1 1
2 1 4
1 2 5 10
2 1 3
2 2 3
1 2 2 8
1 2 3 7
1 4 4 10
2 1 2
1 4 5 6
2 3 4

#### 样例输出

15
34
32
33
50

#### 数据范围与提示

sum1【x】=a【i】-a【x-1】，a【x】=sum1【1】+sum1【2】+……sum1【x】

a【1】+a【2】+……a【x】=sum1【1】+（sum1【1】+sum1【2】）+……（sum1【1】+sum1【2】+……sum1【x】）

=x*sum1【1】+（x-1）*sum1【2】+……sum1【x】

=x*（sum1【1】+sum1【2】+……sum1【x】）-∑(i=1-x)(i-1)*sum1[i]

,所以我们就多建立一个数组sum2用来维护（x-1）sum1【x】就好了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
ll sum1[maxn],sum2[maxn],a[maxn];
int n,q;
int lowbit(int x){return x&(-x);}
for(int i=x;i<=n;i+=lowbit(i)){
sum1[i]+=val;
sum2[i]+=(x-1)*val;
}
}
ll res=0;
for(int i=x;i;i-=lowbit(i)){
res+=x*sum1[i]-sum2[i];
}
return res;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
while(q--){
int id,l,r,x;
scanf("%d",&id);
if(id==1){
scanf("%d%d%d",&l,&r,&x);
}
else{
scanf("%d%d",&l,&r);
}
}
return 0;
}

分块解法：

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
int n,q,num,block;
ll lazy[maxn],sum[maxn],a[maxn],l[maxn],r[maxn],belong[maxn];
void build(){
block=sqrt(n);
num=n/block;
if(n%block)num++;
for(int i=1;i<=num;i++)
l[i]=(i-1)*block+1,r[i]=i*block;
r[num]=n;
for(int i=1;i<=n;i++)
belong[i]=(i-1)/block+1;

for(int i=1;i<=num;i++)
for(int j=l[i];j<=r[i];j++)
sum[i]+=a[j];
}
void update(int x,int y,int val){
if(belong[x]==belong[y]){
for(int i=x;i<=y;i++) a[i]+=val;
sum[belong[x]]+=(y-x+1)*val;
return;
}
for(int i=x;i<=r[belong[x]];i++)a[i]+=val;
for(int i=belong[x]+1;i<belong[y];i++){
sum[i]+=block*val;
lazy[i]+=val;
}
for(int i=l[belong[y]];i<=y;i++)a[i]+=val;
sum[belong[x]]+=(r[belong[x]]-x+1)*val;
sum[belong[y]]+=(y-l[belong[y]]+1)*val;
}
ll res=0;
if(belong[x]==belong[y]){
for(int i=x;i<=y;i++)res+=a[i]+lazy[belong[x]];
return res;
}
for(int i=x;i<=r[belong[x]];i++)res+=a[i]+lazy[belong[x]];
for(int i=belong[x]+1;i<belong[y];i++) res+=sum[i];
for(int i=l[belong[y]];i<=y;i++)res+=a[i]+lazy[belong[y]];
return res;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build();
while(q--){
int id,x,y,z;
scanf("%d",&id);
if(id==1){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z);
}
else{
scanf("%d%d",&x,&y);
}
}
return 0;
}
View Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const ll maxn=1e6+10;
int n,q;
ll a[maxn],tree[maxn*4],lazy[maxn*4];
void pushup(int rt){tree[rt]=tree[rt<<1]+tree[rt<<1|1];}
void pushdown(int l,int r,int rt){
if(lazy[rt]){
tree[rt<<1]+=l*lazy[rt];
tree[rt<<1|1]+=r*lazy[rt];
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
lazy[rt]=0;
}
}
void build(int l,int r,int rt){
lazy[rt]=0;
if(l==r){
tree[rt]=a[l];
return;
}
int mid=l+r>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,ll val,int l,int r,int rt){
if(l>=L&&r<=R){
tree[rt]+=val*(r-l+1);
lazy[rt]+=val;
return;
}
int mid=l+r>>1;
pushdown(mid-l+1,r-mid,rt);
if(mid>=L) update(L,R,val,lson);
if(mid<R) update(L,R,val,rson);
pushup(rt);
}
ll ask(int L,int R,int l,int r,int rt){
ll res=0;
if(l>=L&&r<=R) return tree[rt];
int mid=l+r>>1;
pushdown(mid-l+1,r-mid,rt);
return res;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
while(q--){
int id,x,y,z;
scanf("%d",&id);
if(id==1){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,1,n,1);
}
else{
scanf("%d%d",&x,&y);
}