n是數組長度,a數組是原數列,c數組是輔助數組(樹狀數組,在第一種模板中表示前綴和,第二種模板中表示區間最大值),也就是樹狀數組的節點
單點更新和區間求和
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+50;
ll a[N],c[N],n,x;
int lowbit(int x){
return x&-x;
}
void update(int i,ll d){
while(i<=n){
c[i]+=d;
i+=lowbit(i);
}
}
ll getSum(int i){
ll ans=0;
while(i>0){
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main(void){
return 0;
}
單點更新和區間最值
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+50;
ll a[N],c[N],n,x;
int lowbit(int x){
return x&-x;
}
//這裏要先更新a數組,若是有插入操作,也要更新n
void update(int x){
while(x<=n){
//c[x]記錄max(x...x-lowbit(x)+1)
c[x]=a[x];
int lx=lowbit(x);
//每次加1複雜度太高,因爲x只能從x-2^k轉移而來(lowbit的性質)
for(int i=1;i<lx;i<<=1){
c[x]=max(c[x],c[x-i]);
}
x+=lowbit(x);
}
}
ll getMax(int l,int r){
ll ans=0;
while(l<=r){
ans=max(ans,a[r]);
for(r--;r-lowbit(r)>=l;r-=lowbit(r)){
ans=max(ans,c[r]);
}
}
return ans;
}
int main(void){
return 0;
}