主席树+dfs SPOJ BZOJ2588 Count on a tree
这道题我由于智障错误导致一直错。
在树上建主席树,加上lca思想,很简单。
#include<bits/stdc++.h>
using namespace std;
const int N=;
struct node
{
int to,nex;
}e[N<<];
struct tree
{
int l,r,s;
}t[];
int n,m,cnt,cn,mx,pre,head[N],f[N][],a[N],rt[N],d[N];
vector<int>v;
inline int get(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+;}
void add(int x,int y)
{
e[++cnt].to=y;e[cnt].nex=head[x];head[x]=cnt;
}
void update(int &x,int l,int r,int p,int num)
{
x=++cn;t[x]=t[p];t[x].s++;int mid=l+r>>;
if(l==r)return ;
if(mid>=num)update(t[x].l,l,mid,t[p].l,num);
else update(t[x].r,mid+,r,t[p].r,num);
}
void dfs(int x,int fa)
{
update(rt[x],,mx,rt[fa],get(a[x]));f[x][]=fa;d[x]=d[fa]+;
for(int i=;i<=;++i)
if(d[x]>=(<<i))
f[x][i]=f[f[x][i-]][i-];
for(int i=head[x];i;i=e[i].nex)
{
int y=e[i].to;
if(y==fa)continue;
dfs(y,x);
}
}
int lca(int x,int y)
{
if(d[x]<d[y])swap(x,y);
int tmp=d[x]-d[y];
for(int i=;i>=;i--)
if(tmp&(<<i))x=f[x][i];
for(int i=;i>=;i--)
{
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
}
return x==y?x:f[x][];
}
int query(int l,int r,int rt1,int rt2,int rt3,int rt4,int k)
{
if(l==r){
pre=v[l-];return l;
}
int sum=t[t[rt1].l].s+t[t[rt2].l].s-t[t[rt3].l].s-t[t[rt4].l].s;
int mid=(l+r)>>;
if(sum>=k)return query(l,mid,t[rt1].l,t[rt2].l,t[rt3].l,t[rt4].l,k);
else return query(mid+,r,t[rt1].r,t[rt2].r,t[rt3].r,t[rt4].r,k-sum);
}
int main()
{
//freopen("rand.out","r",stdin);
//freopen("my.out","w",stdout);
int x,y,k;
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
{
scanf("%d",&a[i]);v.push_back(a[i]);
}
sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());mx=v.size();
for(int i=;i<n;++i)
{
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs(,);
for(int i=;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&k);
int z=lca(pre^x,y);
printf("%d",v[query(,mx,rt[pre^x],rt[y],rt[z],rt[f[z][]],k)-]);
if(i<m) puts("");
}
return ;
}
BZOJ最后换行会PE。。。。
主席树+dfs SPOJ BZOJ2588 Count on a tree的更多相关文章
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- 洛谷P2633/bzoj2588 Count on a tree (主席树)
洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...
- BZOJ2588 Count on a tree 【树上主席树】
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7577 Solved: 185 ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
随机推荐
- css3图片响应式布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- SMB MS17-010 利用(CVE-2017-0144 )
exploit-db : https://www.exploit-db.com/exploits/42315/ 该漏洞的影响版本很广泛:Microsoft Windows Windows 7/8.1/ ...
- python3爬虫.1.简单的网页爬虫
此为记录下我自己的爬虫学习过程. 利用url包抓取网页 import urllib.request #url包 def main(): url = "http://www.douban.co ...
- perl6 struct2-045 EXP
测试站点: http://www.yutian.com.cn/index.action http://www.hjxzyzz.com:8088/pfw/login.action 代码如下: use v ...
- windows安装React Native开发运行环境
React Native是什么 React Native是facebook开源的一个用于开发app的框架.React Native的设计理念:既拥有Native (原生) 的用户体验.又保留React ...
- 最小的Django应用
创建一个hello.py 内容如下: import sys from django.conf import settings # 设置 settings.configure( DEBUG = Tr ...
- Ubuntu每次开机后提示:检测到系统程序出现问题的解决方法
首先,错误报告存放位置: cd /var/crash/ ls //可以查看错误报告 1 2 sudo rm /var/crash/* //删除该目录下的所有文件 1 但是,这只是删除掉的是错误报告,如 ...
- Tutorial 2: Requests and Responses
转载自:http://www.django-rest-framework.org/tutorial/2-requests-and-responses/ Tutorial 2: Requests and ...
- (转)粒子编辑器Particle designer属性的介绍
转载:http://blog.csdn.net/ym19860303/article/details/9210539 Particle designer粒子编辑器可到这里下载(包含授权码):http: ...
- 初探Java字符串
转载: 初探Java字符串 String印象 String是java中的无处不在的类,使用也很简单.初学java,就已经有字符串是不可变的盖棺定论,解释通常是:它是final的. 不过,String是 ...