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.要求 ...
随机推荐
- jquery库与其他库(比如prototype)冲突的解决方法
前端开发很容易会遇到jQuery库与其他库冲突的场景,比如和prototype库冲突. 实际上这种冲突是因为不同的第三方JS库争夺对$标识符的控制权引起的. 解决方法,就是使用jQuery提供的jQu ...
- JS提交表单页面不跳转、JS下载、动态创建from
JS下载 function downloadFile(id) { var url = "<%=request.getContextPath()%>/cer/downlo ...
- WPF MultiBinding,多值转化器IMultiValueConverter,自制调色板 palette
public class RGBConverter : IMultiValueConverter { //正向修改,整合颜色值 public object Convert(object[] value ...
- WPF布局介绍(1)
开局一张图,内容全靠...,本系列的文章, 主要针对刚入门.亦或是从 winform/bs转过来的开发人员快速入门的指南, 相对于其它一些文章中会详细的从项目如何建立到其实现的原理及组成部分, 本系列 ...
- centOs6和Centos7开放/关闭端口区别
#centos6启动防火墙 service iptables start #centos6停止防火墙/关闭防火墙 service iptables stop #centos6重启防火墙 servic ...
- 跟随腾讯WeTest一起来2019Unreal Open Day!
WeTest 导读 Unreal Open Day 是由 Epic Games 中国一年一度倾力打造的面向虚幻引擎开发者的技术分享活动,是引擎行业规格最高.规模最大.阵容最强的年度盛会之一. 自从 ...
- vue单元素/组件的过渡
(1)过渡的类名 v-enter:定义进入过渡的开始状态.在元素被插入之前生效,在元素被插入之后的下一帧移除. v-enter-active:定义进入过渡生效时的状态.在整个进入过渡的阶段中应用,在元 ...
- ES6 入门系列 (一)ES6的前世今生
要学好javascript , ECMAScript标准比什么都强, ESMAScript标准已经用最严谨的语言和最完美的角度展现了语言的实质和特性. 理解语言的本质后,你已经从沙堆里挑出了珍珠,能经 ...
- 实验吧——加了料的报错注入(exp报错注入)
题目地址:http://ctf5.shiyanbar.com/web/baocuo/index.php 先查看页面源码得到提示知道了后台执行的sql语句,很常规的查询 测试了一个报错函数发现如下回显, ...
- Android 培训准备资料之project与module的区别(1)
project和module的区别? 现在我们来看看在Android studio中怎样新建一个project (1)file->new->new project. Application ...