【bzoj 2588】Spoj 10628. Count on a tree
Description
Input
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
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=1e5+;
int n,m,tot,x,y,rk,cnte,ind,cnt,temp,lastans;
int v[N],tmp[N],first[N],id[N],num[N],root[N];
int deep[N],fa[N][];
struct node{int lc,rc,sum;}tr[N*];
struct edge{int to,next;}e[N*];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void ins(int u,int v){e[++cnte]=(edge){v,first[u]};first[u]=cnte;}
void insert(int u,int v){ins(u,v);ins(v,u);}
void dfs(int x)
{
ind++;id[x]=ind;num[ind]=x;
for(int i=;(<<i)<=deep[x];i++)fa[x][i]=fa[fa[x][i-]][i-];
for(int i=first[x];i;i=e[i].next)
{
if(deep[e[i].to])continue;
fa[e[i].to][]=x;deep[e[i].to]=deep[x]+;dfs(e[i].to);
}
}
int lca(int ri,int rj)
{
if(deep[ri]<deep[rj])swap(ri,rj);
int d=deep[ri]-deep[rj];
for(int i=;(<<i)<=d;i++)if((<<i)&d)ri=fa[ri][i];
if(ri==rj)return ri;
for(int i=;i>=;i--)
if((<<i)<=deep[ri]&&fa[ri][i]!=fa[rj][i])
ri=fa[ri][i],rj=fa[rj][i];
return fa[ri][];
}
void update(int &x,int last,int L,int R,int num)
{
x=++cnt;tr[x].sum=tr[last].sum+;
if(L==R)return;
tr[x].lc=tr[last].lc;tr[x].rc=tr[last].rc;
int mid=(L+R)>>;
if(num<=mid)update(tr[x].lc,tr[last].lc,L,mid,num);
else update(tr[x].rc,tr[last].rc,mid+,R,num);
}
int query(int x,int y,int rk)
{
int a=x,b=y,c=lca(x,y),d=fa[c][];
a=root[id[x]];b=root[id[b]];c=root[id[c]];d=root[id[d]];
int L=,R=tot;
while(L<R)
{
int mid=(L+R)>>;
temp=tr[tr[a].lc].sum+tr[tr[b].lc].sum-tr[tr[c].lc].sum-tr[tr[d].lc].sum;
if(temp>=rk)R=mid,a=tr[a].lc,b=tr[b].lc,c=tr[c].lc,d=tr[d].lc;
else rk-=temp,L=mid+,a=tr[a].rc,b=tr[b].rc,c=tr[c].rc,d=tr[d].rc;
}
return tmp[L];
}
int main()
{
n=read();m=read();
for(int i=;i<=n;i++)v[i]=tmp[i]=read();
sort(tmp+,tmp+n+);tot=unique(tmp+,tmp+n+)-tmp-;
for(int i=;i<=n;i++)v[i]=lower_bound(tmp+,tmp+tot+,v[i])-tmp;
for(int i=;i<n;i++)x=read(),y=read(),insert(x,y);
deep[]=;dfs();
for(int i=;i<=n;i++)
{
temp=num[i];
update(root[i],root[id[fa[temp][]]],,tot,v[temp]);
}
for(int i=;i<=m;i++)
{
x=read();y=read();rk=read();x^=lastans;
lastans=query(x,y,rk);printf("%d",lastans);
if(i!=m)printf("\n");
}
return ;
}
【bzoj 2588】Spoj 10628. 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 ...
- 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- 【BZOJ】【2588】COT(Count On a Tree)
可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...
- 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(树链剖分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 ...
随机推荐
- [SHOI2001]化工厂装箱员(dp?暴力:暴力)
118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度 ...
- 洛谷P2704 炮兵阵地
本题过于经典...... 对于这种网格状压DP,套路一波刷表法DFS转移就没了. 三进制状压,0表示当前,上一个都没有.1表示当前无,上一个有.2表示当前有. 转移的条件就是上一行为0,当前不是山地, ...
- js小结
1,浏览器对json支持的方法: JSON.parse(jsonstr);将string转为json的对象. JSON.stringify(jsonobj);将json对象转为string. 2,js ...
- textarea高度自适应自动展开
在使用之前,推荐两个比较好的事件,分别是oninput和onpropertychange,IE9以下不兼容oninput.在textarea发生变化时,可以通过监听这两个事件来触发你需要的功能. te ...
- C++基础知识--DAY2
昨天我们主要是讲的C++相对于C语言的变化,结尾讲述了一点引用的基础知识,要明白,引用就是对一个变量取别名,在C++中需要用指针的都可以思考是否可以用引用来代替. 1. 常引用 常引用(const s ...
- Python 的文件处理
Python提供了os.shutil.glob 等开发包处理文件 一.OS包 os包中包含了目录创建.目录删除.文件创建.执行系统命令等方法. import os remove() 删除文件 mk ...
- Android Studio 签名 安全图片
apk 输出地址: /Users/houzhibin/javaself/android/SpgApp/app/build/outputs/apk 上图是debug版的: 发布版的需要在终端输入命令行: ...
- docker基础之镜像
获取镜像 从 Docker Registry 获取镜像的命令是 docker pull.其命令格式为: docker pull [选项] [Docker Registry地址]<仓库名>: ...
- Ansible Callback
非api模式下自定义callback ansible.cfg中开启callback功能 callback_plugins = /usr/share/ansible/plugins/callback # ...
- thinkpad yoga 12 / thinkpad s1 yoga / WS860
s 序号 IP地址 MAC地址 主机名 设备商 1 192.168.3.6 34-02-86-29-46-8B Intel(R) Dual Band Wireless-AC 7265 Intel公司/ ...