[Luogu P4178]Tree (点分治+splay)
题面
传送门:https://www.luogu.org/problemnew/show/P4178
Solution
首先,长成这样的题目一定是淀粉质跑不掉了。
考虑到我们不知道K的大小,我们可以开一个splay来统计比某个数小的数的数量。
具体做法等我开淀粉质讲解的坑再满满填(咕)
Code
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
long long read()
{
long long x=0,f=1; char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int N=40000+100;
struct road
{
long long w;
int to;
road(int A,long long B)
{
to=A,w=B;
}
};
struct SBT
{
#define root son[0][1]
int fa[N],son[N][2],w[N],size[N],cnt[N],to;
inline void update(int now)
{
size[now]=size[son[now][0]]+size[son[now][1]]+cnt[now];
}
inline void rotate(int x,int type)
{
int y=fa[x],z=fa[y];
fa[x]=z,son[z][y==son[z][1]]=x;
son[y][!type]=son[x][type],fa[son[x][type]]=y;
son[x][type]=y,fa[y]=x;
update(y),update(x);
}
void splay(int now,int to)
{
while(fa[now]!=to)
{
if(now==son[fa[now]][fa[now]==son[fa[fa[now]]][1]] and fa[fa[now]]!=to)
rotate(fa[now],now==son[fa[now]][0]),
rotate(now,now==son[fa[now]][0]);
else
rotate(now,now==son[fa[now]][0]);
}
}
inline void InitTree()
{
root=to=0;
}
inline void init(int now)
{
fa[now]=son[now][0]=son[now][1]=size[now]=w[now]=0;
}
void Insert(int num)
{
if(root==0)
{
root=++to;
init(root);
fa[root]=0;
w[root]=num,cnt[root]=1,update(root);
return;
}
int now=root,last=root;
while(now!=0)
{
if(w[now]==num)
{
cnt[now]++;
splay(now,0);
return;
}
last=now,now=son[now][num>w[now]];
}
now=++to,init(now);
w[now]=num,cnt[now]=1;
fa[now]=last,son[last][num>w[last]]=now;
update(now),splay(now,0);
}
int Query(int num)
{
int now=root,t_ans=0;
while(now!=0)
{
if(num>=w[now])
{
if(w[now]>=w[t_ans]) t_ans=now;
now=son[now][1];
}
else
now=son[now][0];
}
if(t_ans==0) return 0;
splay(t_ans,0);
return size[son[root][0]]+cnt[root];
}
#undef root
}sbt;
vector <road> e[N];
long long n,K;
bool vis[N],t_vis[N],done[N];
int size[N],cnt,root;
int GetSize(int now)
{
t_vis[now]=true;
size[now]=1;
for(int i=0;i<int(e[now].size());i++)
if(t_vis[e[now][i].to]==false and vis[e[now][i].to]==false)
size[now]+=GetSize(e[now][i].to);
t_vis[now]=0;
return size[now];
}
void GetRoot(int now)
{
t_vis[now]=true,size[now]=1;
bool OK=true;
for(int i=0;i<int(e[now].size());i++)
if(t_vis[e[now][i].to]==false and vis[e[now][i].to]==false)
{
GetRoot(e[now][i].to);
size[now]+=size[e[now][i].to];
if(size[e[now][i].to]>cnt/2)
OK=false;
}
if(cnt-size[now]>cnt/2) OK=false;
if(OK==true) root=now;
t_vis[now]=0;
}
int ans;
void dfs2(int now,long long dis,int type)
{
t_vis[now]=true;
if(type==1 and K-dis>=0)
ans+=sbt.Query(K-dis);
else if(type==2)
sbt.Insert(dis);
for(int i=0;i<int(e[now].size());i++)
if(t_vis[e[now][i].to]==false and done[e[now][i].to]==true)
dfs2(e[now][i].to,dis+e[now][i].w,type);
t_vis[now]=false;
}
void dfs(int now)
{
//cerr<<now<<endl;
vis[now]=true;
vector <road> son;
for(int i=0;i<int(e[now].size());i++)
if(vis[e[now][i].to]==false)
{
cnt=GetSize(e[now][i].to);
GetRoot(e[now][i].to);
son.push_back(e[now][i]);
dfs(root);
}
sbt.InitTree();
sbt.Insert(0);
for(int i=0;i<int(son.size());i++)
dfs2(son[i].to,son[i].w,1),dfs2(son[i].to,son[i].w,2);
done[now]=true;
}
int main()
{
freopen("4178.in","r",stdin); n=read();
for(int i=1;i<=n;i++)
e[i].reserve(4);
for(int i=1;i<n;i++)
{
long long s=read(),t=read(),w=read();
e[s].push_back(road(t,w));
e[t].push_back(road(s,w));
}
K=read(); cnt=GetSize(1);
GetRoot(1);
dfs(root); printf("%d",ans);
return 0;
}
[Luogu P4178]Tree (点分治+splay)的更多相关文章
- luogu P4178 Tree
题目链接 luogu P4178 Tree 题解 点分治 代码 // luogu-judger-enable-o2 #include<cstdio> #include<algorit ...
- [Luogu P4178]Tree 题解(点分治+平衡树)
题目大意 给定一棵树,边带权,问有多少点对满足二者间距离$\leq K$,$n \leq 40000$. 题解 点分治专题首杀!$Jackpot!$ (本来看着题意比较简单想捡个软柿子捏,结果手断了… ...
- 洛谷P4178 Tree (点分治)
题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入输出格式 输入格式: N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下 ...
- 洛谷 P4178 Tree —— 点分治
题目:https://www.luogu.org/problemnew/show/P4178 这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美: 没有写 sor ...
- P4178 Tree 点分治
思路:点分治 提交:1次 题解: 要求权值和\(\leq K\) 的路径,我们可以类比点分治的模板,把长为\(len\)是否存在,改为\(len\)的路径的条数,并用用树状数组维护前缀和,这样就可以求 ...
- [洛谷P4178] Tree (点分治模板)
题目略了吧,就是一棵树上有多少个点对之间的距离 \(\leq k\) \(n \leq 40000\) 算法 首先有一个 \(O(n^2)\) 的做法,枚举每一个点为起点,\(dfs\) 一遍可知其它 ...
- POJ1471 Tree/洛谷P4178 Tree
Tree P4178 Tree 点分治板子. 点分治就是直接找树的重心进行暴力计算,每次树的深度不会超过子树深度的\(\frac{1}{2}\),计算完就消除影响,找下一个重心. 所以伪代码: voi ...
- 【题解】[P4178 Tree]
[题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...
- 【Luogu】P2596书架(Splay)
题目链接 通过这题我加深了对Splay的理解,原来Splay的子树也是可以接来接去接到别的点上的,而不是只能旋转qwq 具体接的办法就是swap大法. 对于Top操作我们把当前节点Splay到根,然后 ...
随机推荐
- Django-当前菜单激活状态-模版 request | slice
如何满足这个需求? 1. view中传递过来一个当前页面的参数标识,通过模版语言进行判断 {% if current_page == 'index' %}active{% endif %} # 每一个 ...
- 不知如何创建UML电路图?看看本文
Visual Paradigm是包含设计共享.线框图和数据库设计新特性的企业项目设计工具.现在你只需要这样单独的一款模型软件 Visual Paradigm就可以完成用UML设计软件,用BPMN去执行 ...
- Thymeleaf 异常:Exception processing template "index": An error happened during template parsing (template: "class path resource [templates/index.html]")
Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE ...
- Flink实例(五十): Operators(十)多流转换算子(五)coGroup 与union
参考链接:https://mp.weixin.qq.com/s/BOCFavYgvNPSXSRpBMQzBw 需求场景分析 需求场景 需求诱诱诱来了...数据产品妹妹想要统计单个短视频粒度的「点赞,播 ...
- 【Redis之疑难解析】(error) READONLY You can't write against a read only slave
一.问题描述 已部署好 Redis 主从服务器,实现了数据的同步. Redis 主服务器(master server)具有读写的权限,而 从服务器(slave master)默认 只具有 读 的权限. ...
- 数据结构与算法:AVL树
AVL树 在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树.增加和删除可能需要通过一次或多次树旋转来重新平衡这个树.AV ...
- 还不会ida*算法?看完这篇或许能理解点。
IDA* 算法分析 IDA* 本质上就是带有估价函数和迭代加深优化的dfs与,A * 相似A *的本质便是带 有估价函数的bfs,估价函数是什么呢?估价函数顾名思义,就是估计由目前状态达 到目标状态的 ...
- 适合刚刚学习编程的萌新:C语言编程学习制作超简单又好玩的报数游戏!
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- BASH提示符颜色、显示返回值,终端标题显示当前目录与正在执行的命令
BASH的PS1变量控制提示符相关的东西,善用它可以让BASH用起来舒服很多 提示符颜色 提示符显示上一个命令的返回值(exit code),并根据是否0调整颜色 提示符生成的时间(这样就知道上一条命 ...
- 利用Image对象,建立Javascript前台错误日志记录
手记:摘自Javascript高级程序设计(第三版),利用Image对象发送请求,确实有很多优点,有时候这也许就是一个创意点,再次做个笔记供自己和大家参考. 原文: 开发 Web 应用程序过程中的一种 ...