目录
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种:
+ x: 表示向集合中添加一个元素x
- x:表示删除集合中值为x的一个元素
? x:表示查询集合中与x异或的最大值为多少
析:这是一个字典树的应用,不过确实没看出来。。。。主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除,
剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个。这样就是最大的。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <stack> using namespace std; typedef long long LL; typedef pair< int , int > P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const LL LNF = 100000000000000000; const double PI = acos (-1.0); const double eps = 1e-8; const int maxn = 4e6 + 5; const int mod = 1e9 + 7; const char *mark = "+-*" ; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; int n, m; inline bool is_in( int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } inline LL Max(LL a, LL b){ return a < b ? b : a; } inline LL Min(LL a, LL b){ return a > b ? b : a; } inline int Max( int a, int b){ return a < b ? b : a; } inline int Min( int a, int b){ return a > b ? b : a; } int ch[maxn][2]; int t[maxn]; int cnt = 0; void add( int x){ int k = 1; for ( int i = 30; i >= 0; --i){ int j = ((1<<i) & x) > 0; if (!ch[k][j]) ch[k][j] = ++cnt; k = ch[k][j]; ++t[k]; } } void del( int x){ int k = 1; for ( int i = 30; i >= 0; --i){ int j = ((1<<i) & x) > 0; k = ch[k][j]; --t[k]; } } int query( int x){ int k = 1; int ans = 0; for ( int i = 30; i >= 0; --i){ int j = ((1<<i) & x) == 0; if (t[ch[k][j]]){ ans |= (1<<i); k = ch[k][j]; } else k = ch[k][1-j]; } return ans; } int main(){ while ( scanf ( "%d" , &n) == 1){ cnt = 1; memset (ch, 0, sizeof (ch)); memset (t, 0, sizeof (t)); add(0); while (n--){ char s[3]; int x; scanf ( "%s %d" , s, &x); if (s[0] == '+' ) add(x); else if (s[0] == '-' ) del(x); else printf ( "%d\n" , query(x)); } } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)