hdu--4825 Xor Sum (dictionary tree)

Source: Internet
Author: User

Title Link: hdu--4825 Xor Sum

MMP SB Dictionary tree because the array is not large enough, WA is not the re!!!. Looking for an afternoon bug grass

Convert each number into a binary memory dictionary tree and try to take a different position from the X.

First, one of the most primitive codes is written like crap.

#include <iostream>#include<cstring>#include<algorithm>#include<string.h>#include<stdio.h>#include<cmath>#include<vector>using namespacestd;#defineMAXN 100010Long Longtot=0;structac{Long Longsum,fa,nex[3]; voidinit () {sum=0; fa=0; memset (NEX,0,sizeof(NEX)); }}TRE[MAXN* -];voidAddCharA[],Long LongII) {//Translate this into a string hereLong LongI=0, j=0, k=0;  while(A[i]) {Long Longz=a[i]-'0'; if(Tre[k].nex[z]) {J=Tre[k].nex[z]; Tre[j].sum++; }Else{Tre[k].nex[z]=++tot; J=tot;          Tre[j].init (); Tre[j].sum++; } k=J; I++; } TRE[K].FA=II;}Long LongQueryChara[]) {   Long LongI=0, j=0, k=0; Long Longs=0;  while(A[i]) {Long Longz=a[i]-'0'; if(z==0){         if(tre[k].nex[1]) {J=tre[k].nex[1]; }Elsej=tre[k].nex[0]; }Else{         if(tre[k].nex[0]) {J=tre[k].nex[0]; }Elsej=tre[k].nex[1]; } k=J; I++; }   returnTre[k].fa;}Long LongC[maxn],cnt=0;voidinit () {tot=0; cnt=0; Memset (Tre,0,sizeof(tre));}Chara[ -],b[ -];intMain () {Long LongT,ant=1; scanf ("%lld",&t);  while(t--) {init (); Long Longn,m; scanf ("%lld%lld",&n,&m);  for(Long Longj=0; j<n;j++){         Long Longx; scanf ("%lld",&x); C[cnt++]=x; Long Longlen=0;  while(x) {Long Longi=x%2; A[len++]=i+'0'; X/=2; }         Long Longz=len-1;  for(Long Longk=0; k<= +; k++){           if(( +-K) = =z) {B[k]=A[z]; Z--; }Elseb[k]='0'; } Add (b,c[cnt-1]); } printf ("Case #%lld:\n", ant++);  for(Long Longj=0; j<m;j++){         Long Longx; scanf ("%lld",&x); Long LongIi=x; Long Longlen=0;  while(x) {Long Longi=x%2; A[len++]=i+'0'; X/=2; }         Long Longz=len-1;  for(Long Longk=0; k<= +; k++){           if(( +-K) = =z) {B[k]=A[z]; Z--; }Elseb[k]='0'; }         Long LongI=query (b); printf ("%lld\n", i); }   }}

The improvement was much more enjoyable.

#include <bits/stdc++.h>using namespacestd;#defineMAXN 3000000+5intA[MAXN];inttot;structac{intfa,nex[3]; voidinit () {FA=-1; Memset (nex,-1,sizeof(NEX)); }}TRE[MAXN];voidAddintXinty) {  intk=0;  for(intj= to; j>=0; j--){     BOOLZ= ((1&LT;&LT;J) &x); if(tre[k].nex[z]==-1) {Tre[k].nex[z]=++tot;     Tre[tot].init (); } k=Tre[k].nex[z]; }  //cout<<k<< "" <<y<<endl;Tre[k].fa=y;}intQueryintx) {   intk=0;  for(intj= to; j>=0; j--){      BOOLZ= ((1&LT;&LT;J) &x); if(tre[k].nex[z^1]!=-1) {k=tre[k].nex[z^1]; }      Elsek=Tre[k].nex[z]; }   returnTre[k].fa;}voidinit () {memset (tre,0,sizeof(tre)); Tot=0; tre[0].init ();}intMain () {intT,ant=1; CIN>>T;  while(t--) {init (); intn,m; scanf ("%d%d",&n,&m);  for(intj=0; j<n;j++){         intx; scanf ("%d",&x);      Add (x,x); } printf ("Case #%d:\n", ant++);  for(intj=0; j<m;j++){         intx; scanf ("%d",&x); printf ("%d\n", query (x)); }   }}

hdu--4825 Xor Sum (dictionary tree)

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.