rt,犯了上界的错误(错的一样),但是我没发现错哪了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,Q;
int a[50000005],q[50000005];
class binary_index_tree{
public:
int tr[50000005];
#define lb(x) (x&-x)
void cg(int x,int y,int upper = 50000003){for(int i = x;i <= upper;i += lb(i))tr[i] += y;return ;}
int qq(int x){int s = 0;for(int i = x;i;i -= lb(i))s += tr[i];return s;}
int qr(int l,int r){return qq(r) - qq(l - 1);}
private:
}b,b2;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> Q;
for(int i = 1;i <= n;++i) cin >> a[i],q[a[i]]++;
for(int i = 1;i <= n;++i) if(q[i]) b2.cg(q[i],1),b.cg(q[i],q[i]);
while(Q-->0){
int oper,x,ans = 0;
cin >> oper >> x;
if(oper == 1){
if(q[x])b2.cg(q[x],-1);
if(q[x])b.cg(q[x],-q[x]);
++ q[x];
b2.cg(q[x],1);
b.cg(q[x],q[x]);
}
else if(oper == 2){
b2.cg(q[x],-1);
b.cg(q[x],-q[x]);
-- q[x];
if(q[x])b2.cg(q[x],1);
if(q[x])b.cg(q[x],q[x]);
}
else cout << b.qr(x,n) - x * b2.qr(x,n) << '\n';
}
return 0;
}