[bzoj2588][Spoj10628]Count on a tree_主席树
Count on a tree bzoj-2588 Spoj-10628
题目大意:给定一棵n个点的树,m次查询。查询路径上k小值。
注释:$1\le n,m\le 10^5$。
想法:好像更博顺序有一些问题... ...
root[i]表示以i为根的主席树根
对于查询的两个点x,y,我们只需要查询
root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]]的主席树即可。
具体地,每个节点的权值线段树维护的是当前结点到根的路径上这些点权值的权值线段树。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int ans,tot,cnt,anc,n,m,q,x,y,z,d[100010],v[100010],h[100010],l[3000010],r[3000010],to[200010],head[100010],next[200010],sum[3000010],root[100010],f[100010][20];
map<int,int>b;
void add(int x,int y)
{
tot++;
next[tot]=head[x];
head[x]=tot;
to[tot]=y;
}
int lca(int x,int y)
{
if(d[x]<d[y])
{
swap(x,y);
}
int dep=d[x]-d[y];
for(int i=0;i<=19;i++)
{
if((dep&(1<<i))!=0)
{
x=f[x][i];
}
}
if(x==y)
{
return x;
}
for(int i=19;i>=0;i--)
{
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
}
return f[x][0];
}
int updata(int pre,int L,int R,int k)
{
int mid=(L+R)>>1;
int rt=++cnt;
l[rt]=l[pre];
r[rt]=r[pre];
sum[rt]=sum[pre]+1;
if(L==R)
{
return rt;
}
else
{
if(k<=mid)
{
l[rt]=updata(l[pre],L,mid,k);
}
else
{
r[rt]=updata(r[pre],mid+1,R,k);
}
}
return rt;
}
int query(int x,int y,int anc,int fa,int L,int R,int k)
{
int mid=(L+R)>>1;
if(L==R)
{
return b[L];
}
int num=sum[l[x]]+sum[l[y]]-sum[l[anc]]-sum[l[fa]];
if(num>=k)
{
return query(l[x],l[y],l[anc],l[fa],L,mid,k);
}
else
{
return query(r[x],r[y],r[anc],r[fa],mid+1,R,k-num);
}
}
void dfs(int x,int fa)
{
d[x]=d[fa]+1;
int k=lower_bound(h+1,h+1+m,v[x])-h;
b[k]=v[x];
root[x]=updata(root[fa],1,n,k);
for(int i=1;i<=19;i++)
{
f[x][i]=f[f[x][i-1]][i-1];
}
for(int i=head[x];i;i=next[i])
{
if(to[i]!=fa)
{
f[to[i]][0]=x;
dfs(to[i],x);
}
}
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&v[i]);
h[i]=v[i];
}
sort(h+1,h+1+n);
m=unique(h+1,h+1+n)-h-1;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(1,0);
for(int i=1;i<=q;i++)
{
scanf("%d%d%d",&x,&y,&z);
x=x^ans;
anc=lca(x,y);
ans=query(root[x],root[y],root[anc],root[f[anc][0]],1,n,z);
printf("%d\n",ans);
}
}
小结:在树上的主席树就是nb... ...
[bzoj2588][Spoj10628]Count on a tree_主席树的更多相关文章
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- 【bzoj2588】Count on a tree 主席树
这题给人开了个新思路. 原本构造一个序列的主席树,是这个位置用上个位置的信息来省空间,树上的主席树是继承父亲的信息来省空间. 此题若带修改怎么办? 若对某个点的权值做修改,则这个点的子树都会受影响,想 ...
- BZOJ2588 SPOJ10628 Count on a tree 【主席树】
BZOJ2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中l ...
- 洛谷P2633/bzoj2588 Count on a tree (主席树)
洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- [bzoj2588][count on a tree] (主席树+lca)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- BZOJ2588:Count on a tree(主席树)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- P2633 Count on a tree(主席树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
随机推荐
- Android Calendar的运用
import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; impo ...
- codeforces 916E Jamie and Tree dfs序列化+线段树+LCA
E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...
- aspectC++常用命令
常用命令:1.ag++ main.cc //在工程目录下产生编译后的exe2.ag++ main.cc --weave_only //产生.acc 纯c++文件3.ag++ main.cc --gen ...
- python 端口扫描程序
#! /usr/bin/env python3 #-*- coding:utf-8 -*- import socket import threading OPEN_COUNT = 0 lock = t ...
- Django:提交表单报错:RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have A
Django:提交表单报错:RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you ...
- ROS-URDF-活动关节
前言:介绍活动关节,并使机器人活动起来. 参考自:http://wiki.ros.org/urdf/Tutorials/Building%20a%20Movable%20Robot%20Model%2 ...
- Android Unresolved Dependencies
在Android Studio的开发中,在软件中集成了ButterKnife插件,另外需要集成ButterKnife的jar包.因为本地没有现成的,所以在module的build.gradle文件中添 ...
- windows和linux无法访问VMware中linux的tomcat主页问题
1.一定确定自己的tomcat服务器是启动的.(为了确保保险可以在测试前重新shutdown,startup一次) 2.确定自己访问的ip地址和端口号是正确的 如果是VMware外部windows的话 ...
- 安装完MongoDB后尝试mongod -dbpath命令为什么会一直卡在连接端口?
1.现象如下 Linux Windows 2.原因 其实,这不是卡住了,而是告诉我们.数据库已经启动,而且这个东东还不能关掉,关掉意味着数据库也关了.一开始我也是傻逼逼的在那等了一天,哎.... 3. ...
- response.getWriter().write()乱码问题
前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...