COJ966 WZJ data structure (minus 34)

Source: Internet
Author: User

WZJ data structure (minus 34)
Difficulty level: C; operating time limit: 20000ms; operating space limit: 262144KB; code length limit: 2000000B
Question Description
to a tree of n nodes, for the shape of "u r" q query, the answer is centered on the u node, radius r within the node, the maximum weight of the node number is. If there are multiple nodes, returns the smallest number.
Input
There is a set of test data.
The first line contains an integer n (1≤n≤10^5) that represents the total number of nodes.
The next line, containing n numbers, represents the Weight VI (1≤vi≤10^6) of each node. The next n-1 line, with three integers per line (AI, Bi, WI), represents an edge that connects the AI, the BI node, and the edge length to WI (1≤ai, Bi≤n, 1≤wi≤3).
The next line contains an integer q, which indicates the total number of queries (1≤Q≤10^5). The next Q line, each row contains two integers u, r (1≤u≤n, 0≤r≤300), which indicates the number of nodes with the largest weight in the node with the U node as the center and radius R. If there is more than one solution returns the smallest number.
Output
For each set of queries, the output line represents the answer.
Input example
7
1 2 3 4 5 6 7
1 2 1
2 3 1
2 4 1
1 5 1
5 6 1
5 7 1
4
1 1
1 2
2 1
2 2
Output example
5
7
4
5
Other Notes
The example is evil.

Consider using point division to solve the problem offline, then the question is converted to how to solve the overweight x question Max (val[y]|depx+depy<=r).

We can use the usual practice of maintaining DEP, Val simultaneously incrementing the decision sequence, which can be done with a balanced tree, and then swept back and forward.

However, it is not necessary to note that X, y in the same subtrees tree does not affect the answer (think, why), so just offline to construct the decision sequence and then two points on the line.

#include <cstdio>#include<cctype>#include<queue>#include<stack>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=100010;intfirst[maxn],next[maxn<<1],to[maxn<<1],dis[maxn<<1],e;voidAddedge (intWintVintu) {to[++e]=v;dis[e]=w;next[e]=first[u];first[u]=e; to[++e]=u;dis[e]=w;next[e]=first[v];first[v]=e;}intN,Q,VAL[MAXN],ANS[MAXN];intBetterintXinty) {if(val[x]<val[y]| | (Val[x]==val[y]&&x>y))return 0; return 1;}voidRelaxint& X,intY) {if(Better (y,x)) x=y;}structQuery {intX,r,id,next;} Q[MAXN];intfirst2[maxn],cnt;voidAddQuery (intIdintRintx) {q[++cnt]= (Query) {x,r,id,first2[x]};first2[x]=CNT;}intVis[maxn],f[maxn],s[maxn],size,root;voidGetroot (intXintFA) {S[x]=1;intmaxs=0; renif(to[i]!=fa&&!Vis[to[i]])        {getroot (to[i],x); S[X]+=s[to[i]];maxs=Max (Maxs,s[to[i]]); } F[x]=max (maxs,size-s[x]); if(F[root]>f[x]) root=x;}intTOT,NUM[MAXN],DEP[MAXN],ID[MAXN],A[MAXN],B[MAXN];voidDfsintXintFaintD) {num[++tot]=x;dep[tot]=e; renif(To[i]!=fa&&!vis[to[i]]) DFS (to[i],x,d+dis[i]);}intcmpintXintY) {returndep[x]<dep[y]| | (dep[x]==dep[y]&&val[num[x]]>Val[num[y]]);}voidSolveintx) {vis[x]=1; tot=0;d FS (x,0,0); Rep (I,1, tot) id[i]=i; Sort (ID+1, id+tot+1, CMP); inttmp=tot;tot=0; Rep (I,1, TMP)if(Better (Num[id[i]],a[tot])) a[++tot]=num[id[i]],b[tot]=Dep[id[i]]; Rep (I,1, TMP) for(intj=first2[num[i]];j;j=Q[j].next) {        intL=1, r=tot+1;  while(L +1<r) {intMid=l+r>>1; if(B[mid]<=q[j].r-dep[i]) l=mid; ElseR=mid; }        if(b[l]<=q[j].r-Dep[i]) relax (ans[q[j].id],a[l]); } renif(!Vis[to[i]]) {Size=f[0]=s[to[i]];getroot (to[i],root=0);    Solve (root); }}intMain () {n=read (); Rep (I,1, N) val[i]=read (); Rep (I,2, N)    Addedge (read (), read (), read ()); Q=read (); Rep (I,1, Q)    AddQuery (I,read (), read ()); Size=f[0]=n;getroot (1,0);    Solve (root); Rep (I,1, q) printf ("%d\n", Ans[i]); return 0;}
View Code

COJ966 WZJ data structure (minus 34)

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.