hdu5111 树链剖分,主席树
hdu5111
链接
思路
先考虑序列上如何解决。
1 3 2 5 4
1 2 4 5 3
这个序列变成
1 2 3 4 5
1 3 5 5 2
是对答案没有影响的(显然)。
然后查询操作\(l,r,L,R\)就是,
一段连续的区间\([L,R]\)内包含几个值在\([l,r]\)的数字个数.
主席树就可以做了。
\(query(rt[L-1],rt[R],[l,r]的和)\)
可以用树链剖分把树上问题转化成链上。
左边一棵树树链剖分,每一条链子都是一段连续的。
右边一棵树根据父子关系建立主席树。
然后向上跳统计贡献。
吐槽
本来我写完之后可以两遍编译,一遍样例,一遍AC的。
鬼知道我清空出了什么毛病。
for(i 0 to limit-1) a[i]=0
居然清空不了。
debug了半天,换成了memset才过。
我也是醉了。
代码
#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
#define ls(x) (t[x].ls)
#define rs(x) (t[x].rs)
using namespace std;
const int _=1e5+7;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n1,n2,w1[_],w2[_],rt[_];
map<int,int> dsr;
struct node {
int v,nxt;
}e1[_<<1],e2[_<<1];
int head1[_],head2[_],tot1,tot2;
void add1(int u,int v) {
e1[++tot1].v=v;
e1[tot1].nxt=head1[u];
head1[u]=tot1;
}
void add2(int u,int v) {
e2[++tot2].v=v;
e2[tot2].nxt=head2[u];
head2[u]=tot2;
}
namespace ZXS {
struct node {
int ls,rs,tot;
}t[_*30];
int cnt;
void clear() {
memset(t,0,sizeof(t));
// for(int i=0;i<=cnt;++i)
// ls(i)=rs(i)=t[i].tot=0;
cnt=0;
}
void insert(int l,int r,int k,int x,int &y) {
t[y=++cnt]=t[x];
t[y].tot++;
if(l==r) return;
int mid=(l+r)>>1;
if(k<=mid) insert(l,mid,k,ls(x),ls(y));
else insert(mid+1,r,k,rs(x),rs(y));
}
int query(int l,int r,int L,int R,int x,int y) {
if(L<=l&&r<=R) return t[y].tot-t[x].tot;
int mid=(l+r)>>1,ans=0;
if(L<=mid) ans+=query(l,mid,L,R,ls(x),ls(y));
if(R>mid) ans+=query(mid+1,r,L,R,rs(x),rs(y));
return ans;
}
}
namespace LCA{
int siz[_],son[_],top[_],f[_],dep[_],idx[_],cnt;
void clear() {
cnt=0;
memset(idx,0,sizeof(idx));
// memset(siz,0,sizeof(siz));
memset(son,0,sizeof(son));
// memset(top,0,sizeof(top));
// memset(dep,0,sizeof(dep));
// memset(f,0,sizeof(f));
}
void dfs1(int u,int fa) {
dep[u]=dep[fa]+1;
f[u]=fa;
siz[u]=1;
for(int i=head2[u];i;i=e2[i].nxt) {
int v=e2[i].v;
if(v==fa) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int topf) {
idx[u]=++cnt;
top[u]=topf;
if(!son[u]) return;
dfs2(son[u],topf);
for(int i=head2[u];i;i=e2[i].nxt) {
int v=e2[i].v;
if(!idx[v]) dfs2(v,v);
}
}
int query(int x,int y) {
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=f[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
return x;
}
}
namespace SLPF{
int siz[_],son[_],top[_],f[_],dep[_],idx[_],cnt;
void clear() {
cnt=0;
memset(idx,0,sizeof(idx));
// memset(siz,0,sizeof(siz));
memset(son,0,sizeof(son));
// memset(top,0,sizeof(top));
// memset(dep,0,sizeof(dep));
// memset(f,0,sizeof(f));
}
void dfs1(int u,int fa) {
dep[u]=dep[fa]+1;
f[u]=fa;
siz[u]=1;
for(int i=head1[u];i;i=e1[i].nxt) {
int v=e1[i].v;
if(v==fa) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int topf) {
idx[u]=++cnt;
dsr[w1[u]]=cnt;
top[u]=topf;
if(!son[u]) return;
dfs2(son[u],topf);
for(int i=head1[u];i;i=e1[i].nxt) {
int v=e1[i].v;
if(!idx[v]) dfs2(v,v);
}
}
void QQ(int x,int y,int u2,int v2) {
int lca=LCA::query(u2,v2),ans=0;
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=ZXS::query(1,n1,idx[top[x]],idx[x],rt[lca],rt[u2]);
// cout<<ZXS::query(1,n1,idx[top[x]],idx[x],rt[lca],rt[u2])<<"\n";
ans+=ZXS::query(1,n1,idx[top[x]],idx[x],rt[LCA::f[lca]],rt[v2]);
// cout<<ZXS::query(1,n1,idx[top[x]],idx[x],rt[LCA::f[lca]],rt[v2])<<"\n";
x=f[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=ZXS::query(1,n1,idx[x],idx[y],rt[lca],rt[u2]);
// cout<<ZXS::query(1,n1,idx[x],idx[y],rt[lca],rt[u2])<<"\n";
ans+=ZXS::query(1,n1,idx[x],idx[y],rt[LCA::f[lca]],rt[v2]);
// cout<<ZXS::query(1,n1,idx[x],idx[y],rt[LCA::f[lca]],rt[v2])<<"\n";
printf("%d\n",ans);
}
}
void dfs(int u,int fa) {
if(dsr.count(w2[u])) ZXS::insert(1,n1,dsr[w2[u]],rt[fa],rt[u]);
else rt[u]=rt[fa];
for(int i=head2[u];i;i=e2[i].nxt) {
int v=e2[i].v;
if(v==fa) continue;
dfs(v,u);
}
}
int main() {
// freopen("data.in","r",stdin);
// freopen("a.out","w",stdout);
while(scanf("%d",&n1)!=EOF) {
//clear
LCA::clear();
SLPF::clear();
ZXS::clear();
dsr.clear();
tot1=tot2=0;
memset(rt,0,sizeof(rt));
memset(head1,0,sizeof(head1));
memset(head2,0,sizeof(head2));
//read
for(int i=2;i<=n1;++i) {
int u=i,v=read();
// cout<<u<<"->"<<v<<"\n";
add1(u,v),add1(v,u);
}
for(int i=1;i<=n1;++i) w1[i]=read();
n2=read();
for(int i=2;i<=n2;++i) {
int u=i,v=read();
// cout<<u<<"->"<<v<<"\n";
add2(u,v),add2(v,u);
}
for(int i=1;i<=n2;++i) w2[i]=read();
//init
SLPF::dfs1(1,0);
SLPF::dfs2(1,1);
LCA::dfs1(1,0);
LCA::dfs2(1,1);
dfs(1,0);
//ask
int Q=read();
while(Q --> 0) {
int u1=read(),v1=read(),u2=read(),v2=read();
SLPF::QQ(u1,v1,u2,v2);
}
}
return 0;
}
hdu5111 树链剖分,主席树的更多相关文章
- dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448
4448: [Scoi2015]情报传递 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 588 Solved: 308[Submit][Status ...
- Codechef FIBTREE 树链剖分 主席树 LCA 二次剩余 快速幂
原文链接https://www.cnblogs.com/zhouzhendong/p/CC-FIBTREE.html 题目传送门 - CC-FIBTREE 题意 给定一个有 $n$ 个节点,初始点权都 ...
- BZOJ1146 [CTSC2008]网络管理Network 树链剖分 主席树 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1146 题意概括 在一棵树上,每一个点一个权值. 有两种操作: 1.单点修改 2.询问两点之间的树链 ...
- bzoj 4448 [Scoi2015]情报传递 (树链剖分+主席树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4448 题面: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络 ...
- BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树
4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...
- [GDOI2016][树链剖分+主席树]疯狂动物城
题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...
- HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树
题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...
- 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治
LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...
- BZOJ3531 SDOI2014 旅行 - 树链剖分,主席树
题意:给定一棵树,树上每个点有权值和类型.支持:修改某个点的类型:修改某个点的权值:询问某条链上某个类型的点的和/最大值.点数/类型数/询问数<=100000. 分析: 树链剖分,对每个类型的点 ...
- BZOJ4012 HNOI2015开店(树链剖分+主席树)
考虑这样一个问题:一棵树初始全是白点,有两种操作:把一个点染黑:询问某点到所有黑点的距离之和. 注意到树上两点x和y的距离为depth[x]+depth[y]-depth[lca(x,y)]*2.要求 ...
随机推荐
- Linux下用source insight的另一种方式--Samba
花了一些时间想找一个在Linux下的类似source insight的东东,网上有人推荐的source navigator,kscope之类,就那么几种颜色(也许没深入设置),也能叫语法高亮?至于其他 ...
- windows远程桌面无法拷贝文件的问题与解决方法
在开发完往windows服务器上部署系统或者给系统打补丁的时候,都会需要远程桌面的双向拷贝文件功能. 但是有些时候却会发现没有办法拷贝文件,原因主要有两个. 01 远程桌面的剪贴板设置 一个是在远程桌 ...
- python网络爬虫(1)——安装scrapy框架的常见问题及其解决方法
Scrapy是为了爬取网站数据而编写的一款应用框架,出名,强大.所谓的框架其实就是一个集成了相应的功能且具有很强通用性的项目模板. 其实在Linux和 Mac安装,就简单的pip命令即可: pip i ...
- axios解决跨域问题
最近把我自己的网站升级生成前后端分离的项目(vue+springBoot),不可避免的就遇到了跨域问题.从中学到了许多知识,随便分享出来,也巩固下所学. 谈到跨域,首先得了解CORS(Cross or ...
- 基于.NET平台常用的框架整理 转自 http://www.cnblogs.com/zhuyongblogs/p/5353751.html
常用的一些开源组件整理: 导出Excel报表的插件:NOPI.dll(基于微软OpenXml实现)开源的作业调度和自动任务框架:Quartz.NET用于大数据搜索引擎的全文检索框架:Lucene.ne ...
- axios安装及使用
使用npm安装 $ npm install axios 使用 bower安装 $ bower install axios 使用 cdn: <script src="https://un ...
- Android 常用炫酷控件(开源项目)git地址汇总
第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...
- 实现个虚拟机只要几百行的 toy 版就够了
关键是 指令 的 eval 并把 高层代码进行翻译. 典型的项目: 1. java-compiler (C++) 2. 手把手教你构建 C 语言编译器(0)- 前言 | 三点水 (C)
- ElasticSearch(九)e代驾使用Elasticsearch流程设计(Yii1版本)
一.控制器层的更新.添加.删除 class AddKnowledgeAction extends CAction { //add and update public function actionPo ...
- [转]Java游戏引擎
JME(jMonkey Engine): JME是一个高性能的3D图形API,采用LWJGL作为底层支持.它的后续版本将支持JOGL.JME和Java 3D具有类似的场景结构,开发者必须以树状方式组织 ...