BZOJ3653: Laughing

Source: Internet
Author: User

Description

Set T to a tree with a root tree, we do the following definitions:
• Set A and B as two different nodes in T. If a is an ancestor of B, then it is called "a than B does not know
Where did gaoming go? "
• Set A and B as two different nodes in T. If the distance between A and B in the tree does not exceed a given
Constant X, then called "A and B" 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 queries, ask for a 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 the P node;
2. A and b are better than C to know where the wise go;
3. A and B are laughing. The constant in the laughing here is the given K.

Input

The first line of the input file 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.

Output

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

Sample Input5 3
1 2
1 3
2 4
4 5
2 2
4 1
2 3
Sample Output
3
1
3
HINT


1<=p<=n

1<=k<=n

n<=300000

q<=300000

Ah, the clam problem ... According to the definition is not difficult to find that ABC three points should be the same in a straight chain, then we press B on the top and the following 2 categories the first case is very simple, because a subtree of all nodes except a can be used as C. The second case is a partial-order structure, where B should be within a subtree and the depth of B does not exceed the depth of a +k. Because the problem can be taken offline, the size information can be maintained by merging it with a tree of balanced trees and line segments. Because the memory is richer, I'm writing a line tree merge.
#include <cstdio> #include <cctype> #include <queue> #include <cstring> #include <algorithm > #define REP (i,s,t) for (int. i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for (int i=first[x ];i;i=next[i]) using namespace Std;const int Buffersize=1<<16;char Buffer[buffersize],*head,*tail;inline Char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin); tail= (Head=buffer) +l;} return *head++;}    inline int read () {int X=0,f=1;char c=getchar (); for (;!    IsDigit (c); C=getchar ()) if (c== '-') f=-1;    for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} typedef long LONG ll;const int Maxn=300010;const int maxnode=maxn*20;struct Query {int k,id,next;} Q[maxn];int n,m,first2[maxn],first[maxn],next[maxn<<1],to[maxn<<1],dep[maxn],siz[maxn],e,cnt;void Addedge (int u,int v) {to[++e]=v;next[e]=first[u];first[u]=e;to[++e]=u;next[e]=first[v];first[v]=e;} void addquery (int x,int k,int ID) {q[++cnt]= (Query) {k,id,first2[x]};first2[x]=cnt;}ll s[maxnode],ans[maxn];int ls[maxnode],rs[maxnode],root[maxn],tot;void Build (int& o,int l,int r,int p,int val) { if (!o) o=++tot;s[o]+=val;if (l==r) return;int mid=l+r>>1;if (p<=mid) build (ls[o],l,mid,p,val); else Build (rs[ O],mid+1,r,p,val);} int merge (int x,int y) {if (!x) return y;if (!y) return X;s[x]+=s[y];ls[x]=merge (Ls[x],ls[y]); Rs[x]=merge (Rs[x],rs[y]); return x;} ll query (int o,int l,int r,int x) {if (!o) return 0;if (l==r) return s[o];int mid=l+r>>1;if (x<=mid) return query (LS [O],l,mid,x]; return S[ls[o]]+query (rs[o],mid+1,r,x);} void Dfs (int x,int fa) {Siz[x]=1;dep[x]=dep[fa]+1;ren if (TO[I]!=FA) DFS (to[i],x), Siz[x]+=siz[to[i]],root[x]=merge ( Root[x],root[to[i]]); for (int i=first2[x];i;i=q[i].next) {int k=q[i].k,j=q[i].id;ans[j]= (LL) (siz[x]-1) *min (k,dep[x ]-1); Ans[j]+=query (Root[x],1,n,min (N,dep[x]+k));} Build (root[x],1,n,dep[x],siz[x]-1);} int main () {n=read (); M=read (); Rep (i,2,n) Addedge (read (), read ()), Rep (i,1,m) {int p=read (), K=read (); AddQuery (p,k,i);} DFS (1,0); Rep (i,1,m) printf ("%lld\n", Ans[i]); return 0;} 

  

BZOJ3653: Laughing

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.