Codeforces 282E. Sausage Maximization Trie

Source: Internet
Author: User

Topic links

Give the number of N, let you find a prefix and a suffix, they are different or complete after the maximum, the prefix and suffix are different or derived, can be empty, as empty when the 0 count. Prefix suffixes are not overwritten.

This question is good God, unexpectedly is trie tree ...

First, the XOR of all numbers is used as the initial suffix and the initial prefix is 0. Then insert the prefix in the dictionary tree, in the suffix to find, find, from the high to low, if the suffix of the I bit is 0, then look for the dictionary tree in this bit there is no 1, there are 1 to 1 of the road to find, not only to 0 that road to find.

See the code specifically.

#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };structtrie{Trie*next[2]; Trie () {next[0] = next[1] =NULL; }};ll num[100005], top = +; Trie*root =NewTrie ();voidAdd (ll val) {Trie*p =Root;  for(inti = top-1; i>=0; i--) {        intTMP = val>>i&1; if(!p->next[tmp]) p-&GT;NEXT[TMP] =NewTrie (); P= p->Next[tmp]; }}ll query (ll val) {Trie*p =Root; LL ret=0;  for(inti = top-1; i>=0; i--) {        intTMP = val>>i&1; if(p->next[tmp^1]) {ret|= 1ll<<i; TMP^=1; } P= p->Next[tmp]; }    returnret;}intMain () {intN; ll prefix=0, suffix =0, ans =0; CIN>>N;  for(inti =1; i<=n; i++) {cin>>Num[i]; Suffix^=Num[i]; }     for(inti =1; i<=n+1; i++) {Add (prefix); Ans=max (ans, query (suffix)); if(i<=N) {prefix^=Num[i]; Suffix^=Num[i]; }} cout<<ans<<Endl; return 0;}

Codeforces 282E. Sausage Maximization 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.