BZOJ3551 [ONTAK2010]Peaks加强版

[Solution]

It's easy to come up with the conclusion that you can convert the problem into queries on a minimal spanning tree. You need to find the k-th max value of a piece that the edge values are no more than x.

However, it's not easy to find a piece because the may be not continuous on the DFS order, while I'm not able to have other ways to deal with a piece.

We can consider like this: we make a tree in the order of disjointing set. For each merging, we create a new node which has a value of the original edge's value. Therefore, the sons of a node can be always merged using edges whose values are no more than x.

We make a DFS in that order. And use chairman tree to modify the k-th height.


[Code]

It's so long and ugly. Because i was afraid of MLE, I used dynamic memory management.

#include <cstdio>
#include <cstdlib>
#include <memory.h>
#include <ctype.h>
#include <algorithm>

using namespace std;

struct edgeG {
	int a, b, v;
};
struct edgeT {
	int t;
	edgeT* next;
};
struct seg {
	int s;
	seg *ls, *rs;
};

const int maxp = 100010;
const int maxn = 200010;
const int maxe = 500010;
const int maxl = 22;
const int psz = 800000;
const int maxm = 1000000010;
const int inf = 0x3f3f3f3f;

edgeG eo[maxe];
edgeT *head[maxn], *ep;
seg *sp, *rt[maxn];
int n, m, tq, t, csn, lans, h[maxn];
int r[maxn], v0[maxn];
int dfb[maxn], dfe[maxn], odf[maxn], dfo;
int ath[maxn][maxl], mvl[maxn][maxl];

int nextInt() {
	int s = 0, d;
	bool flag = 0;
	do {
		d = getchar();
		if (d == '-')
			flag = 1;
	} while (!isdigit(d));
	do 
		s = s * 10 + d - 48, d = getchar();
	while (isdigit(d));
	return flag ? -s : s;
}

inline bool cmpEdgeG(edgeG a, edgeG b) {
	return a. v < b. v;
}

inline void addEdge(int u, int v) {
   ep-> t = v;
   ep-> next = head[u];
   head[u] = ep ++;
}

inline seg* newSeg() {
	return (!csn) ? (csn = psz, sp = new seg[psz + 2]) : (csn --, ++ sp);
}

inline int getRoot(int x) {
	return (r[x] == x) ? x : (r[x] = getRoot(r[x]));
}

#define getls(x) ((x)?(x->ls):0)
#define getrs(x) ((x)?(x->rs):0)
#define getsum(x) ((x)?(x->s):0)

seg* sgtIns(seg* q, int p0) {
	int l = 0, r = maxm;
	seg *s = newSeg(), *p = s;
	while (l < r) {
		int m = (l + r) >> 1;
		p-> s = getsum(q) + 1;
		if (p0 <= m) {
			r = m;
			p-> rs = getrs(q);
			q = getls(q);
			p-> ls = newSeg();
			p = p-> ls;
		}
		else {
			l = m + 1;
			p-> ls = getls(q);
			q = getrs(q);
			p-> rs = newSeg();
			p = p-> rs;
		}
	}
	p-> s = getsum(q) + 1;
	return s;
}

int sgtKth(seg *q, seg *p, int k) {
	if (getsum(p) - getsum(q) < k)
		return -1;
	int l = 0, r = maxm;
	while (l < r) {
		int sr = getsum(getrs(p)) - getsum(getrs(q));
		int m = (l + r) >> 1;
		if (sr >= k) {
			l = m + 1;
			p = getrs(p);
			q = getrs(q);
		}
		else {
			k -= sr;
			r = m;
			p = getls(p);
			q = getls(q);
		}
	}
	return l;
}

void dfs(int p) {
	dfb[p] = ++ dfo;
	odf[dfo] = p;
	for (int i = 1; i < maxl; i ++) {
		ath[p][i] = ath[ath[p][i - 1]][i - 1];
		mvl[p][i] = max(mvl[p][i - 1], mvl[ath[p][i - 1]][i - 1]);
	}
	for (edgeT* g = head[p]; g; g = g-> next) {
		ath[g-> t][0] = p;
		mvl[g-> t][0] = v0[p];
		dfs(g-> t);
	}
	dfe[p] = dfo;
}

void pre() {
	sort(eo, eo + m, cmpEdgeG);
	t = n;
	for (int i = 1; i <= n * 2; i ++)
		r[i] = i, v0[i] = 0;
	for (int i = 0; i < m; i ++)
		if (getRoot(eo[i]. a) != getRoot(eo[i]. b)) {
			t ++;
			h[t] = -1;
			v0[t] = eo[i]. v;
			addEdge(t, getRoot(eo[i]. a));
			addEdge(t, getRoot(eo[i]. b));
			r[getRoot(eo[i]. a)] = t;
			r[getRoot(eo[i]. b)] = t;
		}
	t ++;
	for (int i = 1; i < t; i ++)
		if (r[i] == i)
			addEdge(t, i);
	dfo = 0;
	ath[t][0] = 0;
	mvl[t][0] = inf;
	v0[t] = inf;
	dfs(t);
	rt[0] = 0;
	for (int i = 1; i <= t; i ++)
		if (h[odf[i]] > -1)
			rt[i] = sgtIns(rt[i - 1], h[odf[i]]);
		else
			rt[i] = rt[i - 1];
}

int query(int p, int x, int k) {
	for (int i = maxl - 1; i >= 0; i --)
		if (mvl[p][i] <= x)
			p = ath[p][i];
	return sgtKth(rt[dfb[p] - 1], rt[dfe[p]], k);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	memset(head, 0, sizeof(head));
	ep = new edgeT[maxn * 2];
	csn = 0;
	n = nextInt();
	m = nextInt();
	tq = nextInt();
	for (int i = 1; i <= n; i ++)
		h[i] = nextInt();
	for (int i = 0; i < m; i ++) {
		eo[i]. a = nextInt();
		eo[i]. b = nextInt();
		eo[i]. v = nextInt();
	}
	pre();
	lans = 0;
	while (tq --) {
		if (lans == -1)
			lans = 0;
		int v = nextInt() ^ lans;
		int x = nextInt() ^ lans;
		int y = nextInt() ^ lans;
		printf("%d\n", (lans = query(v, x, y)));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值