bzoj3551 Peaks Enhanced Edition

Source: Internet
Author: User

This question ... Feel that offline and online code is almost as difficult (Pb_ds don't talk).

Offline, is to put all the queries according to the order of W, and then one side kruskal+ the balance tree heuristic merge side to answer the question.

It's not hard to write online. First Kruskal the refactoring tree (the Kruskal is the kind that does not merge by rank and adds a virtual point ...), then the points that can be reached at each point must be in a subtree. The DFS sequence of the subtree is continuous, so the DFS sequence can be built as a tree to find the interval k large. And because only the point of the leaf node is meaningful, so you can only build the tree of the DFS sequence of the leaves. When querying, multiply jumps to the top of the w<= asking for the W Point and then the Chairman tree is ready.

In fact, the tree split jump father can also, first jump the whole chain, the whole chain is not moving in the last chain of two points, also O (Logn). But it may be too weak, two points write hung, the result wa to die ... Helpless to rewrite it again with multiplication.

Limited time 20s, the result I ran 19.8s, this speed is really touching ...

Also, the copy of the A began to forget + 1, a lesson, a false death ...

1 /**************************************************************2 problem:35513 User:hzoier4 language:c++5 result:accepted6 time:19800 Ms7 memory:114432 KB8 ****************************************************************/9#include <cstdio>Ten#include <cstring> One#include <algorithm> A using namespacestd; - Const intmaxn=200010, maxe=500010; - structedge{ the     int  from, to,w; -     BOOL operator< (ConstEdge &e)Const{returnw<E.W;} -}e[maxe+MAXN]; - voidKruskal (); + intFindrootint); - voidMergeset (int,int); + voidDfsint); A voidBuildint,int,int&,int); at voidQueryint,int,int,int); - intsm[maxn<<5],lc[maxn<<5],rc[maxn<<5],root[maxn],tree_cnt=0; - intn,m=0, m,q,h[maxn],a[maxn],cnt,prt[maxn],w[maxn],f[maxn][ -],ch[maxn][2],l[maxn],r[maxn],pr=0, X,d,k,ans; - intMain () { -scanf"%d%d%d",&n,&m,&q); -      while((1&LT;&LT;M) < (n<<1)) m++; in      for(intI=1; i<=n;i++) scanf ("%d",&h[i]); -Copy (H +1, h+n+1, A +1); toSort (A +1, a+n+1); +      for(intI=1; i<=n;i++) H[i]=lower_bound (A +1, a+n+1, H[i])-A; -      for(intI=1; i<=m;i++) scanf ("%d%d%d", &e[i]. from,&e[i].to,&E[I].W); the      for(intI=2; i<=n;i++){ *E[++M]. from=1; $e[m].to=i;Panax NotoginsengE[m].w=~ (1<< to); -     } theCnt=N; + Kruskal (); A DFS (CNT); the      for(intj=1; j<=m;j++) for(intI=1; i<=cnt;i++) f[i][j]=f[f[i][j-1]][j-1]; +      while(q--){ -scanf"%d%d%d",&x,&d,&k); $         if(ans!=-1) {x^=ans;d^=ans;k^=ans;} $          for(intj=m;j!=-1; j--)if(F[x][j]&&w[f[x][j]]<=d) x=F[x][j]; -Query1, n,root[r[x]],root[l[x]-1]); -printf"%d\n", ans); the     } -     return 0;Wuyi } the voidKruskal () { -      for(intI=1; i<=n;i++) prt[i]=i; WuStable_sort (e+1, e+m+1); -      for(intI=1; i<=m;i++)if(Findroot (e[i). from)!=Findroot (e[i].to)) { Aboutcnt++; $prt[cnt]=CNT; -w[cnt]=E[I].W; -ch[cnt][0]=findroot (E[i]. from); -ch[cnt][1]=Findroot (e[i].to); AMergeset (E[i]. from, CNT); + Mergeset (e[i].to,cnt); the     } - } $ intFindrootintx) {returnPrt[x]==x?x: (prt[x]=Findroot (prt[x]));} the voidMergeset (intXintY) {prt[findroot (x)]=findroot (y);} the voidDfsintx) { the     if(ch[x][0]){ thef[ch[x][0]][0]=f[ch[x][1]][0]=x; -DFS (ch[x][0]); inDFS (ch[x][1]); thel[x]=l[ch[x][0]]; ther[x]=r[ch[x][1]]; About     } the     Else{ thek=H[x]; theBuild1, n,root[pr+1],ROOT[PR]); +l[x]=r[x]=++PR; -     } the }Bayi voidBuildintLintRint&rt,intPR) { thesm[rt=++tree_cnt]=sm[pr]+1; the     if(L==R)return; -lc[rt]=lc[pr];rc[rt]=RC[PR]; -     intMid= (l+r) >>1; the     if(k<=mid) Build (L,MID,LC[RT],LC[PR]); the     ElseBuild (mid+1, R,RC[RT],RC[PR]); the } the voidQueryintLintRintRtintPR) { -     if(sm[rt]-sm[pr]<k) { theans=-1; the         return; the     }94     if(l==R) { theans=A[l]; the         return; the     }98     intMid= (l+r) >>1; About     if(K&LT;=SM[RC[RT]]-SM[RC[PR]]) query (mid+1, R,RC[RT],RC[PR]); -     Else{101k-=sm[rc[rt]]-SM[RC[PR]];102 query (L,MID,LC[RT],LC[PR]);103     }104}
View Code

The end and the beginning, there is always one waiting for you.

bzoj3551 Peaks Enhanced Edition

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.