Bzoj 4260:codechef Rebxor (trie)

Source: Internet
Author: User

With the prefix and, the maximum XOR value ending with the X element is max (sumx^sumP) (1≤P<X), which is accelerated with trie. Suffix the same, and then sweep it again OK. Time Complexity O ( 31N)

-----------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <cctype>using namespace std; const int MAXN = 400009;const int n = +;int Read () {char C = getchar (); int ret = 0;For (;!isdigit (c); c = GetChar ());For (; IsDigit (c); c = GetChar ()) ret = RET * + C-' 0 ';return ret;}int SEQ[MAXN], L[MAXN], R[MAXN];int N;struct Node {node* ch[2];} POOL[MAXN * n], *pt, *root;void init () {memset (pool, 0, sizeof pool);pt = pool; root = pt++;} 

void Insert (int x) {

node* t = root;for (int i = n; i--;) {int v = (x >> i) & 1;if (!t->ch[v]) t->ch[v] = pt++;t = t->ch[v];}}int Find (int x) {node* t = root;int ret = 0;for (int i = n; i--;) {int v = ((((x >> i) & 1) ^ 1);if (T->ch[v])ret |= 1 << i, t = t->ch[v];Elset = t->ch[v ^ 1];}return ret;}int main () {N = read ();for (int i = 1; I <= N; i++) Seq[i] = read ();int sum = l[0] = 0;init (); Insert (sum);for (int i = 1; I <= N; i++) {sum ^= seq[i];L[i] = max (l[i-1], Find (sum));Insert (sum);}sum = r[n + 1];init (); Insert (sum);for (int i = N; i; i--) {sum ^= seq[i];R[i] = max (r[i + 1], Find (sum));Insert (sum);}int ans = 0;for (int i = 1; i < N; i++)ans = max (ans, l[i] + r[i + 1]);printf ("%d\n", ans);return 0;}

-----------------------------------------------------------------------

4260:codechef rebxor Time limit: Sec Memory Limit: + MB
Submit: Solved: 76
[Submit] [Status] [Discuss] Description

The first line of input data contains an integer n representing the number of elements in the array. The second line contains n integers a1,a2,..., an.

The output line contains the maximum possible value for the given expression.

Sample Input5
1 2 3) 1 2Sample Output6HINT

The conditions are met (L1,R1,L2,R2): (1,2,3,3), (1,2,4,5), (3,3,4,5).
For 100% of data, 2≤n≤4*105,0≤ai≤109.

Source

by yts1999

Bzoj 4260:codechef Rebxor (trie)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.