"BZOJ-4568" Lucky number tree chain split + linear base Merge

Source: Internet
Author: User

4568: [Scoi2016] Lucky number time limit:60 Sec Memory limit:256 MB
submit:238 solved:113
[Submit] [Status] [Discuss] There are n cities in the Descriptiona country, which are connected by the N-1 Road, allowing any two cities to reach each other, and the path is the only one. Each city has a lucky number, standing in the form of a monument in the heart of the city, as a symbol of the city. Some travelers want to visit country A. The traveler plans to land in the X city by plane, along the only route between the city of X and the city of Y, eventually departing from the city of Y. As you pass through each city, visitors will have the opportunity to take pictures of the city's lucky numbers and save the fortune on their own. Fortunately, however, it is not easy to superimpose, and it is clear to the visitors. They are superstitious about how lucky numbers are kept on their own in a different way or another. For example, the visitors took 3 photos and the lucky values were 5,7,11, so the lucky value that eventually remained on them was 9 (5 XOR 7 xor 11). Some smart visitors find that they can get a bigger lucky value by selectively taking pictures. For example, in the three lucky values above, only 5 and 11 are selected, and the lucky value that can be retained is 14. Now, some of the visitors have found the smart you, and want you to help them figure out the maximum number of lucky values that can be kept in their schedule. The first line of input contains 2 positive integers n, q, respectively, indicating the number of cities and the number of travelers. The second line contains n nonnegative integers, where the I integer Gi represents the lucky value of city I. The rows are then n-1, each line containing two positive integers x, Y, indicating that there is a road connected between City X and City Y. followed by q lines, each line contains two positive integers x, Y, indicating that the traveler's travel plan is from City X to City Y. N<=20000,q<=200000,gi<=2^60output

The output needs to contain q rows, each containing 1 nonnegative integers, indicating the maximum lucky value that the traveler can retain.

Sample Input4 2
11 5 7 9
1 2
1 3
1 4
2 3
1 4Sample Output14
OneHintsourcesolution

Linear Base on tree

The feeling can have multiple practices (1) tree chain split + linear base merge (2) lca+ multiplier + linear base merge (3) point Division + linear Base Merge

The latter two are $logn$ level, the first is the $log^{2}n$ level, their own start to calculate the complexity of the wrong, so happy to write (1), but Bzoj open to 60s time limit, so still can water over the

In general, the tree chain is divided, the segment tree maintenance interval linear basis, two leaf nodes of the linear basis of the combination is the root of the linear basis, the specific merger is the direct violence to insert one into another

Then the query when the normal query is good.

lca+ multiplication is the multiplication of the $2^{j}$ step of the linear basis, preprocessing and query, it is also a violent combination of linear radicals

Code
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstring>using namespacestd;Long LongRead () {Long Longx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 20010intN,q;Long LongVAL[MAXN];structedgenode{intNext,to;} edge[maxn<<1];inthead[maxn],cnt;voidAddintUintV) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;}voidInsertintUintv) {Add (u,v); add (v,u);}//==============================================================================================intsize[maxn],deep[maxn],fa[maxn],son[maxn],pl[maxn],sz,pr[maxn],top[maxn],father[maxn][ -];Long LongPRE[MAXN];voidDfs_1 (intNow ) {Size[now]=1;  for(intI=1; i<= -; i++)        if(Deep[now]>= (1<<i)) Father[now][i]=father[father[now][i-1]][i-1]; Else  Break;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Fa[now]) {Deep[edge[i].to]=deep[now]+1; Fa[edge[i].to]=now; father[edge[i].to][0]=Now ;                Dfs_1 (edge[i].to); if(Size[edge[i].to]>size[son[now]]) son[now]=edge[i].to; Size[now]+=Size[edge[i].to]; }}voidDfs_2 (intNowintchain) {Pl[now]=++sz; Pre[sz]=val[now]; top[now]=chain; if(Son[now]) dfs_2 (Son[now],chain);  for(intI=head[now]; I I=edge[i].next)if(Edge[i].to!=fa[now] && edge[i].to!=Son[now]) dfs_2 (edge[i].to,edge[i].to); Pr[now]=sz;}intLCA (intXinty) {    if(deep[x]<Deep[y]) swap (x, y); intdd=deep[x]-Deep[y];  for(intI=0; i<= -; i++)        if(dd& (1<<i)) x=Father[x][i];  for(intI= -; i>=0; i--)         if(father[x][i]!=Father[y][i]) x=father[x][i],y=Father[y][i]; if(x==y)returnXElse returnfather[x][0];}//==============================================================================================structxxj{Long Longa[ A];} Ans;structtreenode{intL,r; Xxj X;} tree[maxn<<2];voidMerge (Xxj &x,xxj &AMP;Y,XXJ &z)    {XXJ tmp;  for(intI= -; i>=0; i--) tmp.a[i]=Y.a[i];  for(intI= -; i>=0; i--)         if(Z.a[i]) {Long Longtp=Z.a[i];  for(intI= -; i>=0; i--)                    if(tp& (1ll<<i))if(!tmp.a[i]) {TMP.A[I]=TP; Break;} Elsetp^=Tmp.a[i]; }     for(intI= -; i>=0; i--) x.a[i]=tmp.a[i];}voidBuildtree (intNowintLintR) {TREE[NOW].L=l; Tree[now].r=R; if(l==r) {memset (Tree[now]. X.A,0LL,sizeof(Tree[now].            X.A)); Long Longtp=Pre[l];  for(intI= -; i>=0; i--)                    if(tp& (1ll<<i))if(!tree[now]. X.a[i]) {Tree[now]. X.A[I]=TP; Break;} Elsetp^=Tree[now].            X.a[i]; return; }    intMid= (l+r) >>1; Buildtree ( now<<1, L,mid); Buildtree (now<<1|1, mid+1, R); Merge (Tree[now]. X,tree[now<<1]. x,tree[now<<1|1]. X);}voidQuery (intNowintLintR) {    if(L&LT;=TREE[NOW].L && R&GT;=TREE[NOW].R) {Merge (Ans,ans,tree[now]. X);return;} intMid= (TREE[NOW].L+TREE[NOW].R) >>1; if(L<=mid) Query (now<<1, L,r); if(R>mid) Query (now<<1|1, l,r);}//==============================================================================================voidSolve_query (intXinty) {    intz=LCA (x, y); if(deep[x]<Deep[y]) swap (x, y);  while(deep[top[x]]>Deep[z]) Query (1, Pl[top[x]],pl[x]), x=Fa[top[x]]; Query (1, pl[z],pl[x]); if(y!=z) {intdd=deep[y]-deep[z]-1; z=y;  for(intI=0; i<= -; i++)                if(dd& (1<<i)) z=Father[z][i]; if(dd==0) z=y;  while(deep[top[y]]>Deep[z]) Query (1, Pl[top[y]],pl[y]), y=Fa[top[y]]; Query (1, Pl[z],pl[y]); }}//==============================================================================================intMain () {n=read (); q=read ();  for(intI=1; i<=n; i++) val[i]=read ();  for(intU,v,i=1; i<=n-1; i++) U=read (), v=read (), insert (U,V); Dfs_1 (1); Dfs_2 (1,1); Buildtree (1,1, N);  while(q--)        {            intX=read (), Y=read ();Long Longans=0; memset (ANS.A,0LL,sizeof(ANS.A));            Solve_query (x, y);  for(intI= -; i>=0; i--)if((Ans^ans.a[i]) >ans) ans^=Ans.a[i]; printf ("%lld\n", ans); }    return 0;}

Thanks YJQ Big Data, the thrill of a question before the exam

"BZOJ-4568" Lucky number tree chain split + linear base Merge

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.