BZOJ 3653 主席树
思路:
(抄一波公式)
$$ans=min(dep[x],k)×(size[x]-1)+\sum_{y在x的子树中,且dis(x,y)<=k}(size[y]-1)$$
顺着DFS序
按照deep往线段树里插就好了...
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=;
int n,q,xx,yy,first[N],next[N*],v[N*],tot,root[N];
int dfn[N],revdfn[N],last[N],cnt,deep[N],size[N],lson[N*],rson[N*];
long long tree[N*];
void add(int x,int y){v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void dfs(int x,int fa){
dfn[x]=++cnt,revdfn[cnt]=x,size[x]=;
for(int i=first[x];~i;i=next[i])if(v[i]!=fa){
deep[v[i]]=deep[x]+,dfs(v[i],x),size[x]+=size[v[i]];
}last[x]=cnt;
}
void insert(int l,int r,int &pos,int last,int num,int wei){
pos=++cnt,tree[pos]=tree[last]+wei;
if(l==r)return;
int mid=(l+r)>>;
if(mid<num)lson[pos]=lson[last],insert(mid+,r,rson[pos],rson[last],num,wei);
else rson[pos]=rson[last],insert(l,mid,lson[pos],lson[last],num,wei);
}
long long query(int l,int r,int pos,int last,int L,int R){
if(l>=L&&r<=R)return tree[pos]-tree[last];
int mid=(l+r)>>;
if(mid<L)return query(mid+,r,rson[pos],rson[last],L,R);
else if(mid>=R)return query(l,mid,lson[pos],lson[last],L,R);
else return query(l,mid,lson[pos],lson[last],L,R)+query(mid+,r,rson[pos],rson[last],L,R);
}
int main(){
memset(first,-,sizeof(first));
scanf("%d%d",&n,&q);
for(int i=;i<n;i++)scanf("%d%d",&xx,&yy),add(xx,yy),add(yy,xx);
deep[]=,dfs(,);
for(int i=;i<=n;i++)insert(,n,root[i],root[i-],deep[revdfn[i]],size[revdfn[i]]-);
while(q--){
scanf("%d%d",&xx,&yy);
printf("%lld\n",query(,n,root[last[xx]],root[dfn[xx]],deep[xx],deep[xx]+yy)+min(deep[xx]-,yy)*(1ll*size[xx]-));
}
}
BZOJ 3653 主席树的更多相关文章
- bzoj 1901 主席树+树状数组
修改+查询第k小值 单纯主席树修改会打乱所有,所以再套一个树状数组维护前缀和使得修改,查询都是log 对了,bzoj上不需要读入组数,蜜汁re.. #include<cstdio> #in ...
- BZOJ 2588 主席树
思路: 主席树 做完BZOJ 3123 觉得这是道水啊-- 然后狂RE 狂MLE 要来数据 忘把deep[1]设成1了----------. 啊wocccccccccccccccc //By Siri ...
- bzoj 1818 主席树
思路:主席树搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #de ...
- BZOJ 4771 主席树+倍增+set
思路: 因为有深度的限制,并且我们是在线段树上维护权值,所以我们把点按照dep排序,然后一个一个修改...主席树的下标就是dfs序,子树的查询就是区间查询... 但是发现这样怎么去维护LCA呢...因 ...
- BZOJ 3674/BZOJ 3673 主席树
思路: 主席树维护可持久化数组 剩下的就是普通的并查集了- //By SiriusRen #include <cstdio> #include <cstring> #inclu ...
- BZOJ 3123 主席树 启发式合并
思路: 主席树 搞树上的k大 x+y-lca(x,y)-fa(lca(x,y)) 按照size小树往大树上插 启发式合并 n*log^2n的 搞定~ //By SiriusRen #include & ...
- BZOJ 4448 主席树+树链剖分(在线)
为什么题解都是离线的-- (抄都没法抄) 搞一棵主席树 1 操作 新树上的当前节点设成1 2 操作 查max(i-xx-1,0)那棵树上这条路径上有多少个点是1 让你找经过了多少个点 查的时候用dee ...
- BZOJ 3524主席树裸题 (雾)
思路: 按权值建一棵主席树 (但是这好像不是正解 空间复杂度是不对的--.) //By SiriusRen #include <cstdio> #include <cstring&g ...
- BZOJ 3524 - 主席树
传送门 题目分析 标准主席树,按照位置插入每个数,对于询问l, r, 在l-1,r两树上按照线段树搜索次数大于(r - l + 1) / 2的数. code #include<bits/stdc ...
随机推荐
- CAD直接打印,不出现打印对话框(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_Print 直接打印,不出现打印对话框,详细说明如下: 参数 说明 double ptLBx 打印的范围左下角x double ...
- 一个电商项目的Web服务化改造3:改进方案の规范和约定、单表、单一职责
最近一直在做一个电商项目,需要把原有单系统架构的项目,改造成基于服务的架构,SOA. 有点挑战,做完了,会有很大进步. 上一篇,我们描述了原有项目中的问题. 或者说是,本篇的基本 ...
- vue组件 $children,$refs,$parent的使用详解
1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"></firstchild> 然后在父组件中,通过this.$ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 请问spfa+stack 和spfa+queue 是什么原理
一个是bfs加迭代 一个是dfs加迭代 请问迭代是什么 就是不断地做,做到没有更优的解为止 或者是不断得做,做到逼近答案为止.. 栈比队列更快更节省空间
- CodeForces - 205B - Little Elephant and Sorting
先上题目: Little Elephant and Sorting time limit per test 1 second memory limit per test 256 megabytes i ...
- spring boot 中访问 REST 接口
RestTemplate restTemplate = new RestTemplate(); Object result = restTemplate.getForObject("http ...
- Spring MVC的@RequestMapping多个URL映射到同一个方法
@RequestMapping可以是一个URL对应一个方法,也可以多个URL对应同一个方法,写法如下: @RequestMapping(value={"url","res ...
- Oracle数据库点滴
分页查询: SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM <= 40)W ...
- lsyncd + rsync 实时同步搭建
一.inotify和lsync inotify和lsyncd对比一下,发现虽然lsyncd没有inotify那么真正的实时同步,但是lsyncd的同步基本上可以满足基本实时同步的要求,而且lsyncd ...