两段异或和的最大值 二进制TRIE图表达式 XOR CodeChef Nikitosh and xor
来源:互联网 发布:logback 不打印sql 编辑:程序博客网 时间:2024/06/07 01:03
(A[l1] ⊕ A[l1 + 1] ⊕ · · · ⊕ A[r1]) + (A[l2] ⊕ A[l2 + 1] ⊕ · · · ⊕ A[r2])
其中,1 ≤ l1 ≤ r1 < l2 ≤ r2 ≤ N。
式中 x ⊕ y 表示 x 和 y 的 按位异或
PROBLEM LINK:
Practice
Contest
Author: Yurii Rebryk
Tester: Kevin Atienza
Editorialists: Pushkar Mishra and Suhash Venkatesh
DIFFICULTY:
Medium
PREREQUISITES:
Tries, Properties of Bitwise XOR
PROBLEM:
Given is an array
EXPLANATION:
Subtask 1
Let's start off by thinking of the simplest algorithm that we can. We we will then optimise it step by step by making certain observations.
So, the simplest thing to do is to implement what the problem says. We can run four loops, one each for
How can we optimise this? Let's make an observation. If we know for each
What remains now is to calculate efficiently for each
Calculating the arrays
Subtask 2
We need to somehow speed up the calculation of arrays
Let
Then for some
The proof behind this is left to the reader as a simple exercise.
We can now say that
Calculating
We can use Tries to get the required complexity. We will keep a trie which holds the binary representation of elements in the array
Inserting into a trie is a standard operation. Let us look at how we do the other operation, i.e., for a value
COMPLEXITY:
SAMPLE SOLUTIONS:
Author
Tester
Editorialist
#include <stdio.h> #define NMAX 500000 int A[NMAX], N;int maxleft[NMAX], maxright[NMAX]; void ReadInput() {scanf("%d", &N);for (int i = 1; i <= N; i++) scanf("%d", &A[i]);} #define MAX_TRIE_NODES 20000000 int next[MAX_TRIE_NODES][2];int nnodes; void ClearTrie() {nnodes = 1;next[1][0] = next[1][1] = 0;} void InsertInTrie(int x) {for (int bit = 30, node = 1; bit >= 0; bit--) {int dir = 0;if ((x & (1 << bit)) > 0) dir = 1;if (next[node][dir] == 0) {nnodes++;next[node][dir] = nnodes;next[nnodes][0] = next[nnodes][1] = 0;}node = next[node][dir];}} int GetMax(int x) {int ans = 0;for (int bit = 30, node = 1; bit >= 0; bit--) {int dir = 0;if ((x & (1 << bit)) > 0) dir = 1;if (next[node][1 - dir] > 0) {ans |= (1 << bit);dir = 1 - dir;}node = next[node][dir];}return ans;} int main() {//freopen("y.txt", "r", stdin);ReadInput();ClearTrie();InsertInTrie(0);int i, sxor = 0;maxleft[0] = 0;for (i = 1; i <= N; i++) {sxor ^= A[i];maxleft[i] = GetMax(sxor);if (maxleft[i - 1] > maxleft[i]) maxleft[i] = maxleft[i - 1];InsertInTrie(sxor);//fprintf(stderr, "maxleft(%d)=%d\n", i, maxleft[i]);}ClearTrie();InsertInTrie(0);sxor = 0;maxright[N + 1] = 0;for (i = N; i >= 1; i--) {sxor ^= A[i];maxright[i] = GetMax(sxor);if (maxright[i + 1] > maxright[i]) maxright[i] = maxright[i + 1];InsertInTrie(sxor);//fprintf(stderr, "maxright(%d)=%d\n", i, maxright[i]);} unsigned int ans = 0, cans;for (i = 2; i <= N; i++) {cans = (unsigned int) maxleft[i - 1] + (unsigned int) maxright[i];if (cans > ans) ans = cans;}printf("%u\n", ans);return 0;}
- 两段异或和的最大值 二进制TRIE图表达式 XOR CodeChef Nikitosh and xor
- 【codechef】Nikitosh and xor(线段树)
- 求and or xor的最大值
- Trie/Xor
- HDU4825 Xor Sum(Trie树,二进制)
- 【CodeChef-XRQRS】Xor Queries【可持久化Trie / +主席树】
- BZOJ 4546|CodeChef XRQRS|Xor Queries|可持久化Trie
- hdu4825 Xor Sum【Trie、Xor】
- CodeChef XRQRS - Xor Queries
- 【主席树】Codechef Prefix XOR
- [主席树] Codechef: Prefix XOR
- 【POJ3764】The xor-longest Path Trie树+异或性质
- 【USACO6.1.3】Cow XOR奶牛异或 trie
- xor
- XOR ^
- xor
- xor
- xor
- 我的分享
- iOS—常用iOS、Mac框架和库及常用中文开发博客
- Effective C++——条款52(第8章)
- Python Socket学习
- linux服务器上svn的安装和配置
- 两段异或和的最大值 二进制TRIE图表达式 XOR CodeChef Nikitosh and xor
- 跟我一起读Hadoop源码——HDFS篇(01)
- 好用的全盘搜索软件Everything
- 【Jenkins系列之五】Jenins安全管理和权限控制
- C++替换字符
- Java正则表达式入门
- Word公式和文字的显示位置调整
- 白盒测试中的逻辑覆盖
- Spark 源码阅读一-启动脚本