树状数组知识点
树状数组应用
他是前缀和的一种优化,主要支持用的时间复杂度区间查找和单点修改
可用于查找逆序对,实现查找逆序对。
用于延伸后,可以实现区间修改和区间查找。是常数。
最后,它还可以用来查找动态中位数。
除了前缀和,还可以用于后缀和,时间复杂度与前缀和相同。
与线段树的区别是:代码简单,更大的实用性,扩展性强。
树状数组原理
他是一个数组,一个普通的数组。
发现了什么?数组相加的长度是,但是有什么用呢?
如果要修改一个前缀和,几乎所有的元素都要翻新。可是,如果要修改这个的话,如,只要修,数据少看不出来,但数据多就会发现,修改差不多只要,因为下一个修改都会在原基础下加一个。 可得出代码
long long lowerbit(long long x){
return x&-x;
}
void xiugai(long long x){
while(x<=n){
c[x]+=res;
x+=lowerbit(x);
}
return ;
}而查询的话,前缀和是,而树状数组要查询的话,如查询,3的二进制是,正好对应和,所以我们的查找也是,好巧!正好是将改成了 从而得出以下代码
long long lowerbit(long long x){
return x&-x;
}
long long chazhao(long long x){
long long res=0;
while(x){
res+=c[x];
x-=lowerbit(x);
}
return res;
}练习题解
由于一天时间有限,所以蒟蒻只写一下作业的
会议座位
这道题我们先定义一个哈希,将字符串从左到右的顺序附上值(顺序),之后再来看第二个,把字符串替换成数字后,会发现:不就是求逆序对吗?
首先,定义为树状数组,我们只要到查询了后面有几个比他数值高,那就贡献了几个逆序对。
设为比小的有多少个,这样走到此位置时,只需要修改(操作一)就可以了,至于参数(一个是位置一个是加的值), and 。
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
long long num[N],c[N];
string a[N],b[N];
map<string,int> mp;
long long lowerbit(long long x){
return x&-x;
}
long long chazhao(long long x){
long long res=0;
while(x){
res+=c[x];
x-=lowerbit(x);
}
return res;
}
void xiugai(long long x,long long res){
while(x<=N){
c[x]+=res;
x+=lowerbit(x);
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
mp[a[i]]=i;
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
long long ans=0;
for(int i=n;i>=1;i--){
ans+=chazhao(mp[b[i]]-1);
xiugai(mp[b[i]],1);
}
cout<<ans;
return 0;
}P1774 最接近神的人
通过查找发现,最好的办法是每次减少一个逆序对,这样才是没有浪费任何一步。事实证明也没有更好的解
#include<bits/stdc++.h>
using namespace std;
const int N=3e6+10;
long long num[N],c[N],a[N],b[N];
map<int,int> mp;
long long lowerbit(long long x){
return x&-x;
}
long long chazhao(long long x){
long long res=0;
while(x){
res+=c[x];
x-=lowerbit(x);
}
return res;
}
void xiugai(long long x,long long res){
while(x<=N){
c[x]+=res;
x+=lowerbit(x);
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i]+1;
a[i]++;
}
sort(b+1,b+1+n);
for(int i=1;i<=n;i++){
mp[b[i]]=i;
}
long long ans=0;
for(int i=n;i>=1;i--){
if(a[i]==0){
continue;
}
ans+=chazhao(mp[a[i]]-1);
xiugai(mp[a[i]],1);
}
cout<<ans;
return 0;
}奇数码问题
这道题用二维十分困难,我们转化一下:
______
1 2 3 | __________________
0 4 6 |->|1 2 3 0 4 6 7 5 8 |
7 5 8 | |__________________|
______|对于上,逆序对所受影响只会在框起来的区域:
_______
|1 2 3 0| 4 6 7 5 8
|_______|去除0,增加了2个逆序对
0 2 3 1 4 6 7 5 8而剩下的,加减逆序对数量都是偶数个,所以只要逆序对奇偶性相同,就可以,反之则无。
#include <bits/stdc++.h>
using namespace std;
const int N=250010;
int c[N];
int ans1[N],ans2[N];
void clear_c(int n) {
memset(c,0,sizeof c);
}
int lowbit(int x) {
return x & -x;
}
void xiugai(int x,int val,int n) {
while(x<=n){
c[x]+=val;
x+=lowbit(x);
}
}
int chaxun(int x) {
int sum=0;
while (x>0) {
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
long long s(int ans[], int n) {
clear_c(n);
long long inv=0;
for (int i=1;i<=n;i++){
inv+=i-chaxun(ans[i]);
xiugai(ans[i],1,n);
}
return inv;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
while(cin>>n){
n=n*n;
int num,idx=0;
for (int i=1;i<=n;i++){
cin>>num;
if(num!=0){
ans1[++idx]=num;
}
}
idx=0;
for (int i=1;i<=n;i++){
cin>>num;
if (num!=0){
ans2[++idx]=num;
}
}
long long inv1=s(ans1,n-1);
long long inv2=s(ans2,n-1);
if((inv1%2)==(inv2%2)){
cout<<"TAK"<<endl;
}else{
cout<<"NIE"<<endl;
}
}
return 0;
}前前缀和
我们需要求的是,即 暴力求解肯定是不行的,需要化简一下:
我们可以考虑一下每一个元素对前前缀合的贡献,第i个数被~之间的每一个 都计算了一遍,所以它的贡献就是,我们需要求的就是。
我们回到单个元素,根据乘法分配率,我们就得到, 所以, 再将(k+1)提出来之后就得到式子
对于和
我们都能够用树状数组来维护。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
long long c1[N*10],c2[N*10],a[N];
long long n,m;
long long lowerbit(long long x){
return x&-x;
}
long long chazhao(long long x,long long c[]){
long long res=0;
while(x){
res+=c[x];
x-=lowerbit(x);
}
return res;
}
void xiugai(long long x,long long res,long long c[]){
while(x<=n){
c[x]+=res;
x+=lowerbit(x);
}
return ;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
xiugai(i,a[i],c1);
xiugai(i,a[i]*i,c2);
}
while(m--){
string as;
cin>>as;
if(as=="Modify"){
long long x,res;
cin>>x>>res;
res=res-a[x];
a[x]+=res;
xiugai(x,res,c1);
xiugai(x,x*res,c2);
}else{
long long x;
cin>>x;
cout<<(x+1)*chazhao(x,c1)-chazhao(x,c2)<<endl;
}
}
return 0;
}P3372 【模板】线段树1
这道题确实可以用线段树写,但是太麻烦了。
带入主题。
我们稍加观察就可以发现,这里所有区间修改在差分上都十分简单,只需要两个修改,所以我们将输入的看成前缀和,要求的是前前缀和,就和上一题一模一样。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
long long c1[N],c2[N],a[N],t[N];
long long n,m;
long long lowerbit(long long x){
return x&-x;
}
long long chazhao(long long x,long long c[]){
long long res=0;
while(x){
res+=c[x];
x-=lowerbit(x);
}
return res;
}
void xiugai(long long x,long long res,long long c[]){
while(x<=n){
c[x]+=res;
x+=lowerbit(x);
}
return ;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>t[i];
}
for(int i=1;i<=n;i++){
a[i]=t[i]-t[i-1];
xiugai(i,a[i],c1);
xiugai(i,a[i]*i,c2);
}
while(m--){
int c,l,r;
cin>>c>>l>>r;
if(c==1){
long long res=0;
cin>>res;
a[l]+=res;
a[r+1]-=res;
xiugai(l,res,c1);
xiugai(r+1,-res,c1);
xiugai(l,l*res,c2);
xiugai(r+1,-(r+1)*res,c2);
}else{
long long x1,x2;
x1=(l)*chazhao(l-1,c1)-chazhao(l-1,c2);
x2=(r+1)*chazhao(r,c1)-chazhao(r,c2);
cout<<x2-x1<<endl;
}
}
return 0;
}楼兰图腾
遍历左边,将此端点看作中心那点,就可以轻松写出来。
用树状数组维护左边有多少大于他的,小于他的直接计算出来。之后再反着跑一趟,最后在计算答案
对了!别忘了用数组存比它大的和比它小的!
#include<bits/stdc++.h>
using namespace std;
const int N=200010;
unsigned long long a[N],b[N],l1[N],l2[N],r1[N],r2[N];
long long c[N],t[N];
map<unsigned long long,int> mp;
int n,m;
long long lowerbit(long long x){
return x&-x;
}
long long chazhao(long long x){
long long res=0;
while(x){
res+=c[x];
x-=lowerbit(x);
}
return res;
}
void xiugai(long long x,long long res){
while(x<=n){
c[x]+=res;
x+=lowerbit(x);
}
return ;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b+1,b+1+n);
for(int i=1;i<=n;i++){
mp[b[i]]=i;
}
unsigned long long ans1=0,ans2=0;
for(int i=1;i<=n;i++){
long long res1=chazhao(mp[a[i]]-1);
long long res2=i-1-res1-t[mp[a[i]]];
// cout<<res1<<" "<<res2<<" "<<i<<endl;
xiugai(mp[a[i]],1);
t[mp[a[i]]]++;
l1[i]=res1;
l2[i]=res2;
}
memset(c,0,sizeof c);
memset(t,0,sizeof t);
for(int i=n;i>=1;i--){
long long res1=chazhao(mp[a[i]]-1);
long long res2=n-i-res1-t[mp[a[i]]];
// cout<<res1<<" "<<res2<<" "<<i<<endl;
xiugai(mp[a[i]],1);
t[mp[a[i]]]++;
ans1+=res1*l1[i];
ans2+=res2*l2[i];
}
cout<<ans2<<" "<<ans1;
return 0;
}
