题面

这道题应该比较裸吧。

\(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. Java Web学习笔记之---EL和JSTL

    Java Web学习笔记之---EL和JSTL (一)EL (1)EL作用 Expression  Language(表达式语言),目的是代替JSP页面中复杂的代码 (2)EL表达式 ${变量名} ( ...

  2. Oracle中start with...connect by/start with…connect by prior子句的用法

    connect by 是结构化查询中用到的,其基本语法是:select … from tablenamestart with 条件1connect by 条件2where 条件3;例:select * ...

  3. HDU4405 Aeroplane chess (概率DP,转移)

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落 ...

  4. C#如何获取系统downloads和documents路径

    https://stackoverflow.com/questions/7672774/how-do-i-determine-the-windows-download-folder-path 如果你通 ...

  5. ruby异常处理

    begin # 这段代码抛出的异常将被下面的 rescue 子句捕获 rescue # 这个块将捕获所有类型的异常 retry # 这将把控制移到 begin 的开头 end

  6. Microsoft Office Word

    快捷键 选区 选择块:[Shift]+click,光标放到块的一端,然后按住Shift,然后光标放到块的另一端. 更新域: F9 右键没有更新域选项时可以使用,如更新全部域先Ctrl + A然后F9 ...

  7. Windows 下手工搭建 LNMP 环境

    参考资料 如果想方便,可以直接使用集成的 LNMP 环境(例如 PHPStudy). 下载 PHP 下载地址:https://windows.php.net/download 根据你的系统选择 32 ...

  8. 16/8/23-jQuery完全图解scrollLeft,scrollWidth,clientWidth,offsetWidth 获取相对途径,滚动图片

    引用地址:http://www.cnblogs.com/mguo/archive/2013/03/19/2969657.html scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最 ...

  9. 学习HTML5 全局属性 accesskey-title

    属性classcontextmenu指定一个元素的上下文菜单.当用户右击该元素,出现上下文菜单dirdropzone指定是否将数据复制,移动,或链接,或删除idspellcheck检测元素是否拼写错误 ...

  10. hive环境

    一.hive安装部署 1.hive安装及配置 (1)解压apache-hive-1.2.1-bin.tar.gz到/opt/module/目录下面 tar -zxvf apache-hive-1.2. ...