LUOGU 3899: [Hunan training] laughing segment tree merger

title

LUOGU 3899

Description

Let T be a rooted tree, we make the following definition:

• a and b are provided in two different nodes of T. If you are a b ancestors, then called "a clever than b does not know where to go."

• a and b are provided in two different nodes of T. If a and b of the trees in the distance does not exceed a given constant x, then called "a and b are laughing."

Given an n nodes rooted tree T, the node numbered 1 ~ n, the root node is a node number. You need to answer the q inquiry, inquiry given two integers p and k, asked how many ordered triples \ ((a, b, c ) \) is satisfied:

  1. a, b and c are three different points T, and p a is the node number;
  2. c and b than a clever not know where to go;
  3. a and b laughing. K is a constant given herein is talking.

analysis

Segment tree merge.

Because claim triplet \ ((a, b, c ) \) in \ (a, b \) are \ (C \) of ancestors, only two situations

  • \ (b \) is \ (a \) ancestors

    Statistics like this case directly, the answer is \ (\ min \ {depth ( a) -1, k \} (size (a) -1) \) where \ (depth (a) \) of \ (A \) depth, \ (size (a) \) is the size of subtree

  • \ (a \) is \ (b \) ancestors

    Pick any one to meet the requirements of \ (b \) , then \ (b \) subtree optionally a \ (c \) can satisfy answer. The answer is \ (\ SUM \ limits_ depth {(A) + k≥depth (B)} size (B) -1 \) . To this thing to calculate, for each node maintains a depth-number to \ (size (x) -1 \ ) is the weight of the segment tree, each time the segment tree of subtrees up combined.

Note To open long long.

code

#include<bits/stdc++.h>

typedef long long ll;
const int maxn=3e5+10;
typedef int iarr[maxn];

namespace IO
{
    char buf[1<<15],*fs,*ft;
    inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1, ch=getchar();
        while (!isdigit(ch) && ch^'-') ch=getchar();
        if (ch=='-') f=-1, ch=getchar();
        while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
        x*=f;
    }

    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::read;
using IO::write;

template<typename T>inline T min(T a,T b) { return a<b ? a : b; }
template<typename T>inline T max(T a,T b) { return a>b ? a : b; }

int ver[maxn<<1],Next[maxn<<1],head[maxn],len;
inline void add(int x,int y)
{
    ver[++len]=y,Next[len]=head[x],head[x]=len;
}

namespace SGT
{
    struct Orz{int l,r;ll z;}c[maxn*30];
    int num=0;
    inline void Change(int &x,int l,int r,int k,int z)
    {
        if (!x) x=++num;
        c[x].z+=z;
        if (l==r) return ;
        int mid=(l+r)>>1;
        if (k<=mid) Change(c[x].l,l,mid,k,z);
        else Change(c[x].r,mid+1,r,k,z);
    }

    inline ll query(int x,int l,int r,int tl,int tr)
    {
        if (!x || tr<tl) return 0;
        if (tl<=l && r<=tr) return c[x].z;
        int mid=(l+r)>>1; ll ans=0;
        if (tl<=mid) ans+=query(c[x].l,l,mid,tl,tr);
        if (tr>mid) ans+=query(c[x].r,mid+1,r,tl,tr);
        return ans;
    }

    inline int merge(int x,int y)
    {
        if (!x || !y) return x|y;
        int t=++num;
        c[t].l=merge(c[x].l,c[y].l);
        c[t].r=merge(c[x].r,c[y].r);
        c[t].z=c[x].z+c[y].z;
        return t;
    }
}

using SGT::Change;
using SGT::query;
using SGT::merge;

iarr rt,siz,dep;
int n,q;
inline void dfs(int x,int f)
{
    siz[x]=1;
    dep[x]=dep[f]+1;
    for (int i=head[x]; i; i=Next[i])
    {
        int y=ver[i];
        if (y==f) continue;
        dfs(y,x);
        siz[x]+=siz[y];
    }
    Change(rt[x],1,n,dep[x],siz[x]-1);
    if (f) rt[f]=merge(rt[f],rt[x]);
}

int main()
{
    read(n);read(q);
    for (int i=1,x,y; i<n; ++i) read(x),read(y),add(x,y),add(y,x);
    dfs(1,0);
    while (q--)
    {
        int x,y;read(x);read(y);
        write(query(rt[x],1,n,dep[x]+1,dep[x]+y)+1ll*(siz[x]-1)*min(dep[x]-1,y),'\n');
    }
    IO::flush();
    return 0;
}

Guess you like

Origin www.cnblogs.com/G-hsm/p/11420524.html