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个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。
Input
第一行两个整数N,M。
第二行有N个整数,其中第i个整数表示点i的权值。
后面N-1行每行两个整数(x,y),表示点x到点y有一条边。
最后M行每行两个整数(u,v,k),表示一组询问。
Output
M行,表示每个询问的答案。最后一个询问不输出换行符
Sample Input
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output
2
8
9
105
7
HINT:
N,M<=100000
暴力自重。。。
Source
鸣谢seter
/*
树上第K大.
对于每一个节点
以key为下标
用一颗权值线段树维护它到根的路径的区间K大值.
然后用主席树维护.
树剖求lca.
ans=tree[x].size+tree[y].size-tree[lca].size-tree[lca_father].size.
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAXN 100001
using namespace std;
int n,m,ans,cut,maxsize,root[MAXN],tot,s[MAXN],a[MAXN],head[MAXN];
int fa[MAXN],pos[MAXN],top[MAXN],deep[MAXN],size[MAXN];
struct data{int lc,rc,size;}tree[MAXN*20];
struct node{int v,next;}e[MAXN*2];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int u,int v)
{
e[++cut].v=v;e[cut].next=head[u];head[u]=cut;
}
void add(int &k,int last,int l,int r,int x)
{
k=++tot;tree[k].lc=tree[last].lc,tree[k].rc=tree[last].rc;
tree[k].size=tree[last].size+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(x<=mid) add(tree[k].lc,tree[last].lc,l,mid,x);
else add(tree[k].rc,tree[last].rc,mid+1,r,x);
return ;
}
void dfs1(int u,int f)
{
size[u]=1;
int p=lower_bound(s+1,s+n+1,a[u])-s;
add(root[u],root[f],1,n,p);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(v!=f) fa[v]=u,deep[v]=deep[u]+1,dfs1(v,u),size[u]+=size[v];
}
return ;
}
void dfs2(int u,int top1)
{
int k=0;pos[u]=++maxsize;top[u]=top1;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(fa[v]==u&&size[v]>size[k]) k=v;
}
if(!k) return ;
dfs2(k,top1);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(fa[v]==u&&v!=k) dfs2(v,v);
}
return ;
}
int lca(int x,int y)
{
fa[1]=1;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);//1 R.
x=fa[top[x]];
}
fa[1]=0;
if(pos[x]>pos[y]) swap(x,y);
return x;
}
int query(int L,int R,int lc,int falc,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)>>1;
int sum=tree[tree[L].lc].size+tree[tree[R].lc].size-
tree[tree[lc].lc].size-tree[tree[falc].lc].size;
if(sum>=k)
return query(tree[L].lc,tree[R].lc,tree[lc].lc,tree[falc].lc,l,mid,k);
else return query(tree[L].rc,tree[R].rc,tree[lc].rc,tree[falc].rc,mid+1,r,k-sum);
}
void slovequery(int x,int y,int k)
{
int lc=lca(x,y);
ans=query(root[x],root[y],root[lc],root[fa[lc]],1,n,k);
printf("%d",ans=s[ans]);return ;
}
int main()
{
int x,y,z;
n=read(),m=read();
for(int i=1;i<=n;i++) s[i]=a[i]=read();
for(int i=1;i<=n-1;i++) x=read(),y=read(),add(x,y),add(y,x);
sort(s+1,s+n+1);dfs1(1,0),dfs2(1,1),fa[1]=0;
while(m!=1)
{
x=read(),y=read(),z=read();x^=ans;
slovequery(x,y,z);
m--;printf("\n");//2 W.
}
x=read(),y=read(),z=read();x^=ans;
slovequery(x,y,z);
return 0;
}
Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)的更多相关文章
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 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 ...
- 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 (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- (超实用)前端地址栏保存&获取参数,地址栏传输中文不在乱码
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://www.cnblogs.com/autoXingJY/p/115965 ...
- 怎样获取iframe节点的window对象
需要使用iframeElement.contentWindow; var frame = document.getElementById('theFrame'); var frameWindow = ...
- element-ui 页面刷新自动弹Message问题
问题: 通过加载插件的方式引入Message,导致每次页面刷新的时候会自动弹出一个通知消息 该情况只在引入局部插件才会引起 import Vue from 'vue' import 'element- ...
- vue创建组件的几种方法
<html> <head> <title>vue创建组件</title> <meta charset="utf-8"> ...
- HTML中的图片标签的用法!
在HTML中<img>这个标签是定义文本中的图片标签,它的作用就比如说可以提供图片的名字.提供图片的尺寸大小和提供图片的一些图片属性,比如Alt这个属性,可以给图片一个名称来告诉朋友们.这 ...
- Linux下知道一个命令却不知道哪个包提供(解决)
[root@localhost ~]# yum -y install jstack (1/2): epel/x86_64/primary_db | 6.8 MB 00:00:16 (2/2): epe ...
- MySQL 数据库的高可用性分析
MySQL数据库是目前开源应用最大的关系型数据库,有海量的应用将数据存储在MySQL数据库中.存储数据的安全性和可靠性是生产数据库的关注重点.本文分析了目前采用较多的保障MySQL可用性方案. MyS ...
- 凤凰新闻APP的增长黑客流程步骤经验:3.5星|《我不是产品经理》
“ 我问了他三个问题.●你是AI科学家或者算法工程师吗?答:不是.●你想天天坐在电脑旁点鼠标或者打电话吗?答:不想.●你愿意每天盯着数据仪表盘定策略并与生产者做运营沟通吗?答:不愿意.我回答他:你别去 ...
- Educational Codeforces Round 41 (Rated for Div. 2) D. Pair Of Lines (几何,随机)
D. Pair Of Lines time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- OpenLayer3入门——[一]
一.OpenLayer3下载 首先下载OpenLayer3开发包,步骤如下: 下载地址https://github.com/openlayers/openlayers/releases/tag/v3. ...