hdu 4366 Successor

Interesting, I haven't seen this kind of transform before(it proves that i am remain a newbie).

I try to slove it by using sorts of brute-force seg-tree-update method.

Of course I would get a TLE.

At last, I look for a solution on website as I have thinking about it a whole afternoon.

To my surprise, It's an amazing offline algorithm.

We can transform the tree structure to an array that any subtree is in a continuous interval.

So when staff q is fired, we can find his subordinates with Maximum loyal value in an interval which q's subtree is in.

As to guarantee that his subordinates's ability is higher than him, we sort all staff in descending order according to their ability.

There is. To any specific staff, we can find the one to replace him in advance.

In this case, coding an ordinary RMQ seg-tree is enough.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <queue>
#include <stack>
using namespace std;
#define MS(c, a) memset(c, a, sizeof c)
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
#define MAXN (50050)
#define MAXM (1000010)
#define INFI (0x7fffffff)

struct Edge
{
	int v, next;
};
vector<Edge> E;
int L[MAXN];
void G_ini()
{
	E.clear();
	MS(L, -1);
}
void AE(int u, int v)
{
	Edge te = {v, L[u]};
	L[u] = E.size();
	E.push_back(te);
}

struct Stf
{
	int sr, ll, ay, l, r;
	void inp()
	{
		scanf("%d%d%d", &sr, &ll, &ay);
	}
} s[MAXN];
bool cmp(int p, int q)
{
	return s[p].ay > s[q].ay;
}

typedef struct SNode
{
	int l, r, m, mx;
	SNode *ls, *rs;
	SNode()
	{
		l = r = m = mx = -1;
		ls = rs = 0;
	}
	void set(int _mx)
	{
		mx = _mx;
	}
	void upd(SNode *ls, SNode *rs)
	{
		mx = max(ls->mx, rs->mx);
	}
} *PSNode;
SNode St[MAXN << 1], *Stop = St;
struct SGT
{
	PSNode rt;
	void bui(int l, int r, PSNode x)
	{
		x->l = l;
		x->r = r;
		x->m = (l + r) >> 1;
		x->set(-1);
		if (l == r) return ;
		bui(l, x->m, x->ls = ++Stop);
		bui(x->m + 1, r, x->rs = ++Stop);
	}
	void ins(int l, int r, int _mx, PSNode x)
	{
		if (l <= x->l && x->r <= r) x->set(_mx);
		else
		{
			if (l <= x->m) ins(l, r, _mx, x->ls);
			if (x->m < r) ins(l, r, _mx, x->rs);
			x->upd(x->ls, x->rs);
		}
	}
	SNode que(int l, int r, PSNode x)
	{
		SNode res;
		if (l <= x->l && x->r <= r) res = *x; 
		else
		{
			SNode lre, rre;
			if (l <= x->m) lre = que(l, r, x->ls);
			if (x->m < r) rre = que(l, r, x->rs);
			x->upd(x->ls, x->rs);
			res.upd(&lre, &rre);
		}
		return res;
	}
} sgt;

int n, m, id[MAXN], ind, res[MAXM], H[MAXM];

void TTI(int u)
{
	s[u].l = ind++;
	for (int i = L[u]; i != -1; i = E[i].next) TTI(E[i].v);
	s[u].r = ind - 1;
}

int main()
{
	int T;

	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &m);
		G_ini();
		MS(H, -1);
		Rep(i, 1, n)
		{
			s[i].inp();
			H[s[i].ll] = id[i] = i;
			AE(s[i].sr, i);
		}
		TTI(ind = 0);
		sort(id + 1, id + n, cmp);
		sgt.bui(0, n - 1, sgt.rt = Stop = St);
		for (int i = 1; i < n;) 
		{
			int j;
			for (j = i; j < n && s[id[j]].ay == s[id[i]].ay; j++)
			{
				SNode x;
				if (s[id[j]].l < s[id[j]].r)
					x = sgt.que(s[id[j]].l + 1, s[id[j]].r, sgt.rt);
				res[id[j]] = x.mx < 0? -1: H[x.mx];
			}
			for (j = i; j < n && s[id[j]].ay == s[id[i]].ay; j++)
				sgt.ins(s[id[j]].l, s[id[j]].l, s[id[j]].ll, sgt.rt);
			i = j;
		}
		while (m--)
		{
			int q;
			scanf("%d", &q);
			printf("%d\n", res[q]);	
		}
	}
	return 0;
}

Hoho.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值