【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 ...
随机推荐
- Ubuntu16.04下的NetCore环境搭建(附录含Ubuntu 18.04 安装 NetCore2.1)
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux VSCode安装:http://www.cnblogs.com/dunitia ...
- HDU/HDOJ 4864 Task
贪心题. 贪心方法很是naive...... 首先我们就能注意到一个性质:优先选择时间(x)长的,然后才是等级(y). 所以我们把机器和任务排好序,从大到小枚举任务.对于每一个x满足的机器,x也一定满 ...
- pandas 连接数据库直接查表建立dataframe。loc,sort_values数据清洗操作
#导入pandas import pandas as pd import numpy as np #导入SqlAlchemy from sqlalchemy import create_engine ...
- c 结构体 & 函数指针模拟实现一个java class(类) 和方法
闲来无事,纯粹练习. student.h #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED #include <memory.h> ...
- linux系统调用之进程控制
1 进程控制: fork 创建一 ...
- 爬虫之requests请求库高级应用
1.SSL Cert Verification #证书验证(大部分网站都是https) import requests respone=requests.get('https://www.12306. ...
- http协议中的请求方式
get:获取url传的查询字符串(action=show)表单和连接的url中传的值.容量2K左右. post:以post方式提交,获取表单和连接的url中传的值.容量8M左右. delete:删除某 ...
- 8.Django
##update 操作更新数据
- day10-(rr)
回顾: http:: 超文本传输协议 请求和响应 servlet: 运行在服务器端的一个java小程序,本质就是一个类 接受请求,处理逻辑,生成动态内容 编写步骤: 1.编写一个类 继承HttpSer ...
- 新买苹果电脑,mac系统中小白应该了解哪些东西?
本文旨在分享新买了mac电脑,应该做哪些设置,帮助苹果电脑小白轻松上手使用mac电脑,当然,新电脑肯定是需要安装各种软件,这里,小编推荐一下可以看看小编写的mac软件装机必备Mac 装机必备软件推荐, ...