BZOJ4260 Codechef Rebxor

Source: Internet
Author: User

Main topic:

Given a length ofNThe sequence, ask1≤l1≤r1<l2≤r2≤n Makes(⊙R1I=L1AI) + (⊕r2 i =l 2a i) Maximum, output this maximum value.

Ideas:

Use Trie to find the prefix xor sum and suffix xor, and then find the prefix xor and the suffix XOR and the largest, before and after the sum, to find the maximum value. It is also possible to use the trie to find a different or more persistent.

Code:

Trie

1#include <cstdio>2#include <cstring>3#include <iostream>4 #defineM 4000095 using namespacestd;6 intcnt,ans,child[m<<5][2],a[m],lmax[m],rmax[m];7 8 voidBuildintx)9 {Ten     inti,w,now=0; One      for(i=1<< -; i;i>>=1) A     { -w= (x&i)?1:0; -         if(!child[now][w]) child[now][w]=++CNT; thenow=Child[now][w]; -     } - } -  + intCalintx) - { +     inti,w,now=0, ans=0; A      for(i=1<< -; i;i>>=1) at     { -w= (x&i)?0:1; -         if(Child[now][w]) ans+=i,now=Child[now][w]; -         Elsenow=child[now][!W]; -     } -     returnans; in } -  to intMain () + { -     intN,i,now; scanf"%d",&n); the      for(i=1; i<=n;i++) scanf ("%d",&a[i]); *      for(Build (now=0), i=1; i<=n;i++) $     {Panax NotoginsengBuild (now^=a[i]); -Lmax[i]=max (Cal (now), lmax[i-1]); the     } +memset (Child,0,sizeof(child)); A      for(Build (now=cnt=0), i=n;i;i--) the     { +Build (now^=a[i]); -Rmax[i]=max (Cal (now), rmax[i+1]); $     } $      for(i=0; i<=n;i++) Ans=max (ans,lmax[i]+rmax[i+1]); -printf"%d\n", ans); -     return 0; the}

Durable Trie:

1#include <cstdio>2#include <iostream>3 #defineM 4000094 using namespacestd;5 6 intsum[m<<5],lc[m<<5],rc[m<<5],a[m],root[m<<5],cnt;7 8 intRead ()9 {Ten     intx=0; One     CharCh=GetChar (); A      while(ch<'0'|| Ch>'9') ch=GetChar (); -      while(ch>='0'&& ch<='9') x= (x<<3) + (x<<1) +ch- -, ch=GetChar (); -     returnx; the } -  - voidBuildint&cur,int_cur,intXintk) - { +sum[cur=++cnt]=sum[_cur]+1;if(k<0)return; -     if(x& (1<<k)) Lc[cur]=lc[_cur],build (rc[cur],rc[_cur],x,k-1); +     ElseRc[cur]=rc[_cur],build (lc[cur],lc[_cur],x,k-1); A } at  - intAskintXintYintval) - { -     inti,ans=0; -      for(i= -; i>=0; i--) -         if(val& (1<<i)) in             if(Sum[lc[y]]-sum[lc[x]]) ans|=1<<i,x=lc[x],y=Lc[y]; -             Elsex=rc[x],y=Rc[y]; to         Else if(Sum[rc[y]]-sum[rc[x]]) ans|=1<<i,x=rc[x],y=Rc[y]; +              ElseX=LC[X], y=Lc[y]; -     returnans; the } *  $ intMain ()Panax Notoginseng { -     intN=read (), i,mx=0, ans=0; the      for(i=1; i<=n;i++) A[i]=read () ^a[i-1]; +      for(i=1; i<=n;i++) Build (root[i],root[i-1],a[i], -); A      for(i=n-1; i;i--) the     { +mx=Max (Mx,ask (root[i],root[n],a[i)); -Ans=max (Ans,mx+ask (root[1],root[i],a[i])); $     } $printf"%d", ans); -     return 0; -}

BZOJ4260 Codechef Rebxor

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.