hdu5314 Happy King

Source: Internet
Author: User
<span id="Label3"></p><p><p>The tree divided the Rule.</p></p><p><p></p></p><p><p>Code</p></p><pre class="brush:cpp;gutter:true;">#pragma comment (linker, "/stack:102400000,102400000") #include <cstdio> #include <cstring> #include < Algorithm> #define LL Long long#define N 300010using namespace Std;int dp,pre[n],p[n],tt[n],cnt,q[n]; LL c[n],ans; int n,i,a,b,w[n],vis[n],s[n],tmp,d,tot1,tot2,v[n];struct G{int a, b;} A[n],b[n];bool cmp (g x,g y) {return x.b<y.b;} void link (int x,int y) {dp++;p re[dp]=p[x];p [x]=dp;tt[dp]=y;} int lowbit (int x) {return x& (-x);} void cc (int x,int w) {while (x<=n) {c[x]+=w; X=x+lowbit (x); }}ll getsum (int x) {LL ans=0; While (x>0) {ans=ans+c[x]; X=x-lowbit (x); } return ans; void Dfs (int x,int fa,int min,int Max) {int i; Min=min (min,w[x]); Max=max (max,w[x]); If (max-min<=d) {tot1++; a[tot1].a=min; a[tot1].b=max; tot2++; b[tot2]=a[tot1];} elsereturn;i=p[x]; While (i) {if ((!vis[tt[i]) && (tt[i]!=fa)) dfs (tt[i],x,min,max); i=pre[i]; }}void Getroot (inT x,int fa,int Sum) {int i,flag=0; i=p[x];s[x]=1; While (i) {if (!vis[tt[i]) && (tt[i]!=fa)) {getroot (tt[i],x,sum); s[x]+=s[tt[i]]; If (s[tt[i]]>sum/2) flag=1; } i=pre[i]; } If (sum-s[x]>sum/2) flag=1; If (!flag) tmp=x;} int EF (int x) {int l,r,m; l=1;r=n; While (l<=r) {m= (l+r) >>1; If (v[m]>x) r=m-1; else l=m+1; } return r;} void work (int X,int sum) {int root,i,ta,tb; Getroot (x,0,sum); root=tmp; i=p[root]; vis[root]=1; While (i) {if (!vis[tt[i]]) {if (s[tt[i]]>s[root]) work (tt[i],sum-s[root]); else Work (tt[i],s[tt[i]]); } i=pre[i]; } tot1=1; a[tot1].a=w[root]; a[tot1].b=w[root]; i=p[root]; While (i) {if (!vis[tt[i]]) {tot2=0; DFS (tt[i],0,w[root],w[root]); Sortb+1,b+1+tot2,cmp); cnt=0; For (int J=1;j<=tot2;j++) {ta=ef (b[j].b); Tb=ef (b[j].b-d-1); Ans=ans-(getsum (ta)-getsum (tb)); Ta=ef (b[j].a); cnt++;q[cnt]=ta; CC (ta,1); } for (int J=1;j<=cnt;j++) cc (q[j],-1); } i=pre[i]; } Sort (a+1,a+1+tot1,cmp); cnt=0; For (int J=1;j<=tot1;j++) {ta=ef (a[j].b); Tb=ef (a[j].b-d-1); ans=ans+ (getsum (ta)-getsum (tb)); Ta=ef (a[j].a); cnt++;q[cnt]=ta; CC (ta,1); } for (int J=1;j<=cnt;j++) cc (q[j],-1); vis[root]=0; }int main () {int test; scanf ("%d", &test); While (test) {test--; Dp=0;memset (p,0,sizeof (p)); scanf ("%d%d", &n,&d); For (i=1;i<=n;i++) {scanf ("%d", &w[i]); v[i]=w[i]; } Sort (v+1,v+1+n); For (i=1;i<=n-1;i++) {scanf ("%d%d", &a,&b); Link (a, b); Link (b,a); } ans=0; Work (1,n); printf ("%i64d\n", ans*2); }}</pre><p><p>  </p></p><p><p>hdu5314 Happy King</p></p></span>
Related Article

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.