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.要求 ...
随机推荐
- C#:ref关键字和out关键字的区别
1.在不使用关键字(比如ref关键字.out关键字等)修饰函数的情况下,大部分函数的参数是以值传递的方式,也就是说,“调用函数”在使用参数(比如myNumber)时,是把该参数复制多一份,然后将其传递 ...
- SQL -------- WHERE子句与AND,OR和NOT运算符结合使用。
AND, OR and NOT 与 运算符中的且或非的意思相同 WHERE子句可以与AND,OR和NOT运算符结合使用. and 表示 查询的语句必须全部包含and 连接的两个或多个条件 or ...
- [JAVA] 日常填坑 java.lang.SecurityException: Prohibited package name: java.xxx
java虚拟机不允许包名以java开头. https://blog.csdn.net/sinat_28690417/article/details/72328547
- QT 随笔目录
[1]基础部分 <信号和槽机制> <信号与槽知识点> <QString 与 string转换> <QT 继承QWidget && 继承QDia ...
- 并行 Webclient(一)
在 Stackoverflow 上看到了一个提问,关于并行的 WebClient,觉得回答者的代码很有参考性,下面记录一下,以便日后用到: 提问者: 我有一个功能基本上分为两个子功能. html=Re ...
- 人生苦短,我用Python(目录)
目前Django分类的文章正在整顿中,届时将呈现更加丰富的内容,另外整体排版也将会更改,感谢支持!! 一.Python基础 二.并发编程 三.数据库 四.Web 五.Python Web 框架 六.爬 ...
- Prometheus监控学习笔记之prometheus 版本1.7 常用启动参数
日志类: -log.level 可选值 [debug, info, warn, error, fatal] 例:-log.level "info" -log.format 可选 ...
- 用友U9 查看功能页面实体
对着当前页面右键查看属性: 在链接后面加上&__dm=true 在打开的页面将鼠标放到字段上,可以看到页面实体是那一个.
- python 库 PrettyTabble 使用与错误
参考链接:http://zetcode.com/python/prettytable/ PrettyTable能在python中生成ASCII 表,可以使用他控制表的很多方面,包括文本对齐.表的边框. ...
- 【selenium】基于python语言,如何用select选择下拉框
在项目测试中遇到了下拉框选择的控件,来总结下如何使用select选择下拉框: 下图是Select类的初始化描述,意思是,给定元素是得是select类型,不是就抛异常.接下来给了例子:要操作这个sele ...