lowbit代表此二进制下的数的最大连续1的值,对原数组建立一个树状数组,每次更改为logn
查询求和为logn。
单点修改区间查询
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
const int maxx=1e6+100;
const ll inf=1e18+8;
ll a[maxx];
ll n;
ll b[maxx];
ll lowbit(ll x)
{
return x&(-x);
}
ll update(ll x,ll v) // i处增加v
{
ll p=a[x];
while(x<=n)
{
a[x]+=v; // 如果是变成v的话 a[x]-=p; a[x]+=v;
x+=lowbit(x);
}
}
ll getsum(ll x)
{
ll sum=0;
while(x>0)
{
sum+=a[x];
x-=lowbit(x);
}
return sum;
}
char ch[122];
int main()
{
ll i,j,x,k,t;
cin>>t;
int cas=0;
while(t--)
{
memset(a,0,sizeof(a));
cin>>n;
for(i=1;i<=n;i++)
{
cin>>b[i];
update(i,b[i]);
}
printf("Case %d:\n",++cas);
while(scanf("%s",ch))
{
if(ch[0]=='E') break;
scanf("%lld%lld",&i,&j);
if(ch[0]=='A') update(i,j);
if(ch[0]=='S') update(i,-j);
if(ch[0]=='Q')
{
printf("%lld\n",getsum(j)-getsum(i-1));
}
}
}
return 0;
}
区间修改基于查分数组的思想,具体不再阐述了。
区间修改区间查询
//#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
using namespace std;
const int maxx=1e6+100;
const ll inf=1e18+8;
ll sum1[maxx],sum2[maxx];
//(D[1] + D[2] + ... + D[n])
//(1*D[1] + 2*D[2] + ... + n*D[n])
ll a[maxx];
ll n,m;
ll lowbit(ll x)
{
return x&(-x);
}
ll update(ll x,ll k) // i处增加k
{
ll p=x;
while(x<=n)
{
sum1[x]+=k;
sum2[x]+=k*(p-1);
x+=lowbit(x);
}
}
ll getsum(ll i)
{
ll sum=0,x=i;
while(i>0)
{
sum+=x*sum1[i]-sum2[i];
i-=lowbit(i);
}
return sum;
}
char ch[122];
int main()
{
ll i,j,x,k,t,l,r;
cin>>n>>m;
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
update(i,a[i]-a[i-1]);
}
for(i=1;i<=m;i++)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
//cin>>l>>r;
scanf("%lld%lld",&l,&r);
printf("%lld\n",getsum(r)-getsum(l-1));
}
else
{
//cin>>l>>r>>x;
scanf("%lld%lld%lld",&l,&r,&x);
update(l,x);
update(r+1,-x);
}
}
return 0;
}
二维树状数组,但点修改。
直接记录原先每一行一维数组的树状数组,扩展到二维,每次询问时用容斥原理类似于矩阵前缀和求解即可。
五秒的时限把cout快读加上了才过了,2500毫秒
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using namespace std;
const int maxx=5e3+100;
const ll inf=1e18+8;
int n,m,q;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[maxx][maxx];
ll c[maxx][maxx];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int z)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=m;j+=lowbit(j))
c[i][j]+=z;
}
ll getsum(int x,int y)
{
ll sum=0;
for(int i=x;i>=1;i-=lowbit(i))
for(int j=y;j>=1;j-=lowbit(j))
sum+=c[i][j];
return sum;
}
int main()
{
n=read();m=read();
/*for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
a[i][j]=read();
update(i,j,a[i][j]);
}*/ //原始数组
int x;
while(scanf("%d",&x)!=EOF)
{
if(x==1)
{
int y,z,w;
y=read();
z=read();
w=read();
update(y,z,w);
}
if(x==2)
{
int x1,y1,x2,y2;
x1=read(); y1=read(); x2=read();y2=read();
//cout<<getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1)<<endl;
printf("%lld\n",getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1));
}
}
return 0;
}
二维区间修改,加上二维查分数组的思想(暂补中。。。。。。) 拖了两天了才整理,最近有点懒散