HDU 5877 Weak Pair 大连网络赛

题意就是说给你一棵有根树,然后定义了一种有序对叫做weak pair,两个点若满足 第一个点是第二个点的祖先,而且第一个点和第二个点的权值相乘,乘积小于他给的k,那么就是weak pair,方法很简单,就是dfs,从根节点开始向下找,看这个点和他的祖先节点有多少满足情况,比赛时的想法是dfs到某个深度的时候,把之前的所有祖先的权值存在一个数组里,然后去看有多少符合条件的加在ans里,然后超时了,gg,怎么也没想到优化,后来知道有splay树这种东西,可以很快找到有多少符合条件的,但是不会呀,所以看了别人的做法,有用treap的,就用了treap的模板(之前也对这玩意不怎么熟,只知道有这个东西不会用),方法是,看祖先节点中有多少权值小于 k/当前节点权值 ,这样子就比较快了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;

int n, m;
int a[100005];
int in[100005];
int head[100005];
LL k;
LL ans;

int cnt;

struct Edge
{
	int from, to;
	int next;
}edge[100005];

void addedge(int u, int v)
{
	edge[cnt].next = head[u];
	edge[cnt].from = u;
	edge[cnt].to = v;
	head[u] = cnt++;
}


struct Treap
{
	int size;
	int fix;
	LL key;
	Treap *ch[2];

	Treap(LL key)
	{
		size = 1;
		fix = rand();
		this->key = key;
		ch[0] = ch[1] = NULL;
	}
	int cmp(LL x) const
	{
		if (x == key) return -1;
		return x < key ? 0 : 1;
	}
	void Maintain()
	{
		size = 1;
		if (ch[0] != NULL) size += ch[0]->size;
		if (ch[1] != NULL) size += ch[1]->size;
	}
};

void Rotate(Treap* &t, int d)
{
	Treap *k = t->ch[d ^ 1];
	t->ch[d ^ 1] = k->ch[d];
	k->ch[d] = t;
	t->Maintain();
	k->Maintain();
	t = k;
}

void Insert(Treap* &t, LL x)
{
	if (t == NULL) t = new Treap(x);
	else
	{
		int d = (x < t->key ? 0 : 1);
		Insert(t->ch[d], x);
		if (t->ch[d]->fix > t->fix)
			Rotate(t, d ^ 1);
	}
	t->Maintain();
}

void Delete(Treap* &t, LL x)
{
	int d = t->cmp(x);
	if (d == -1)
	{
		Treap *tmp = t;
		if (t->ch[0] == NULL)
		{
			t = t->ch[1];
			delete tmp;
			tmp = NULL;
		}
		else if (t->ch[1] == NULL)
		{
			t = t->ch[0];
			delete tmp;
			tmp = NULL;
		}
		else
		{
			int k = (t->ch[0]->fix > t->ch[1]->fix ? 1 : 0);
			Rotate(t, k);
			Delete(t->ch[k], x);
		}
	}
	else
		Delete(t->ch[d], x);
	if (t != NULL) t->Maintain();
}

bool Find(Treap *t, LL x)
{
	while (t != NULL)
	{
		int d = t->cmp(x);
		if (d == -1) return true;
		t = t->ch[d];
	}
	return false;
}

int Rank(Treap *t, LL x)
{
	int r;
	if (t == NULL) return 0;
	if (t->ch[0] == NULL) r = 0;
	else r = t->ch[0]->size;
	if (x == t->key) return r + 1;
	if (x < t->key)
		return Rank(t->ch[0], x);
	return r + 1 + Rank(t->ch[1], x);
}

void DeleteTreap(Treap* &t)
{
	if (t == NULL) return;
	if (t->ch[0] != NULL) DeleteTreap(t->ch[0]);
	if (t->ch[1] != NULL) DeleteTreap(t->ch[1]);
	delete t;
	t = NULL;
}

void search(int u, Treap* &t)
{
	//if (head[u] == -1) return;
	LL Max = 0x3f3f3f3f3f3f3f3f;
	if (a[u]) Max = k / (a[u]);
	ans += Rank(t, Max);

	Insert(t, a[u]);

	for (int i = head[u]; i != -1; i = edge[i].next)
		search(edge[i].to, t);
	
	Delete(t, a[u]);
	return;
}

int main(void)
{
	//freopen("test.txt", "r", stdin);
	int T;
	scanf("%d", &T);
	while (T--)
	{
		ans = 0;
		cnt = 0;
		memset(in, 0, sizeof in);
		memset(head, -1, sizeof head);
		Treap *root = NULL;
		scanf("%d%lld", &n, &k);
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]);
		int l, r;
		for (int i = 0; i < n - 1; i++)
		{
			scanf("%d%d", &l, &r);
			addedge(l, r);
			in[r]++;
		}
		for (int i = 1; i <= n; i++)
		{
			if (in[i] == 0)
			{
				search(i, root);
				break;
			}
		}
		DeleteTreap(root);
		printf("%lld\n", ans);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值