hdu5111

链接

hdu

思路

先考虑序列上如何解决。

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 树链剖分,主席树的更多相关文章

  1. dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448

    4448: [Scoi2015]情报传递 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 588  Solved: 308[Submit][Status ...

  2. Codechef FIBTREE 树链剖分 主席树 LCA 二次剩余 快速幂

    原文链接https://www.cnblogs.com/zhouzhendong/p/CC-FIBTREE.html 题目传送门 - CC-FIBTREE 题意 给定一个有 $n$ 个节点,初始点权都 ...

  3. BZOJ1146 [CTSC2008]网络管理Network 树链剖分 主席树 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1146 题意概括 在一棵树上,每一个点一个权值. 有两种操作: 1.单点修改 2.询问两点之间的树链 ...

  4. bzoj 4448 [Scoi2015]情报传递 (树链剖分+主席树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4448 题面: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络 ...

  5. BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树

    4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...

  6. [GDOI2016][树链剖分+主席树]疯狂动物城

    题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...

  7. HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树

    题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...

  8. 5.15 牛客挑战赛40 E 小V和gcd树 树链剖分 主席树 树状数组 根号分治

    LINK:小V和gcd树 时限是8s 所以当时好多nq的暴力都能跑过. 考虑每次询问暴力 跳父亲 这样是nq的 4e8左右 随便过. 不过每次跳到某个点的时候需要得到边权 如果直接暴力gcd的话 nq ...

  9. BZOJ3531 SDOI2014 旅行 - 树链剖分,主席树

    题意:给定一棵树,树上每个点有权值和类型.支持:修改某个点的类型:修改某个点的权值:询问某条链上某个类型的点的和/最大值.点数/类型数/询问数<=100000. 分析: 树链剖分,对每个类型的点 ...

  10. BZOJ4012 HNOI2015开店(树链剖分+主席树)

    考虑这样一个问题:一棵树初始全是白点,有两种操作:把一个点染黑:询问某点到所有黑点的距离之和. 注意到树上两点x和y的距离为depth[x]+depth[y]-depth[lca(x,y)]*2.要求 ...

随机推荐

  1. Hbase put写入源码分析

    今天有空闲时间看一下HBASE的写入代码 MutiAction类,是一个action的container,包括get . put. delete.并且是根据region name分组的.其中核心的就是 ...

  2. torch.max

    torch.max() torch.max(input) -> Tensor Explation: ​ Returns the maximum value of all elements in ...

  3. 在ASP.NET Core中添加的Cookie如果含有特殊字符,会被自动转义

    我们知道在Cookie中有些字符是特殊字符,这些字符是不能出现在Cookie的键值中的. 比如"="是Cookie中用来分隔键和值的特殊字符,例如:Key01=Value01,表示 ...

  4. 排序算法Java代码实现(六)—— 堆排序

    本片内容: 堆排序 堆排序 最大堆: 二叉堆是完全二叉树或者是近似完全二叉树, 当父结点的键值总是大于或等于任何一个子节点的键值时为最大堆.(父节点大于任何一个子节点) 算法思想: 把n个元素建立最大 ...

  5. Dos.ORM修改数据遇到的问题

    2019年11月6日,今天使用Dos.ORM进行数据的批量修改,出现修改一条数据造成所有数据相应状态改变的情况,代码如下: 按照一步步调试的方式,排查出原因:生成的orm实体类缺少 主键 的标识,该原 ...

  6. 【翻译】Tusdotnet中文文档(2)事件

    tusdotnet-----一个tus文件上传协议的实现之事件 本章接上篇来继续翻译Tusdotnet的文档,按照如下结构来翻译: 事件 OnAuthorize OnFileComplete OnBe ...

  7. Delphi - TIdFTP 两个重要函数

    TIdFTP 两个重要函数 项目开发过程中发现,直接对于服务器上的文件/路径进行处理,是很危险的事情,因为一旦文件/路径不存在,程序就会抛异常,影响客户体验.所以在对服务器上的文件/路径进行访问之前, ...

  8. 对Haskell这门语言的基本认识

    Haskell语言的核心特征: 1. 函数式,而且是纯函数式(purely functional) 首先,引用一下维基百科上对“典型的函数式编程语言”的划分: 一: 纯函数式 1. 强静态类型: Mi ...

  9. C#中 char、byte、string

    var str = "我是中国人";var str1 = "abc"; char[] chars = str.ToCharArray();char[] char ...

  10. C# abstract class Interface的介绍

    1.基本概念介绍 抽象类: 1.抽象方法只作声明,而不包含实现,可以看成是没有实现体的虚方法 2.抽象类可以但不是必须有抽象属性和抽象方法,但是一旦有了抽象方法,就一定要把这个类声明为抽象类 3.具体 ...