关于树状数组的一个奇怪错误
查看原帖
关于树状数组的一个奇怪错误
235901
Always_Remember_It楼主2024/7/2 21:15
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e6+10;
int n,q,a[N],tot[N];
int c[N],b[N],cnt,sum;
int s[N];
int lowbit(int x){
	return (x)&(-x);
}
void update(int x,int d){
	while(x<N){
		b[x]+=d;
		x+=lowbit(x);
	}
	return;
}
void upd(int x,int d){
	while(x<N){
		s[x]+=d;
		x+=lowbit(x);
	}
	return;
}
int query(int x){
	int now=0;
	while(x){
		now+=b[x];
		x-=lowbit(x);
	}
	return now;
}
int quest(int x){
	int now=0;
	while(x){
		now+=s[x];
		x-=lowbit(x);
	}
	return now;
}
signed main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		++tot[a[i]];
	}
	for(int i=1;i<=n;i++){
		if(tot[i]) c[++cnt]=tot[i];
	}
	for(int i=1;i<=cnt;i++){
		update(1,c[i]);
		update(c[i]+1,-c[i]);
		upd(1,1);
		upd(c[i]+1,-1);
	}
	while(q--){
		int op;cin>>op;
		if(op==1){
			int x;cin>>x;
			if(!tot[x]) c[++cnt]=1;
			++tot[x];
			update(1,1);
			update(tot[x],-1);
			update(tot[x],tot[x]);
			update(tot[x]+1,-tot[x]);
			upd(tot[x],1);
			upd(tot[x]+1,-1);
			continue;
		}
		if(op==2){
			int x;cin>>x;
			--tot[x];
			update(1,-1);
			update(tot[x]+1,1);
			update(tot[x]+1,-tot[x]-1);
			update(tot[x]+2,tot[x]+1);
			upd(tot[x]+1,-1);
			upd(tot[x]+2,1);
			continue;
		}
		int k;cin>>k;
		int ans=query(k),num=quest(k);
		cout<<ans-num*k<<endl;
	}
	return 0;
}
这份代码可以AC,但是把13行或20行的N改成n+q的话就会WA几个点,请问这是为什么
2024/7/2 21:15
加载中...