CF817E Choosing The Commander
2024-08-26 14:50:54
\(\mathtt{CF817E}\)
\(\mathcal{Description}\)
有 \(q\) 个操作\((1 \leq q \leq 10^{5})\):
1、加入一个数 \(p_i(1 \leq p_i \leq 10 ^ 8)\)
2、删去一个数 \(p_i(1 \leq p_i \leq 10 ^ 8)\)
3、询问集合中有多少数异或 \(p_i\) 后的值小于 \(l_i(1 \leq p_i,l_i \leq 10 ^ 8)\)
\(\mathcal{Solution}\)
第三种操作询问异或后的值,于是自然而然可以想到用 \(01trie\),所以可以把 \(p_i\) 插入到字典树内,\(1\) 操作是 \(+1\),\(2\) 操作就是 \(-1\),\(3\)操作询问是,把 \(p_i\) 的值遍历一遍,如果当前的位上是 \(0\),把指针转到 \(p_j\) 的位上,否则加上这个位上的数,把指针转到另一边。
\(\mathcal{Code}\)
#include<bits/stdc++.h>
#define pow(x, i) (x >> i)
using namespace std;
const int N = 6e6 + 10, M = 30;
int cnt[N], trie[N][2], tot = 1, n;
inline int read() {
int x = 0, k = 1; char c = getchar();
for (; c < 48 || c > 57; c = getchar()) k ^= (c == '-');
for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);
return k ? x : -x;
}
inline void add(int x, int dg) {
int p = 1;
for (int i = M; i >= 0; --i) {
if (dg == 1) trie[p][(pow(x, i) & 1)] = (trie[p][(pow(x, i) & 1)] != 0) ? trie[p][(pow(x, i) & 1)] : (++tot);
p = trie[p][(pow(x, i) & 1)], cnt[p] += dg;
}
}
inline int ask(int x, int y) {
int p = 1;
int sum = 0;
for (int i = M; i >= 0; i--) {
int t = (pow(y, i) & 1), t1 = (pow(x, i) & 1);
if (t)
sum += cnt[trie[p][t1]], p = trie[p][t1 ^ 1];
else
p = trie[p][t1];
}
return sum;
}
int main() {
n = read();
while (n--) {
int opt = read(), x = read();
if (opt == 1)
add(x, 1);
else if (opt == 2)
add(x, -1);
else if (opt == 3) {
int y = read();
printf("%d\n", ask(x, y));
}
}
return 0;
}
最新文章
- CYQ.Data V5 从入门到放弃ORM系列:教程 - Log、SysLogs两个日志类使用
- WiFi流量劫持—— JS脚本缓存投毒
- Mui沉浸模式以及状态栏颜色改变
- 关于node.js杂记
- 深入浅出 - Android系统移植与平台开发(十) - led HAL简单设计案例分析
- WPF中获得控件相对于控件的相对位置
- 【poj1144】 Network
- HDU 1394
- 重装linux后
- xmlBean学习一
- Python 资源
- 编译错误“The run destination My Mac 64-bit is not valid for Running the scheme &#39;***&#39;,解决办法
- VC6安装错误——Error Launching acmboot.exe
- NT路径,DOS路径和Device路径互相转换
- ARTS打卡计划第二周-Tips-mysql-binlog-connector-java的使用
- face recognition[MobiFace]
- 异步核心接口IAsyncResult的实现
- can协议
- 基于Spring-Boot框架的Elasticsearch搜索服务器配置
- vertx读取配置文件,获得端口号
热门文章
- mysql limit 数据重复及遗漏
- 解决mac pro 软件损坏
- 2.第一个Codefirst实例
- 理解Python中的继承规则和继承顺序
- 【leetcode】424. Longest Repeating Character Replacement
- <;Jmeter入门不放弃>;之<;3.两种常见录制脚本的方法>;
- Delphi正则表达式使用方法(TPerlRegEx)
- this与super的语法比较
- The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间
- Mysq sql语句教程