传送门

刚开始看错题以为是dfs序瞎搞..

后来看清题了开始想用树剖瞎搞...

感觉要滚粗啊..

对于每个点到根的路径建立线段树,暴力建MLE没跑,上主席树,然后$(x,y)$的路径就可以先求出来$LCA$,然后就可以用$T_x+T_y-T_{LCA}-T_{fa[LCA]}$就行了。

//BZOJ 2588
//by Cydiater
//2016.12.8
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n)		for(int i=j;i<=n;i++)
#define down(i,j,n)		for(int i=j;i>=n;i--)
#define cmax(a,b)		a=max(a,b)
#define cmin(a,b)		a=min(a,b)
#define Auto(i,node)		for(int i=LINK[node];i;i=e[i].next)
const int MAXN=1e5+5;
const int oo=0x3f3f3f3f;
inline int read(){
	char ch=getchar();ll x=0,f=1;
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int N,M,dfn[MAXN],dfs_clock=0,fsort[MAXN],val[MAXN],root[MAXN],rnum=0,cnt=0,fa[MAXN][25],ans=0;
struct Graph{
	int LINK[MAXN],len,dep[MAXN];
	struct edge{
		int y,next;
	}e[MAXN<<1];
	inline void insert(int x,int y){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;}
	inline void Insert(int x,int y){insert(x,y);insert(y,x);}
	void make_graph(){
		N=read();M=read();
		up(i,1,N)val[i]=fsort[i]=read();
		sort(fsort+1,fsort+N+1);
		rnum=unique(fsort+1,fsort+N+1)-(fsort+1);
		up(i,1,N)val[i]=lower_bound(fsort+1,fsort+rnum+1,val[i])-fsort;
		up(i,1,N-1){
			int x=read(),y=read();
			Insert(x,y);
		}
	}
	void dfs(int node,int deep,int father){
		dfn[++dfs_clock]=node;
		dep[node]=deep;fa[node][0]=father;
		Auto(i,node)if(e[i].y!=father)
			dfs(e[i].y,deep+1,node);
	}
	void set_anc(){
		up(i,1,20)up(node,1,N)
			if(fa[node][i-1])fa[node][i]=fa[fa[node][i-1]][i-1];
	}
	int LCA(int x,int y){
		if(x==y)		return x;
		if(dep[x]<dep[y])	swap(x,y);
		down(i,20,0)if(dep[x]-(1<<i)>=dep[y])x=fa[x][i];
		if(x==y)		return x;
		down(i,20,0)if(fa[x][i]!=0&&fa[x][i]!=fa[y][i]){
			x=fa[x][i];
			y=fa[y][i];
		}
		return fa[x][0];
	}
}G;
struct Chair_man_Tree{
	int son[2],sum;
}t[MAXN<<5];
namespace solution{
	int NewNode(int sum,int son0,int son1){
		t[++cnt].sum=sum;t[cnt].son[0]=son0;t[cnt].son[1]=son1;
		return cnt;
	}
	void insert(int leftt,int rightt,int &Root,int last,int pos){
		Root=NewNode(t[last].sum+1,t[last].son[0],t[last].son[1]);
		int mid=(leftt+rightt)>>1;
		if(leftt==rightt)	return;
		if(pos<=mid)		insert(leftt,mid,t[Root].son[0],t[last].son[0],pos);
		else			insert(mid+1,rightt,t[Root].son[1],t[last].son[1],pos);
	}
	void Prepare(){
		G.make_graph();
		G.dfs(1,0,0);
		G.set_anc();
		up(i,1,N){
			int node=dfn[i],father=fa[node][0],pos=val[node];
			insert(1,rnum,root[node],root[father],pos);
		}
	}
	int Get(int leftt,int rightt,int rx,int ry,int rlca,int rflca,int rnk){
		int sum=t[t[rx].son[0]].sum+t[t[ry].son[0]].sum-t[t[rlca].son[0]].sum-t[t[rflca].son[0]].sum;
		if(leftt==rightt)	return fsort[leftt];
		int mid=(leftt+rightt)>>1;
		if(rnk<=sum)	return Get(leftt,mid,t[rx].son[0],t[ry].son[0],t[rlca].son[0],t[rflca].son[0],rnk);
		else		return Get(mid+1,rightt,t[rx].son[1],t[ry].son[1],t[rlca].son[1],t[rflca].son[1],rnk-sum);
	}
	void Slove(){
		while(M--){
			int x=read()^ans,y=read(),k=read(),lca=G.LCA(x,y);
			printf("%d",ans=Get(1,rnum,root[x],root[y],root[lca],root[fa[lca][0]],k));
			if(M)puts("");
		}
	}
}
int main(){
	//freopen("input.in","r",stdin);
	using namespace solution;
	Prepare();
	Slove();
	return 0;
}

BZOJ2588: Spoj 10628. Count on a tree的更多相关文章

  1. bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)

    每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]]) #include<iostream> #i ...

  2. 【主席树】bzoj2588 Spoj 10628. Count on a tree

    每个点的主席树的root是从其父转移来的.询问的时候用U+V-LCA-FA(LCA)即可. #include<cstdio> #include<algorithm> using ...

  3. 主席树初探--BZOJ2588: Spoj 10628. Count on a tree

    n<=100000的点权树,有m<=100000个询问,每次问两个点间的第k小点权,保证有解,强制在线. 主席上树啦!类似于之前的序列不带修改询问的前缀表示法,现在只要把前缀当成某点到根的 ...

  4. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

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

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

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

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

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

  9. 2588: Spoj 10628. Count on a tree

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5766  Solved: 1374 ...

随机推荐

  1. arcgis api for js入门开发系列三地图工具栏(含源代码)

    上一篇实现了demo的地图加载展示,在上篇实现的基础上,新增了地图工具栏以及通用地图控件功能,比如地图框选缩放.地图漫游.清空.量算工具.地图导航控件.地图比例尺控件.地图鹰眼图等等,总共分为5个部分 ...

  2. iOS 10对隐私权限的管理(必须要改否则会crash)

    比如访问的摄像头.麦克风等硬件,都需要提前请求应用权限.允许后才可以使用,或者现在要提前声明,虽然以往要求不严格.比如在iOS10中访问通讯录时,强制必须在Info.plist中加入NSContact ...

  3. 做一个 App 前需要考虑的几件事

    做一个 App 前需要考虑的几件事  来源:limboy的博客   随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...

  4. Reactor 模式的简单实现

    Reactor 模式简单实现 在网上有部分文章在描述Netty时,会提到Reactor.这个Reactor到底是什么呢?为了搞清楚Reactor到底是什么鬼,我写了一个简单的Demo,来帮助大家理解他 ...

  5. C#中DateTime.Ticks属性及Unix时间戳转换

    1.相关概念 DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). ...

  6. python(一)

    python数学函数 abs(x) 返回数字的绝对值,如abs(-10) 返回 10 ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5 cmp(x, y) 如果 x < ...

  7. DevExpress ChartControl 样式设置

    第三方控件,设置ChartControl的样式,仅供参考 Demo: <Grid> <Grid.Resources> <SolidColorBrush x:Key=&qu ...

  8. js判断用户是否正在滚动滚动条,滚动条滚动是否停止

    js智能判断是否可以自动滚动 比如,做一个音乐播放器,边播放,边定位歌词,播放的时候,需要自动定位到播放语句,但是用户去拖动或者滚动div(歌词面板)时,这时就必须停止自动滚动,或者说是不能自动滚动, ...

  9. Unity3D 利用欧拉角实现实体的旋转

    本文系作者原创,转载请注明出处 刚刚入门U3D,也是很多东西搞不懂,最先接触的就是自己尝试去获取键盘上的GetPress之类的事件了吧 官方的API DOC也是帮了不少忙,到处吸收了各位博主的文章也是 ...

  10. js中typeOf用法

    JS中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据. typeof 可以用来检测给定变量的数据类型,可能的返回值:1. 'undefined' --- 这个值未定义: 2. 'boole ...