Bzoj_3653_ Laughing _ Tree Array

Source: Internet
Author: User

Bzoj_3653_ Laughing _ Tree Array

Description set T for a tree with a root tree, we do the following definition:? Set A and B as two different nodes in T. If a is the ancestor of B, then it is said that "a than B does not know where the gaoming went". Set A and B as two different nodes in T. If the distance between A and B does not exceed a given constant x in the tree, then "A and B" is called "laughing". Given a root tree t of n nodes, the nodes are numbered 1 to n and the root node is node # 1th. You need to answer Q a question, ask the given two integers p and K, and ask how many ordered triples (A;B;C) satisfy: 1. A, B and C are three different points in T, and A is P node; 2. A and B are better than C do not know where to go; 3. A and B are laughing. The constant in the laughing here is the given K. The first line of input contains two positive integers n and q, each representing the number of points and queries that have a root tree. Next n-1 lines, each line describes an edge on a tree. Each row contains two integers u and V, which represents an edge between the node U and v. Next the Q line, each line describes an operation. Line I contains two integers representing the p and K of the I inquiry respectively. 1<=p<=n1<=k<=nn<=300000q<=300000output

Output Q line, each line corresponding to a query, representative of the answer to the question.

Sample Input5 3
0 S
1 3
2 4
4 5
2 2
4 1
2 3Sample Output3
1
3 b There are two possible positions, the ancestor of a and the subtree of a. If B is an ancestor of a, then C can only select one of the a subtree except a. If B is a point in the subtree of a, C can only select the B subtree except one of the B sub-trees. The problem is then transformed into a subtree with a distance less than or equal to K in the tree. Two restrictions: the subtree within and the depth is less than or equal to a value. The Dfs sequence position and depth of each point is considered as two coordinates, and transformed into two-dimensional points, which are solved with a tree-like array. Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define N 300050typedef Long Long ll;int head[n],to[n<<1],nxt[n<<1],cnt,n,m;int dep[n],siz[n],dfn[n],s[n],son[n]; ll c[n],ans[n];inline void Add (int u,int v) {to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;} void fix (int x,int v) {for (;x<=n;x+=x& (x)) C[x]+=v;} ll inq (int x) {ll re=0; for (;x;x-=x& (x)) re+=c[x]; return re;} struct Qaq {int p,d,id,opt;}     A[n<<1];bool CMP (const QAQ &x,const qaq &y) {if (X.P==Y.P) return x.opt<y.opt; return X.P&LT;Y.P;} void Dfs (int x,int y) {int i; S[++s[0]]=x; Dfn[x]=s[0]; dep[x]=dep[y]+1;    Siz[x]=1;    for (I=head[x];i;i=nxt[i]) if (to[i]!=y) {DFS (to[i],x); Siz[x]+=siz[to[i]]; } Son[x]=s[0];}    int main () {scanf ("%d%d", &n,&m);    int i,x,y;    for (i=1;i<n;i++) {scanf ("%d%d", &x,&y); Add (x, y); add (y,x);    } dfs (1,0);    int tot=0;        for (i=1;i<=m;i++) {scanf ("%d%d", &x,&y);        Ans[i]=1ll*min (y,dep[x]-1) * (siz[x]-1);        int depp=min (DEP[X]+Y,N); A[++TOT].P=DFN[X]; A[tot].opt=-1; A[tot].d=depp;        A[tot].id=i; A[++TOT].P=SON[X];  A[tot].opt=1; A[tot].d=depp;    A[tot].id=i;    } sort (a+1,a+tot+1,cmp);    int now=0;        for (i=1;i<=tot;i++) {while (NOW&LT;=N&AMP;&AMP;NOW&LT;A[I].P) Now++,fix (dep[s[now]],siz[s[now]]-1);    Ans[a[i].id]+=a[i].opt*inq (A[I].D); } for (i=1;i<=m;i++) printf ("%lld\n", Ans[i]);}

Bzoj_3653_ Laughing _ Tree Array

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.