// 查询异或最大值,每次插入和查询时间都是log(C)
template<class T>
class trie01 {
vector<vector<T>> tree;
public:
trie01() : tree(1, vector<T>(2, 0)) {} // 插入待检查的数字
void insert (T x)
{
int p = 0;
for(int i = sizeof(x)*8-2; i >= 0; --i)
{
bool o = (x&((T)1<<i));
if(tree[p][o] == 0)
{
tree[p][o] = tree.size();
tree.emplace_back(2,0);
}
p = tree[p][o];
}
} // 查询在插入的数字中,返回与x异或后的最大值
T match (T x)
{
T t = x, p = 0;
// 从高位到低位
for(int i = sizeof(x)*8-2; i >= 0; --i)
{
bool o = (x&((T)1<<i));
if(tree[p][!o])
p = tree[p][!o], t = t|((T)1<<i);
else if(tree[p][o])
p = tree[p][o], t = t&(~((T)1<<i));
}
return t;
}
};

已通过 [数组中两个数的最大异或值](421. 数组中两个数的最大异或值 - 力扣(Leetcode))

template<class T>
class trie01 {
vector<vector<T>> tree;
public:
trie01() : tree(1, vector<T>(2, 0)) {} void insert (T x)
{
int p = 0;
for(int i = sizeof(x)*8-2; i >= 0; --i)
{
bool o = (x&((T)1<<i));
if(tree[p][o] == 0)
{
tree[p][o] = tree.size();
tree.emplace_back(2,0);
}
p = tree[p][o];
}
} T match (T x)
{
T t = x, p = 0;
// 从高位到低位
for(int i = sizeof(x)*8-2; i >= 0; --i)
{
bool o = (x&((T)1<<i));
if(tree[p][!o])
p = tree[p][!o], t = t|((T)1<<i);
else if(tree[p][o])
p = tree[p][o], t = t&(~((T)1<<i));
}
return t;
}
};
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
trie01<int> trie;
trie.insert(nums[0]);
int res = 0;
for(auto &a : nums)
res = max(res, trie.match(a)), trie.insert(a);
return res;
}
};

已通过 [E. Sausage Maximization](Problem - 282E - Codeforces)

#include <iostream>
#include <vector>
using namespace std; using ll = long long;
template<class T>
class trie01 {
vector<vector<T>> tree;
public:
trie01() : tree(1, vector<T>(2, 0)) {} void insert (T x)
{
int p = 0;
for(int i = sizeof(x)*8-2; i >= 0; --i)
{
bool o = (x&((T)1<<i));
if(tree[p][o] == 0)
{
tree[p][o] = tree.size();
tree.emplace_back(2,0);
}
p = tree[p][o];
}
} T match (T x)
{
T t = x, p = 0;
// 从高位到低位
for(int i = sizeof(x)*8-2; i >= 0; --i)
{
bool o = (x&((T)1<<i));
if(tree[p][!o])
p = tree[p][!o], t = t|((T)1<<i);
else if(tree[p][o])
p = tree[p][o], t = t&(~((T)1<<i));
}
return t;
}
}; int main()
{ ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL); trie01<ll> trie; int n; cin>>n;
vector<ll> arr(n);
ll sum = 0, res = 0;
for(auto &a : arr) {
cin >> a;
sum ^= a;
res = max(res, a);
trie.insert(sum);
} sum = 0;
for(int i = n-1; i >= 0; --i)
{
sum ^= arr[i];
res = max(res, trie.match(sum));
}
cout << res << endl;
return 0;
}

[模板]01trie,维护异或最大值的更多相关文章

  1. BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)

    题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...

  2. 中南oj 1216: 异或最大值 数据结构

    1216: 异或最大值 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 98  Solved: 29 [Submit][Status][Web Boar ...

  3. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  4. 2013级新生程序设计基础竞赛-正式赛 F 异或最大值 解题报告

    F - 异或最大值 Time Limit: 2000/1000MS (Java/Others)      Memory Limit: 128000/64000KB (Java/Others) Subm ...

  5. CSU 1216 异或最大值

    求n个数里面,求两两异或的最大值 直接来肯定会超时 既然要异或最大值,那么两个数的二进制肯定是正好错开为好...为了能快速找到错开的数,确实有点难想到,用字典树,按二进制数插入,再一个一个在字典树里面 ...

  6. 51nod 1295 XOR key-区间异或最大值-可持久化01Trie树(模板)

    1295 XOR key 2 秒 262,144 KB 160 分 6 级题   给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] ...

  7. CSU1216: 异或最大值(01Trie树)

    Description 给定一些数,求这些数中两个数的异或值最大的那个值 Input 多组数据.第一行为数字个数n,1 <= n <= 10 ^ 5.接下来n行每行一个32位有符号非负整数 ...

  8. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  9. Codeforces 811C Vladik and Memorable Trip (区间异或最大值) (线性DP)

    <题目链接> 题目大意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都只能出现在这个区间. 每个区间的价值为该区间不同的数的异或值之和,现在问你这n个数最大的价值是 ...

  10. [模拟赛]异或最大值 maxinum

    此题在考试时用暴力,暴了30分. 献上30分代码: #include<stdio.h> ]; int main() { int n,t,c,i,max,j,d; freopen(" ...

随机推荐

  1. Springboot创建项目、docker安装mysql及mybatis-plus调试

    Springboot创建项目及测试 1️⃣ idea构建springboot项目 2️⃣ 测试类 hello 1. 代码 RestController是ResponseBody + controlle ...

  2. 【JS】强化Promise理解,从零手写属于自己的Promise.all与Promise.race

    壹 ❀ 引 在一个思路搞定三道Promise并发编程题,手摸手教你实现一个Promise限制器一文中,我们在文章结尾留了一个疑问,关于第三题的实现能否解决当每次调用时间都不相等的情况(比如第二次调用要 ...

  3. NC19872 [AHOI2005]SHUFFLE 洗牌

    题目链接 题目 题目描述 为了表彰小联为Samuel星球的探险所做出的贡献,小联被邀请参加Samuel星球近距离载人探险活动. 由于Samuel星球相当遥远,科学家们要在飞船中度过相当长的一段时间,小 ...

  4. USB至串口TTL转接设备及Console线

    USB转串口常见芯片方案 FT232, FTDI(英国) 公认稳定可靠, 传输速率3Mbps, 功能最强, 单芯片内置SPI,TWI,JTAG,GPIO等功能. FT232BM为较早型号, FT232 ...

  5. CentOS7 开机网卡加载失败

    服务器CentOS7一开,发现web服务无法访问.最终用ifconfig发现,网卡没有加载,连个IP地址都没有. 这时使用命令 service network restart 试图重启服务器网络.不料 ...

  6. acm数学总结

    1.给定两个质数,m, n, 大于n * m - n - m的数都可以被整数个n和m唯一组成. 相关习题:[Coins] (https://ac.nowcoder.com/acm/contest/34 ...

  7. 硬件开发笔记(十四):RK3568底板电路LVDS模块、MIPI模块电路分析、LVDS硬件接口、MIPI硬件接口详解

    前言   本篇继续分析底板原理图mipi/lvds屏幕电路原理图.硬件接口详解.   LVDS与MIPI的区别   液晶屏有RGB TTL.LVDS.MIPI.HDMI接口,这些接口区别于信号的类型( ...

  8. OpenCV开发笔记(六十一):红胖子8分钟带你深入了解Shi-Tomasi角点检测(图文并茂+浅显易懂+程序源码)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  9. TCP Server and Client Demo

    server.go package main import ( "bufio" "fmt" "io" "net" &qu ...

  10. Anaconda下载安装步骤

    下载地址 下载比较慢的,用迅雷下,点击复制地址,然后在迅雷里面直接创建连接 Anaconda基于python3.8 Anaconda基于python3.6的 安装步骤 没啥说的,一路安装,中间有个勾选 ...