Bzoj 1604: [Usaco2008 open]cow Neighborhoods Cow's neighbour Manhattan spanning tree

Source: Internet
Author: User

Approximate test instructions: The number of point pairs consisting of a Manhattan distance less than or equal to C in the statistical plane.

A template question for the Manhattan spanning tree. Related explanation: http://blog.csdn.net/acm_cxlove/article/details/8890003

  

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;#defineMAXN 100100#defineMaxe maxn*8#defineINF 0x3f3f3f3f#defineINFL 0X3F3F3F3F3F3F3F3FLLtypedefLong LongQword;intn,m;structedge{intx, y; Qword D;} E[maxe];inttope=-1;BOOLcmp_d (Edge E1,edge E2) {returne1.d<e2.d;}intUF[MAXN];intGET_FA (intNow ) {        returnUf[now]==now? now:uf[now]=Get_fa (Uf[now]);}intComb (intXinty) {x=Get_fa (x); Y=Get_fa (y); if(x==y)return false; UF[X]=y; return true;}structpoint{Qword x, y; intID;} PL[MAXN];BOOLcmp_x (Point p1,point p2) {if(p1.x==p2.x)returnP1.y>p2.y; returnP1.x>p2.x;}voidrotate1 () { for(intI=0; i<n;i++) pl[i].x=-pl[i].x;}voidRotate2 () { for(intI=0; i<n;i++) Pl[i].y=-pl[i].y;}voidRotate3 () { for(intI=0; i<n;i++) swap (PL[I].X,PL[I].Y);} Qword H[maxn],toph=-1; Qword TARR[MAXN];intTARR_ID[MAXN];voidAdd_tarr (intPos,qword V,intID) {         while(pos<MAXN) {Tarr[pos]=min (tarr[pos],v); if(V==tarr[pos]) tarr_id[pos]=ID; POS+=pos& (-POS); }}pair<qword,int> Qry_tarr (intPOS) {Pair<qword,int>ret; Ret.first=INFL;  while(POS) {Ret.first=min (Ret.first,tarr[pos]); if(ret.first==Tarr[pos]) Ret.second=Tarr_id[pos]; POS-=pos& (-POS); }        returnret;}voidWork () {inti,j; Toph=0; memset (Tarr,0x3f,sizeof(tarr));  for(i=0; i<n;i++) h[++toph]=pl[i].x-pl[i].y; Sort (H+1, h+toph+1); Toph=unique (H +1, h+toph+1)-h-1; Sort (PL,PL+n,cmp_x);  for(i=0; i<n;i++) {pair<qword,int>PR; Qword T=pl[i].x-pl[i].y; T=lower_bound (H +1, h+toph+1, T)-I; PR=Qry_tarr (t); if(pr.first!=infl) {e[++tope].x=pl[i].id; E[tope].y=Pr.second; E[TOPE].D=pr.first-pl[i].x-pl[i].y; } Add_tarr (t,pl[i].x+pl[i].y,pl[i].id); }}intIS_ROOT[MAXN],UF_SIZE[MAXN];intMain () {Freopen ("Input.txt","R", stdin); //freopen ("Output.txt", "w", stdout);        inti,j,k,x,y,z; scanf ("%d%d",&n,&m);  for(i=0; i<n;i++) {scanf ("%lld%lld",&pl[i].x,&pl[i].y); Pl[i].id=i; }        //1Work (); //2Rotate3 ();        Work ();        Rotate3 (); //3Rotate2 ();        Rotate3 ();        Work ();        Rotate3 ();        Rotate2 (); //4Rotate2 ();        Work ();        Rotate2 (); //5Rotate2 ();        Rotate1 ();        Work ();        Rotate1 ();        Rotate2 (); //6rotate1 ();        Rotate2 ();        Rotate3 ();        Work ();        Rotate3 ();        Rotate2 ();        Rotate1 (); //7rotate1 ();        Rotate3 ();        Work ();        Rotate3 ();        Rotate1 (); //8rotate1 ();        Work ();        Rotate1 (); Sort (e,e+tope+1, cmp_d);  for(i=0; i<=n;i++) uf[i]=i;  for(i=0; I<=tope && e[i].d<=m;i++) {comb (E[I].X,E[I].Y); }        intAns2=0;  for(i=0; i<n;i++) {IS_ROOT[GET_FA (i)]=1; Uf_size[uf[i]]++; }         for(i=1; i<n;i++) Is_root[i]+=is_root[i-1];  for(i=0; i<n;i++) Ans2=Max (ans2,uf_size[i]); printf ("%d%d\n", is_root[n-1],ans2); return 0;}

Bzoj 1604: [Usaco2008 open]cow Neighborhoods Cow's neighbour Manhattan spanning 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.