BZOJ_3653_talking and laughing_tree array

BZOJ_3653_talking and laughing_tree array

Description

Let T be a rooted tree, we make the following definition:
• Let a and b be two distinct nodes in T. If a is an ancestor of b, then it is said that "a is more ignorant than b
Where has Gao Ming gone?"
• Let a and b be two distinct nodes in T. If a and b are not farther apart in the tree than a given
Constant x, then it is said that "a and b are chatting and laughing".
Given a rooted tree T of n nodes, the nodes are numbered 1 to n, and the root node is node 1. you need
To answer q queries, ask given two integers p and k, how many ordered triples (a;b;c) satisfy:
1. a, b, and c are three different points in T, and a is the node p;
2. Both a and b are better than c. I don't know where they are;
3. a and b chat and laugh. Here the constant in Talking and Laughing is a given k.

Input

The first line contains two positive integers n and q, which represent the number of points in the rooted tree and the number of queries, respectively.
Next n - 1 lines, each describing an edge in the tree. Each row contains two integers u and v, representing an edge between nodes u and v.
The next q lines, each describing an operation. The ith row contains two integers, representing p and k of the ith query, respectively.
1<=P<=N
1<=K<=N
N<=300000
Q<=300000

Output

Output q lines, each line corresponds to a query, representing the answer to the query.

Sample Input

5 3
1 2
1 3
2 4
4 5
2 2
4 1
2 3

Sample Output

3
1
3

 b has two possible positions, an ancestor of a and a subtree of a.
If b is an ancestor of a, then c can only choose one of the subtrees of a except a.
If b is a point in a subtree of a, c can only choose one of the subtrees of b except for b.
So the problem is transformed into the sum of the size of subtrees whose distance is less than or equal to k in the subtree.
Two constraints: subtree within and depth less than or equal to one value.
The dfs order position and depth of each point are regarded as two coordinates, converted into two-dimensional number points, and solved with the tree array.
 
Code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 300050
typedef 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<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); you [x] + = you [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<=n&&now<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]);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066826&siteId=291194637