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. JDK1.8--API

    链接:https://pan.baidu.com/s/1mNlMIS1_8wFuQZ2vl2eTGg 提取码:e4yr

  2. 中文情感分析——snownlp类库 源码注释及使用

    最近发现了snownlp这个库,这个类库是专门针对中文文本进行文本挖掘的. 主要功能: 中文分词(Character-Based Generative Model) 词性标注(TnT 3-gram 隐 ...

  3. 利用SQL计算两个地理坐标(经纬度)之间的地表距离

    两个地理坐标(经纬度)地表距离计算公式: 公式解释如下: Long1,Lat1表示A点经纬度,Long2,Lat2表示B点经纬度: a=Lat1–Lat2 为两点纬度之差,b=Long1-Long2为 ...

  4. Redis学习之ziplist压缩列表源码分析

    一.压缩列表ziplist在redis中的应用 1.做列表键 当一个列表键只包含少量列表项,并且每个列表项要么是小整数,要么是短字符串,那么redis会使用压缩列表作为列表键的底层实现 2.哈希键 当 ...

  5. eclipse打开本地文件所在目录位置的快捷键

    在开发的过程中总是会遇到需要在本地文件夹找到该本地文件的情况,比如说要发送给同事什么的. 在使用Eclipse的过程中,大多数人都是先在Eclipse目录中定位到文件,然后通过在文件的右键属性中找到文 ...

  6. 使用SonarQube和SonarQube Scanner分析项目

    一.概述 SonarQube的安装,请参考链接:https://www.cnblogs.com/xiao987334176/p/12011623.html 配置好sonar的服务端后,接下来就要使用s ...

  7. Peewee之playhouse中的数据库连接池

    参见 http://note.youdao.com/noteshare?id=104255d91b5a00d716a713ae36e911fd 目前在学习Python库的源码,这是第二篇,比较简单的功 ...

  8. 【转载】C#的ArrayList使用IndexOf方法查找第一个符合条件的元素位置

    在C#的编程开发中,ArrayList集合是一个常用的非泛型类集合,在ArrayList集合中如果需要查找第一个符合条件的元素所在的位置,可以使用ArrayList集合的IndexOf方法,Index ...

  9. echarts 折线图百分比 tooltip 实例 两种方法

    方法一 在知道有几个类型时:下面有五个类型 tooltip : { show : true, trigger: 'axis', formatter: '{b0}<br/>{a0}: {c0 ...

  10. Python之路(第四十五篇)线程Event事件、 条件Condition、定时器Timer、线程queue

    一.事件Event Event(事件):事件处理的机制:全局定义了一个内置标志Flag,如果Flag值为 False,那么当程序执行 event.wait方法时就会阻塞,如果Flag值为True,那么 ...