hdu-5314 Happy King

Source: Internet
Author: User

Test instructions

Give a tree of n nodes, point the right value;

Find the difference between the maximum and minimum values for a pair (x, y) that satisfies x!=y and x to Y paths <=D;

n<=100000, multiple sets of data, sum of all data n <=500000;


Exercises

To fill in the pits that were dug in the year;

This data range is really malicious. It's not good to say five groups of data directly!

Consider this question how to do, on the day before the exam, I learned the tree partition algorithm;

Then he went out, then I wrote it, and then I couldn't write it out;

I was really naive that year;

I turned out the code that was handed in, changed for a long time.

First of all, the idea of this problem is relatively easy, the most intuitive is the point of the tree to treat it;

After divided the statistical answer, first search out from the current root to all points of the maximum weight and the minimum weight value;

Then in order to count, we first by the maximum weight from small to large order;

So if this maximum weight is the maximum weight of the road, then the other endpoint must be sorted in front of it;

So the minimum value of the point is inserted into the tree array while traversing;

Each query [0, point to root Path maximum value-d] This interval value is good;

Complexity O (nlog^2n), others have more superior log practices;

Good mouth. A yes, but I was hopeless.

Every time I find the center of gravity, I don't use the center of gravity to divide the treatment!

The difference between the two codes is one letter! Then the complexity did not swap me half a day.


Code:


#pragma COMMENT (linker, "/stack:102400000,102400000") #include <ctype.h> #include <stdio.h> #include < string.h> #include <algorithm> #define N 100100using namespace Std;typedef long long ll;struct pr{int ma,mi; friend bool operator < (PR A,PR b) {return a.ma<b.ma;}} Popo[n];int to[n<<1],nex[n<<1],head[n],val[n];int size[n],dis[n],len;int D,tot,mi,G,bk,cnt;ll ans;int    Sum[n];bool ban[n];void init () {memset (head,0,sizeof (head));    memset (ban,0,sizeof (ban)); Tot=0,ans=0;} inline char getc () {static char buf[1<<15],*s,*t;if (s==t) {t= (s=buf) +fread (Buf,1,1<<15,stdin); if (s==t) return EOF;} return *s++;} inline int read () {static char ch;static int d;while (!isdigit (Ch=getc ())); for (d=ch-' 0 '; IsDigit (Ch=getc ());) d=d*10+ ( ch-' 0 '); return D;} inline int lowbit (int x) {return x& (-X);} inline int lb (int x) {int L=1,r=len,mid;while (l<=r) {mid=l+r>>1;if (dis[mid]<x) l=mid+1;elser=mid-1;} return l;}    void Update (int x,int val) {while (X<=len) {    Sum[x]+=val;    X+=lowbit (x);    }}int query (int x) {if (x<=0) return 0;    int ret=0;        while (x) {ret+=sum[x];    X-=lowbit (x); } return ret;}    void Add (int x,int y) {to[++tot]=y;    NEX[TOT]=HEAD[X]; Head[x]=tot;}    void Get_g (int x,int pre) {size[x]=1;    int i,y,temp=0;            for (I=head[x];i;i=nex[i]) {if (!ban[y=to[i]]&&y!=pre) {get_g (y,x);            Size[x]+=size[y];        Temp=max (Temp,size[y]);    }} Temp=max (Temp,bk-size[x]); if (Temp<mi) mi=temp,g=x;}    void Dfs (int x,int pre,int ma,int mi) {if (ma-mi>d) return;    Ma=max (Ma,val[x]);    Mi=min (Mi,val[x]);    Static PR p;    P.ma=ma,p.mi=mi;    Popo[++cnt]=p;    int i,y;        for (I=head[x];i;i=nex[i]) {if (!ban[y=to[i]]&&y!=pre) {DFS (Y,X,MA,MI);    }}}ll Calc (int x) {static PR p;p.ma=p.mi=val[x];    Popo[cnt=1]=p;    int i,j,y,last;    ll ret=0;      for (J=head[x];j;j=nex[j]) {  if (!ban[y=to[j]]) {last=cnt;            DFS (y,x,val[x],val[x]);            Sort (popo+last+1,popo+cnt+1); for (i=last+1;i<=cnt;i++) {if (Popo[i].ma-popo[i].mi<=d) ret+=i-last-1-query (LB (popo[i].ma-d                )-1);            Update (LB (POPO[I].MI), 1);        } for (i=last+1;i<=cnt;i++) update (LB (POPO[I].MI),-1);    }} Ret=-ret;    Sort (popo+1,popo+cnt+1);        for (i=1;i<=cnt;i++) {if (Popo[i].ma-popo[i].mi<=d) ret+=i-1-query (LB (popo[i].ma-d)-1);    Update (LB (POPO[I].MI), 1);    } for (i=1;i<=cnt;i++) update (LB (POPO[I].MI),-1); return ret;}    void slove (int x) {ban[x]=1;    if (bk==1) {Ban[x]=0;return;}    int i,y;            for (I=head[x];i;i=nex[i]) {if (!ban[y=to[i]]) {bk=size[y];            mi=0x3f3f3f3f;            Get_g (Y,X);        Slove (G); }}ans+=calc (x); ban[x]=0;}    int main () {//freopen ("tt.in", "R", stdin); int C,T,n,m,i,j,k,x,y;    T=read ();        for (c=1;c<=t;c++) {init ();        N=read (), D=read ();            for (i=1;i<=n;i++) {val[i]=read ();        Dis[i]=val[i];        } sort (dis+1,dis+n+1);        Len=unique (dis+1,dis+n+1)-dis-1;            for (i=1;i<n;i++) {x=read (), Y=read ();        Add (x, y), add (y,x);        } bk=n;        mi=0x3f3f3f3f;        Get_g (1,0);        Slove (G);    printf ("%lld\n", ans<<1); } return 0;}



hdu-5314 Happy King

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.