2023河南萌新联赛第(二)场:河南工业大学 B题

考察点:Tire 树的运用   

其次看到异或运算的最大值应该就要想到tire

最后注意根节点要从1开始。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<math.h>

#define endl '\n'

#define int long long

#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)

using namespace std;

const int N = 1e5 + 10;

struct tire{
	int cnt[N * 33];
	int tr[N * 33][2];
	int idx = 1;
	
	void insert(int x, int k) {
		int p = 1;
		
		for (int i = 30; i >= 0; i--) {
			int j = x >> i & 1;
			
			if (!tr[p][j]) tr[p][j] = ++idx;
			
			p = tr[p][j];
			cnt[p] += k;
		}
	}
	
	int query(int x, int h) {
		int p = 1;
		
		int res = 0;
		
		for (int i = 30; i >= 0; i--) {
			int j_x = x >> i & 1;
			int j_h = h >> i & 1;
			
			if(!j_h) {
				res += cnt[tr[p][j_x ^ 1]];
				p = tr[p][j_x];
			}
			else {
				p = tr[p][j_x ^ 1];
			}
		}
		
		return res;
	}
	
}T;

void solve() {
	int n, hp;
	cin >> n >> hp;
	
	for (int i = 0; i < n; i++) {
		int op;
		cin >> op;
		
		if (!op) {
			int x;
			cin >> x;
			T.insert(x, 1);
		}
		else if (op == 1) {
			int x;
			cin >> x;
			T.insert(x, -1);
		}
		else{
			int x, h;
			cin >> x >> h;
			int t = T.query(x, h);
			
			cout << t << endl; 
			if (!t) hp--;
		}
	}
	
	cout << hp << endl;
}

signed main() 
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    solve();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值