21天零基础入门ACM
21天零基础入门ACM之 第15天
树状数组
树状数组
树状数组最重要需要理解的就是lowbit这个函数,树状数组可以说就是一种简化的线段树吧。
例题:
例题1:https://ac.nowcoder.com/acm/problem/15164
代码:
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define sc(x) scanf("%d",x)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; }
ll qpow(ll a, ll b, ll mod) { ll ans = 1; a%=mod; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
const int mod=1e9+7;
const int N=1e5+7;
int n,m;
int nums[N],c[N];
int lowbit(int x){
return x&(-x);
}
void updata(int x,int k){
while(x<=n){
c[x]+=k;
x+=lowbit(x);
}
}
int ask(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
ll x,y,z;
int main(){
n=read();
m=read();
for(int i=1;i<=n;i++) {cin>>nums[i];updata(i, nums[i]);}
// for(int i=1;i<=n;i++) cout<<c[i]<<" ";
while(m--){
x=read();
y=read();
z=read();
if(x==1) updata(y,z);
else cout<<ask(z)-ask(y-1)<<endl;
}
}
例题2:
https://ac.nowcoder.com/acm/problem/15163
代码:
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define sc(x) scanf("%d",&x)
#define pr(x) printf("%lld",x)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; }
ll qpow(ll a, ll b, ll mod) { ll ans = 1; a%=mod; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
const int mod=1e9+7;
const int N=1e5+7;
int n,x;
int nums[N],c[N];
int lowbit(int x){
return x&(-x);
}
void updata(int x,int k){
while(x<=n){
c[x]+=k;
x+=lowbit(x);
}
}
int ask(int x){
ll ans=0;
while(x>0){
ans +=c[x];
x-=lowbit(x);
}
return ans;
}
int main(){
n=read();
ll ans=0;
memset(nums,0,sizeof(nums));
for(int i=1;i<=n;i++){
x=read();
updata(x,1);
ans +=ask(n)-ask(x);
}
cout<<ans<<endl;
}
例题3:https://ac.nowcoder.com/acm/problem/15172
代码:
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define sc(x) scanf("%d",&x)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; }
ll qpow(ll a, ll b, ll mod) { ll ans = 1; a%=mod; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; }
const int mod=1e9+7;
const int N=1e6+7;
int n,m;
int nums[N],c[N];
ll lowbit (ll x){
return x&(-x);
}
void add(int pos,int val){
while(pos<=n*n){
c[pos]+=val;
pos+=lowbit(pos);
}
}
ll query(int x){
ll ans=0;
for(int i=x;i>=1;i-=lowbit(i)){
ans+=c[i];
}
return ans;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n*n;i++){
cin>>nums[i];
if(nums[i]==1) add(i,1);
}
//for(int i=1;i<=n*n;i++) cout<<c[i]<<" ";
int op;
while(m--){
cin>>op;
if(op==1){
int x,y;
cin>>x>>y;
int pos=(x-1)*n+y;
if(nums[pos]==1){nums[pos]=0;add(pos,-1);}
else nums[pos]=1,add(pos,1);
}
else{
int x1,x2,y11,y2;
cin>>x1>>y11>>x2>>y2;
ll ans=0;
if(x1>x2) swap(x1,x2);
if(y11>y2) swap(y11,y2);
for(int i=x1;i<=x2;i++){
ans+=query((i-1)*n+y2)-query((i-1)*n+y11-1);
}
cout<<ans<<endl;
}
}
}