BZOJ 2588 Count on a tree (COT) 是持久的段树

BZOJ 2588 Count on a tree (COT) 是持久的段树

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

标题效果:两棵树之间的首次查询k大点的权利。

思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树。

由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法。详见代码。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define NIL (tree[0])
using namespace std;

pair<int,int> src[MAX];

struct Complex{
	Complex *son[2];
	int cnt;

	Complex(Complex *_,Complex *__,int ___):cnt(___) {
		son[0] = _;
		son[1] = __;
	}
	Complex() {}
}*tree[MAX];

int points,asks;
int xx[MAX];

int head[MAX],total;
int next[MAX << 1],aim[MAX << 1];

int father[MAX][20],deep[MAX];

inline void Add(int x,int y);
void DFS(int x);
void SparseTable();

Complex *BuildTree(Complex *pos,int x,int y,int val);
int GetAns(Complex *l,Complex *r,Complex *f,Complex *p,int x,int y,int k);
int GetLCA(int x,int y);
int Ask(int x,int y,int k);

int main()
{
	cin >> points >> asks;
	for(int i = 1;i <= points; ++i)
		scanf("%d",&src[i].first),src[i].second = i;
	sort(src + 1,src + points + 1);
	for(int i = 1;i <= points; ++i)
		xx[src[i].second] = i;
	for(int x,y,i = 1;i < points; ++i) {
		scanf("%d%d",&x,&y);
		Add(x,y),Add(y,x);
	}
	NIL = new Complex(NULL,NULL,0);
	NIL->son[0] = NIL->son[1] = NIL;
	DFS(1);
	SparseTable();
	int now = 0;
	for(int x,y,k,i = 1;i <= asks; ++i) {
		scanf("%d%d%d",&x,&y,&k);
		printf("%d",now = src[Ask(x ^ now,y,k)].first);
		if(i != asks)	puts("");
	}
	return 0;
}

inline void Add(int x,int y)
{
	next[++total] = head[x];
	aim[total] = y;
	head[x] = total;
}

void DFS(int x)
{
	deep[x] = deep[father[x][0]] + 1;
	tree[x] = BuildTree(tree[father[x][0]],1,points,xx[x]);
	for(int i = head[x];i;i = next[i]) {
		if(aim[i] == father[x][0])	continue;
		father[aim[i]][0] = x;
		DFS(aim[i]);
	}
}

void SparseTable()
{
	for(int j = 1;j <= 19; ++j)
		for(int i = 1;i <= points; ++i)
			father[i][j] = father[father[i][j - 1]][j - 1];
}

int Ask(int x,int y,int k)
{
	int lca = GetLCA(x,y);
	return GetAns(tree[x],tree[y],tree[lca],tree[father[lca][0]],1,points,k);
}

Complex *BuildTree(Complex *pos,int x,int y,int val)
{
	int mid = (x + y) >> 1;
	if(x == y)	return new Complex(NIL,NIL,pos->cnt + 1);
	if(val <= mid)	return new Complex(BuildTree(pos->son[0],x,mid,val),pos->son[1],pos->cnt + 1);
	return new Complex(pos->son[0],BuildTree(pos->son[1],mid + 1,y,val),pos->cnt + 1);	
}

int GetLCA(int x,int y)
{
	if(deep[x] < deep[y])	swap(x,y);
	for(int i = 19; ~i; --i)
		if(deep[father[x][i]] >= deep[y])
			x = father[x][i];
	if(x == y)	return x;
	for(int i = 19; ~i; --i)
		if(father[x][i] != father[y][i])
			x = father[x][i],y = father[y][i];
	return father[x][0];
}

int GetAns(Complex *l,Complex *r,Complex *f,Complex *p,int x,int y,int k)
{
	if(x == y)	return x;
	int mid = (x + y) >> 1;
	int temp = l->son[0]->cnt + r->son[0]->cnt - f->son[0]->cnt - p->son[0]->cnt;
	if(k <= temp)	return GetAns(l->son[0],r->son[0],f->son[0],p->son[0],x,mid,k);
	return GetAns(l->son[1],r->son[1],f->son[1],p->son[1],mid + 1,y,k - temp);
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117451.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月5日 下午5:00
下一篇 2022年1月5日 下午5:00


相关推荐

  • spss 13.0 英文正式版14个模块的授权号码

    spss 13.0 英文正式版14个模块的授权号码

    2021年5月3日
    157
  • 21.shell语言之if条件判断语句

    21.shell语言之if条件判断语句shell的if语法和C语言等高级语言非常相似,唯一需要注意的地方就是shell的if语句对空格方面的要求比较严格(其实shell对所有语法的空格使用都比较严格)。注意:1、[]表示条件测试。注意这里的空格很重要。要注意在’[‘后面和’]’前面都必须要有空格2、在shell中,then和fi是分开的语句。如果要在同一行里面输入,则需要用分号将他们隔开。3、注意if判断中对于变量的处理,需要加引号,以免一些不必要的错误。

    2022年7月11日
    22
  • 如何卸载cuda

    如何卸载cuda============Summary============Driver:InstalledToolkit:Installedin/usr/local/cuda-10.2/Samples:Installedin/home/game/,butmissingrecommendedlibrariesPleasemakesurethat-…

    2022年6月21日
    45
  • 住宅区和住宅建筑内光纤到户通信设施工程设计规范_光纤入户的配置原则及设计案例…

    住宅区和住宅建筑内光纤到户通信设施工程设计规范_光纤入户的配置原则及设计案例…一 强制性原则住宅区和住宅建筑内光纤到户通信设施工程的设计 必须满足多家电信业务经营者平等接入 用户可自由选择电信业务经营者的要求 1 地下通信管道的管孔容量 2 用户接入点处预留的配线设备安装空间 3 电信间及设备间面积应满足至少 3 家电信业务经营者通信业务接入的需要 二 用户光缆容量配置三 管道及光缆配置 1 根据用户数确定光缆总容量 2 光缆各段光纤芯数根据光纤接入方式 住宅建筑类型 所辖住户数计算

    2026年3月16日
    2
  • 关于Navicat ER图「建议收藏」

    关于Navicat ER图「建议收藏」今天给大家分享一个关于NavicatER图使用的小技巧,这里以NavicatforMySQL10.1.7为例。首先说一下ER图在哪里打开点击“查看”,选择“ER图表”这时就可以显示你建好的各个表了。虽然加了外键,但是显示不是很清晰,怎么办?在NavicatforMySQL界面的左下角有一个刷新按钮点击后,即可生成关系明确的ER图了如果点击刷新后仍没反应,请点击“重新…

    2025年11月27日
    14
  • 正则表达式匹配_正则表达式匹配字符串长度

    正则表达式匹配_正则表达式匹配字符串长度题目描述请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意

    2022年8月2日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号