题面

这道题应该比较裸吧。

\(a\),\(b\)都是\(c\)的祖先。

那么第一种情况是\(b\)是\(a\)的祖先,那么方案数就是\(\min\{dep[a]-1,k\}\cdot (num[a]-1)\)。

第二种是\(a\)是\(b\)的祖先,那么方案数是

\[\sum_{c\in subtree(a),dep[c]-dep[a]\leq k} num[c]-1
\]

显然这东西用主席树,一个维度是dfs序,一个维度数深度维护一下就好了吧。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define REP(i,a,n) for(register int i(a);i<=(n);++i)
#define FEC(i,x,y) for(register int i=head[x],y=g[i].to;i;i=g[i].ne,y=g[i].to)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
const int SZ=(1<<21)+1;char ibuf[SZ],*iS,*iT,obuf[SZ+128],*oS=obuf,*oT=obuf+SZ-1;
#ifdef ONLINE_JUDGE
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SZ,stdin),(iS==iT?EOF:*iS++)):*iS++)
#else
#define gc() getchar()
#endif
template<typename I>inline void read(I&x){char c=gc();int f=0;for(;c<'0'||c>'9';c=gc())c=='-'?f=1:0;for(x=0;c>='0'&&c<='9';c=gc())x=(x<<1)+(x<<3)+(c&15);f?x=-x:0;}
inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
#define printf(...) (oS>oT&&(flush(),1),oS+=sprintf(oS,__VA_ARGS__))
template<typename A,typename B>inline char SMAX(A&a,const B&b){return a<b?a=b,1:0;}
template<typename A,typename B>inline char SMIN(A&a,const B&b){return a>b?a=b,1:0;}
typedef long long ll;typedef unsigned long long ull;typedef std::pair<int,int>pii; const int N=300000+7;
int n,Q,x,y,dfn[N],pre[N],dfc,dep[N],num[N],a[N],T;
struct Edge{int to,ne;}g[N<<1];int head[N],tot;
inline void Addedge(int x,int y){g[++tot].to=y;g[tot].ne=head[x];head[x]=tot;} inline void DFS(int x,int fa=0){
dfn[x]=++dfc;pre[dfc]=x;dep[x]=dep[fa]+1;num[x]=1;
FEC(i,x,y)if(y!=fa)DFS(y,x),num[x]+=num[y];
} struct Node{int lc,rc,id;ll val;}t[N*21];int RT[N],nod;
inline void Insert(int&o,int L,int R,int x,int k){
if(t[o].id!=T)t[++nod]=t[o],t[o=nod].id=T;t[o].val+=k;if(L==R)return;
int M=(L+R)>>1;x<=M?Insert(t[o].lc,L,M,x,k):Insert(t[o].rc,M+1,R,x,k);
}
inline ll Query(int o,int p,int L,int R,int l,int r){
if(l<=L&&R<=r)return t[o].val-t[p].val;
int M=(L+R)>>1;if(r<=M)return Query(t[o].lc,t[p].lc,L,M,l,r);if(l>M)return Query(t[o].rc,t[p].rc,M+1,R,l,r);
return Query(t[o].lc,t[p].lc,L,M,l,r)+Query(t[o].rc,t[p].rc,M+1,R,l,r);
} inline char cmp(const int&x,const int&y){return dep[x]<dep[y];}
int main(){
read(n);read(Q);REP(i,1,n-1)read(x),read(y),Addedge(x,y),Addedge(y,x);
DFS(1);REP(i,1,n)a[i]=i;std::sort(a+1,a+n+1,cmp);int p=1;
REP(i,1,n){
++T;RT[i]=RT[i-1];
while(p<=n&&dep[a[p]]==i)Insert(RT[i],1,n,dfn[a[p]],num[a[p]]-1),++p;//错误笔记:这里的i,p要分清楚不能弄混掉了
}
REP(i,1,Q){
read(x),read(y);//错误笔记:同第49行,i,x要分清楚
printf("%lld\n",std::min(dep[x]-1,y)*(ll)(num[x]-1)+Query(RT[std::min(dep[x]+y,n)],RT[dep[x]],1,n,dfn[x],dfn[x]+num[x]-1));//错误笔记:要把dep[x]+y和n取个min,不然的话还没更新过
}return flush(),0;
}

[BZOJ3653]谈笑风生 主席树的更多相关文章

  1. bzoj 3653 谈笑风生——主席树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3653 原来一直想怎么线段树合并.可是不会把角标挪一位. 查询的其实是子树内一段深度的点的 s ...

  2. bzoj 3653 谈笑风生 —— 主席树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3653 对于一个 (a,b,c),分成 b 是 a 的祖先和 b 在 a 子树里两部分: 第一 ...

  3. BZOJ 3653: 谈笑风生(主席树)

    传送门 解题思路 首先对于一个\(a\)来说,要求\(b\)和\(c\),那么\(a,b,c\)一定在一条链上.把\(b\)分类讨论,如果\(b\)是\(a\)的祖宗,这个方案数就很好统计了,就是\( ...

  4. P3899 [湖南集训]谈笑风生 主席树

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  5. [Luogu P3899] [湖南集训]谈笑风生 (主席树)

    题面 传送门:https://www.luogu.org/problemnew/show/P3899 Solution 你们搞的这道题啊,excited! 这题真的很有意思. 首先,我们可以先理解一下 ...

  6. 【NOI模拟】谈笑风生(主席树)

    题目描述 设 T 为一棵有根树,我们做如下的定义: 设 a 和 b 为 T 中的两个不同节点.如果 a 是 b 的祖先,那么称 “ a 比 b 不知道高明到哪里去了 ” . 设 a 和 b 为 T 中 ...

  7. 数据结构(主席树):COGS 2211. 谈笑风生

    2211. 谈笑风生 ★★★★   输入文件:laugh.in   输出文件:laugh.out   简单对比时间限制:3 s   内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...

  8. 主席树 || 可持久化线段树 || BZOJ 3653: 谈笑风生 || Luogu P3899 [湖南集训]谈笑风生

    题面:P3899 [湖南集训]谈笑风生 题解: 我很喜欢这道题. 因为A是给定的,所以实质是求二元组的个数.我们以A(即给定的P)作为基点寻找答案,那么情况分两类.一种是B为A的父亲,另一种是A为B的 ...

  9. BZOJ3653谈笑风生——可持久化线段树+dfs序

    题目描述 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ? 设a 和 b 为 T 中的两个不同节点.如果 a ...

随机推荐

  1. 【Swagger2】SpringBoot整合swagger2

    Swagger 简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法, ...

  2. Django 基础笔记补充

    1.目录文件 django-admin.py startproject mydj cd mydj python manage.py startapp myapp   后生成目录: mydj/ ├── ...

  3. vue定义组件

    1.定义组件 2.在App.vue里引入Home组件 home代码 <template> <div>home</div> </template> < ...

  4. 前端每日实战:2# 视频演示如何用纯 CSS 创作一个矩形旋转 loader 特效

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/vjLQMM 可交互视频教程 此视频是可以交 ...

  5. Linux shell 归纳之 last

    使用方法: 格式1:last -n n指定账号数量,最新前n个登陆信息(用户,终端,IP, 日期,时间和持续时间) 格式2:last -n <number of the latest accou ...

  6. codeforces 617 E. XOR and Favorite Number(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...

  7. php 中 http_build_query用法

    http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( arra ...

  8. git 时 出现 Permission denied (publickey).

    https://blog.csdn.net/awp0011/article/details/73368481 第一次使用github.com在本地 执行 git clone git@github.co ...

  9. 测开之路七十四:python处理kafka

    kafka-python地址:https://github.com/dpkp/kafka-python 安装kafka-python:pip install kafka-python 接收消息 fro ...

  10. mysql 8.X.X版本多个ip限制访问

    随笔记录,由于客户要求数据库不同ip访问,查了很多,多数都是ip段或者所有ip可以访问: select user,host from user;可以查看某些用户可以访问的ip:但只能设置一个用户一条记 ...