【BZOJ 3653】 谈笑风生|主席树
来源:互联网 发布:软件测试工程师要求 编辑:程序博客网 时间:2024/06/04 18:02
好神的思路
按dfs序建树 深度作为范围
详见黄学长等
wa了一次
1.没有 long long 输出
2.询问的时候没有加root[]调试很长时间
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define LL long longconst int MAXN=300000+10;int cnt,s[MAXN],t[MAXN],size[MAXN],depth[MAXN],fa[MAXN],fx[MAXN];int top,root[MAXN],max_depth,to[MAXN*20][2];LL Num[MAXN*20];int tot,g[MAXN],nnext[MAXN*2],num[MAXN*2];void add(int x,int y){tot++;nnext[tot]=g[x];g[x]=tot;num[tot]=y;}void dfs(int x){depth[x]=depth[fa[x]]+1;max_depth=max(max_depth,depth[x]);s[x]=++cnt;fx[cnt]=x;size[x]=1;for(int i=g[x];i;i=nnext[i])if(num[i]!=fa[x]){fa[num[i]]=x;dfs(num[i]);size[x]+=size[num[i]];}t[x]=cnt;}int n,q;void add(int last,int now,int l,int r,int x,int y){Num[now]=Num[last]+y;if(l==r) return ;int mid=(l+r)/2;if(x<=mid) to[now][1]=to[last][1],to[now][0]=++top,add(to[last][0],to[now][0],l,mid,x,y);else to[now][0]=to[last][0],to[now][1]=++top,add(to[last][1],to[now][1],mid+1,r,x,y);}LL qq(int last,int now,int l,int r,int ss,int tt){//cout<<l<<" "<<r<<endl;if(ss<=l&&r<=tt) return Num[now]-Num[last];int mid=(l+r)/2;LL ans=0;if(ss<=mid) ans+=qq(to[last][0],to[now][0],l,mid,ss,tt);if(mid+1<=tt) ans+=qq(to[last][1],to[now][1],mid+1,r,ss,tt);return ans; }int main(){cin>>n>>q;for(int i=1;i<n;i++){int x,y;scanf("%d %d",&x,&y);add(x,y);add(y,x);}dfs(1);for(int i=1;i<=n;i++) root[i]=++top,add(root[i-1],root[i],1,max_depth,depth[fx[i]],size[fx[i]]-1);//for(int i=1;i<=n;i++) cout<<Num[root[i]]<<' ';//cout<<endl;//cout<<'*'<<qq(root[3],root[5],1,max_depth,1,4);for(int i=1;i<=q;i++){LL ans=0;int x,y;scanf("%d %d",&x,&y);ans+=((LL)min(y,depth[x]-1)*(size[x]-1));//cout<<s[x]<<' '<<t[x]<<endl;ans+=qq(root[s[x]],root[t[x]],1,max_depth,depth[x]+1,depth[x]+y);printf("%lld\n",ans);}return 0;}
0 0
- 【BZOJ 3653】 谈笑风生|主席树
- bzoj 3653: 谈笑风生 (主席树+dfs序)
- [bzoj3653]谈笑风生 主席树
- 【BZOJ3653】谈笑风生【主席树】【DFS序】
- 【bzoj3653】【谈笑风生】【dfs序+主席树】
- [BZOJ3653]谈笑风生(dfs序+主席树)
- 主席树维护dfs序——BZOJ3653/Luogu3899 谈笑风生
- 3653: 谈笑风生
- bzoj 3932(主席树)
- BZOJ 2588 主席树
- 【BZOJ 2809】dispatching(主席树)
- 【主席树】 BZOJ 2653 middle
- 【主席树】BZOJ 2653 middle
- BZOJ 2653: middle|主席树
- bzoj 3524/2223(主席树)
- bzoj 1803(主席树)
- bzoj 2588(树链剖分+主席树)
- bzoj 2653 二分+主席树
- oracle数据库安装2
- LeetCode 13. Roman to Integer
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- Bzoj3676:[Apio2014]回文串:回文自动机
- LeetCode 198. House Robber
- 【BZOJ 3653】 谈笑风生|主席树
- 读书笔记-java基础-知识点
- 抽象类和接口的区别
- Pydev环境搭建ZendStudio 13+python
- [bzoj4527]K-D-Sequence 解题报告
- [转]性能调优攻略(陈浩)
- 关系型数据库工作原理-时间复杂度(翻译自Coding-Geek文章)
- qq设计之 里氏替换原则
- CSS_腾讯QQ官网,新浪官网,网易官网,淘宝官网之css初始化代码