bzoj 3551 [ONTAK2010]Peaks加强版 kruskal重构树+主席树

题面

题目传送门

解法

建出kruskal重构树,然后变成求子树第\(k\)大问题

直接主席树即可

时间复杂度:\(O(q\ log\ n)\)

代码

#include <bits/stdc++.h>
#define N 200010
using namespace std;
template <typename node> void read(node &x) {
	x = 0; int f = 1; char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
	int x, y, v;
	bool operator < (const Node &a) const {
		return v < a.v;
	}
} a[N * 5];
struct SegmentTree {
	struct Node {
		int lc, rc, cnt;
	} t[N * 30];
	int tot;
	int ins(int k, int l, int r, int x) {
		int ret = ++tot; t[ret] = t[k]; t[ret].cnt++;
		if (l == r) return ret; int mid = (l + r) >> 1;
		if (x <= mid) t[ret].lc = ins(t[k].lc, l, mid, x);
			else t[ret].rc = ins(t[k].rc, mid + 1, r, x);
		return ret;
	}
	int query(int k1, int k2, int l, int r, int x) {
		if (x <= 0) return -1;
		if (l == r) return l;
		int mid = (l + r) >> 1, tmp = t[t[k2].lc].cnt - t[t[k1].lc].cnt;
		if (x <= tmp) return query(t[k1].lc, t[k2].lc, l, mid, x);
		return query(t[k1].rc, t[k2].rc, mid + 1, r, x - tmp);
	}
} T;
int n, m, q, Time, h[N], p[N], s[N], dfn[N], v[N], siz[N], rt[N], f[N][21];
vector <int> E[N];
int Find(int x) {
	if (p[x] == x) return x;
	return p[x] = Find(p[x]);
}
void dfs(int x, int fa) {
	dfn[x] = ++Time, siz[x] = 1;
	if (!h[x]) rt[Time] = rt[Time - 1];
		else rt[Time] = T.ins(rt[Time - 1], 1, 1e9, h[x]);
	for (int i = 1; i <= 20; i++)
		f[x][i] = f[f[x][i - 1]][i - 1];
	for (int i = 0; i < E[x].size(); i++) {
		int k = E[x][i]; f[k][0] = x;
		dfs(k, x);
		siz[x] += siz[k], s[x] += s[k];
	}
}
int main() {
	read(n), read(m), read(q);
	for (int i = 1; i <= n; i++) read(h[i]), s[i] = 1;
	for (int i = 1; i <= m; i++)
		read(a[i].x), read(a[i].y), read(a[i].v);
	sort(a + 1, a + m + 1); int tot = n;
	for (int i = 1; i <= 2 * n; i++) p[i] = i;
	for (int i = 1; i <= m; i++) {
		int tx = Find(a[i].x), ty = Find(a[i].y), tp = a[i].v;
		if (tx != ty) {
			p[tx] = p[ty] = ++tot; v[tot] = tp;
			f[tx][0] = f[ty][0] = tot;
			E[tot].push_back(tx), E[tot].push_back(ty);
		}
	}
	dfs(tot, 0); int ans = 0;
	while (q--) {
		int tv, x, k;
		read(x), read(tv), read(k);
		if (ans != -1) tv ^= ans, x ^= ans, k ^= ans;
		for (int i = 20; i >= 0; i--)
			if (f[x][i] && v[f[x][i]] <= tv) x = f[x][i];
		ans = T.query(rt[dfn[x] - 1], rt[dfn[x] + siz[x] - 1], 1, 1e9, s[x] - k + 1);
		cout << ans << "\n";
	}
	return 0;
}

posted @ 2018-08-14 22:24  谜のNOIP  阅读(86)  评论(0编辑  收藏  举报