BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec Memory Limit: 128 MB
Submit: 9280 Solved: 2421
[Submit][Status][Discuss]
Description
Input
Output
M行,表示每个询问的答案。最后一个询问不输出换行符
Sample Input
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
8
9
105
7
HINT
//BZOJ 2588 可持久化线段树+LCA
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m
#define rson m+1,r int ans,tot,cnt,lca;
int n,d,q;
int a[maxn],b[maxn],h[maxn],L[maxn<<],R[maxn<<];
int to[maxn<<],head[maxn],Next[maxn<<];//存图
int sum[maxn<<],root[maxn],dep[maxn];
int fa[maxn][]; void add(int x,int y)//存图
{
tot++;
Next[tot]=head[x];
head[x]=tot;
to[tot]=y;
} int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=;i<=;i++) if(((dep[x]-dep[y])&(<<i))!=) x=fa[x][i];//不知道为什么我注释掉的那种写法为什么不对。。。
//for(int i=0;i<=19;i++) if(dep[x]==dep[y]-(1<<i)) x=fa[x][i];
if(x==y) return x;
for(int i=;i>=;i--){
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
} void update(int pre,int &rt,int l,int r,int k)
{
rt=++cnt;sum[rt]=sum[pre]+;
L[rt]=L[pre];R[rt]=R[pre];
if(l==r){
return ;
} int m=(l+r)>>;
if(k<=m) update(L[pre],L[rt],lson,k);
else update(R[pre],R[rt],rson,k);
} int query(int x,int y,int lca,int fa,int l,int r,int k)
{
if(l==r){
return h[l];
} int m=(l+r)>>;
int now=sum[L[x]]+sum[L[y]]-sum[L[lca]]-sum[L[fa]];
if(now>=k) return query(L[x],L[y],L[lca],L[fa],lson,k);
else return query(R[x],R[y],R[lca],R[fa],rson,k-now);
} void dfs(int x,int fath)//按照dfs进行更新
{
dep[x]=dep[fath]+;
int k=lower_bound(b+,b++d,a[x])-b;
h[k]=a[x];
update(root[fath],root[x],,n,k);
for(int i=;i<=;i++){
fa[x][i]=fa[fa[x][i-]][i-];
}
for(int i=head[x];i;i=Next[i]){
if(to[i]!=fath){
fa[to[i]][]=x;
dfs(to[i],x);
}
}
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
d=unique(b+,b++n)-(b+);//去重建立权值线段树
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(,);
while(q--){
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
x=x^ans;
lca=LCA(x,y);
ans=query(root[x],root[y],root[lca],root[fa[lca][]],,n,k);//按照LCA,从x到lca,然后从lca到y,去掉lca的爸爸。
printf("%d\n",ans);
}
}
BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)的更多相关文章
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...
- 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 ...
- 主席树 || 可持久化线段树 || 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
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- HashMap详谈以及实现原理
(一).HashMap 基于哈希表的 Map 接口的实现 允许使用 null 值和 null 键 HashMap不是线程安全,想要线程安全,Collections类的静态方法synchronizedM ...
- apache log 按日期记录 格式 <GOOD>-- (转)
在apache的配置文件中找到ErrorLog logs/error_logCustomLog logs/access_log common Linux系统配置方法: 将其改为ErrorLog “| ...
- hdu 2059 龟兔赛跑(动态规划DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) M ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- Part2-HttpClient官方教程-Chapter7-高级主题(Advanced topics) (HTTP Caching)
原文链接 7.1 自定义客户端连接 在某些情况下,为了能够处理非标准的.不兼容的行为,可能需要自定义HTTP消息通过网络传输的方式,而不是使用HTTP参数.例如,对于web爬虫,可能有必要迫使Http ...
- 【转】MP3文件原理及结构解析
1.引言文件压缩技术的日新月异使得MP3成为时下最烫手的音乐格式,优质的音乐随着0与1的排列迅速散布 到世界各地,撼动人心.何谓MP3?MP3的全称是MPEG Audio Layer 3,它是一种高效 ...
- vuejs怎么在服务器部署?
通过npm run build 把生成的dist文件夹(不要上传文件夹)里的内容上传到http服务器上就可以通过 http来访问了,开发机上正常,上传以后 程序出现错误不能运行的原因99.99%的可能 ...
- 【Python学习笔记】异常处理try-except
Python异常处理 我们一般使用try-except语句来进行异常处理. 使用except Exception as err可以统一捕捉所有异常,而也可以分开处理单个异常. # 分开捕捉单个异常 t ...
- (十九)git版本管理软件——搭建git服务器
创建管理员git 为管理员用户添加sudo权限 生成管理员秘钥 设置管理员git提交账号和邮箱 下载安装gitolite 启动gitolite 添加项目版本库 添加项目成员 项目成员下载项目 gito ...
- c语言简单实现telnet客户端
c语言简单实现telnet客户端 http://blog.csdn.net/haiwenchen/article/details/69944118