01-trie练习
这里用递归实现01-trie, 可以看做是区间长度为2的幂的权值线段树, 能实现权值的所有操作, 异或时, 翻转左右儿子即可.
练习1 CF 817E Choosing The Commander
大意: 要求维护一个集合, 给定三种操作:
1. 插入属性$p_i$的元素
2. 删除属性$p_i$的元素
3. 询问集合中有多少元素异或$p_i$后的值小于$l_i$
算是比较简单的01-trie入门题, 假若每次都异或0的话, 完全就是裸的权值, 非0的话, 对于非0位翻转左右儿子即可
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std; const int N = 1e5+10, sz = 27;
int n, T, tot;
struct {int ch[2],sum;} tr[N<<5]; void add(int &o, int d, int x, int v) {
if (!o) o=++tot;
tr[o].sum += v;
if (d>=0) add(tr[o].ch[x>>d&1],d-1,x,v);
} int query(int o, int d, int x, int v) {
if (!o) return 0;
int f1 = x>>d&1, f2 = v>>d&1;
if (f2) return tr[tr[o].ch[f1]].sum+query(tr[o].ch[f1^1],d-1,x,v);
return query(tr[o].ch[f1],d-1,x,v);
} int main() {
scanf("%d", &n);
REP(i,1,n) {
int op, x, v;
scanf("%d%d", &op, &x);
if (op==1) add(T,sz,x,1);
else if (op==2) add(T,sz,x,-1);
else {
scanf("%d", &v);
printf("%d\n", query(T,sz,x,v));
}
}
}
练习2 : CF 842D Vitya and Strange Lesson
大意: 给定$n$个数的集合, 每次将集合中所有数异或$x$, 求整个集合的mex
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std; const int N = <<;
int n, m, tot, T;
int vis[N];
struct {int ch[],sum;} tr[N<<]; void ins(int &o, int d, int x) {
if (!o) o=++tot;
++tr[o].sum;
if (d>=) ins(tr[o].ch[x>>d&],d-,x);
} int query(int o, int d, int x) {
if (d<) return ;
int t = x>>d&;
if (tr[tr[o].ch[t]].sum==(<<d)) return (<<d)^query(tr[o].ch[t^],d-,x);
return query(tr[o].ch[t],d-,x);
} int main() {
scanf("%d%d", &n, &m);
REP(i,,n) {
int t;
scanf("%d", &t);
if (vis[t]) continue;
vis[t] = ;
ins(T,,t);
}
int num = ;
REP(i,,m) {
int t;
scanf("%d", &t);
printf("%d\n", query(T,,num^=t));
}
}
01-trie练习的更多相关文章
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- [一本通学习笔记] 字典树与 0-1 Trie
字典树中根到每个结点对应原串集合的一个前缀,这个前缀由路径上所有转移边对应的字母构成.我们可以对每个结点维护一些需要的信息,这样即可以去做很多事情. #10049. 「一本通 2.3 例 1」Phon ...
- 可持久化0-1 Trie 简介
Trie树是字符串问题中应用极为广泛的一种数据结构,可以拓展出AC自动机.后缀字典树等实用数据结构. 然而在此我们考虑0-1 Trie的应用,即在序列最大异或问题中的应用. 这里的异或是指按位异或.按 ...
- 「模板」 01 Trie实现平衡树功能
不想多说什么了.费空间,也不算太快,唯一的好处就是好写吧. #include <cstdio> #include <cstring> const int MAXN=100010 ...
- CSU 1216异或最大值 (0-1 trie树)
Description 给定一些数,求这些数中两个数的异或值最大的那个值 Input 多组数据.第一行为数字个数n,1 <= n <= 10 ^ 5.接下来n行每行一个32位有符号非负整数 ...
- 三元组[01 Trie计数]
也许更好的阅读体验 \(\mathcal{Description}\) \(\mathcal{Solution}\) 有两种方法都可以拿到满分 \(Solution\ 1\) 考虑枚举\(y\) 建两 ...
- P4735 最大异或和 01 Trie
题目描述 给定一个非负整数序列 \(\{a\}\),初始长度为\(n\). 有 \(m\) 个操作,有以下两种操作类型: \(A\ x\):添加操作,表示在序列末尾添加一个数 \(x\),序列的长度 ...
- poj 3764 The xor-longest Path (01 Trie)
链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
- bzoj 4260: Codechef REBXOR (01 Trie)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4260 题面: 4260: Codechef REBXOR Time Limit: 10 S ...
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
随机推荐
- linux查看是否有某个运行的进程命令
linux查看是否有某个运行的进程命令:例如,查询是否包含 “my_post” 关键字的进程 ps aux | grep my_post ps aux | grep my_post | grep - ...
- 出现“基础链接已关闭,无法链接到远程服务器"错误的解决办法
一些用户在安装一些软件或是系统做某些修改后,采集器就没无登录或是无法获取到网页.登录或是使用httppostget工具会出现 ”基础链接已关闭,无法链接到远程服务器“的提示.经分析,是系统Socket ...
- Js中String转int
Js中String转int 方案一代码: Number(str) 方案二代码: //parseInt 方法都有两个参数, 第一个参数就是要转换的对象, 第二个参数是进制基数, 可以是 2, 8, 10 ...
- log4j 根据类名指定文件
log4j.logger.io.netty=INFO, stdout, spiderlog4j.logger.com.ld.net.spider=INFO, stdout, spider log4j. ...
- http://bugs.mysql.com/bug.php?id=72123
今天某个环境发生了这个bug. http://bugs.mysql.com/bug.php?id=72123
- 树(Heap)
对于大量的输入数据,链表的线性访问时间太慢,不宜使用——<数据结构与算法分析——C 语言描述> p 65 对于大量的输入数据,适合用树结构,大部分操作都是 O( log N ). 二叉树 ...
- 01: git & github
目录:GIT其他篇 01: git & github 02: git分支管理 目录: 1.1 常见版本管理工具介绍 及 版本工具作用 1.2 git.GitHub和SVN比较 1.3 本地gi ...
- 20145313exp9
问题回答 SQL注入攻击原理,如何防御 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序 ...
- 【前端】Vue.js实现实例搜索应用
实例搜索应用 实现效果: 实现代码与注释: <!DOCTYPE html> <html> <head> <title>实例搜索</title> ...
- win10中mount和unmount iso文件
https://www.windowscentral.com/how-mount-or-unmount-iso-images-windows-10 You can also right-click t ...