[ABC366C] Balls and Bag Query 题解

[ABC366C] Balls and Bag Query

维护一个 s u m sum sum 值,为当前数字的种类数。再维护 c n t i cnt_i cnti 表示数字 i i i 的个数。

对于 3 3 3 操作,直接输出 s u m sum sum 即可。

对于 1 1 1 操作, c n t x ← c n t x + 1 cnt_x\gets cnt_x+1 cntxcntx+1,如果加完后 c n t x cnt_x cntx 1 1 1,说明这是一个新的数字, s u m ← s u m + 1 sum\gets sum+1 sumsum+1

对于 2 2 2 操作, c n t x ← c n t x − 1 cnt_x\gets cnt_x-1 cntxcntx1,如果减完后 c n t x cnt_x cntx 0 0 0,说明这个数字没有了, s u m ← s u m − 1 sum\gets sum-1 sumsum1

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> q;
	while (q--) {
		int opt, x; cin >> opt;
		if (opt == 3) cout << sum << '\n';
		else if (opt == 1) {
			cin >> x;
			cnt[x] ++;
			sum += (cnt[x] == 1);
		}
		else {
			cin >> x;
			cnt[x] --;
			sum -= (!cnt[x]);
		}
	}
	return 0;
}
  • 21
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值