bzoj 1146 [CTSC2008]网络管理Network 树状数组套线段树

题面

题目传送门

解法

可以考虑一种毒瘤方法:二分答案+树剖+BIT套线段树

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

这个写起来麻烦,而且非常丑陋

发现每一次修改只会对子树中的所有点产生影响,而子树的dfs序是连续的

所以考虑单点修改,区间询问,发现可以使用BIT

询问时记录一下所有会经过的点,然后再线段树上二分即可

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

代码

#include <bits/stdc++.h>
#define N 80010
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 Edge {
	int next, num;
} e[N * 4];
struct Node {
	int lc, rc, cnt;
} t[N * 210];
int n, q, tot, cnt, l1, l2, Time, rt[N], siz[N], dfn[N], d[N], a[N], tx[N], ty[N], f[N][21];
int lowbit(int x) {return x & -x;}
int s(int x) {return t[x].cnt;}
void add(int x, int y) {
	e[++cnt] = (Edge) {e[x].next, y};
	e[x].next = cnt;
}
void dfs(int x, int fa) {
	dfn[x] = ++Time, siz[x] = 1, d[x] = d[fa] + 1;
	for (int i = 1; i <= 20; i++)
		f[x][i] = f[f[x][i - 1]][i - 1];
	for (int p = e[x].next; p; p = e[p].next) {
		int k = e[p].num;
		if (k == fa) continue; f[k][0] = x;
		dfs(k, x); siz[x] += siz[k];
	}
}
int lca(int x, int y) {
	if (d[x] < d[y]) swap(x, y);
	for (int i = 20; i >= 0; i--)
		if (d[f[x][i]] >= d[y]) x = f[x][i];
	if (x == y) return x;
	for (int i = 20; i >= 0; i--)
		if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
	return f[x][0];
}
void ins(int &k, int l, int r, int x, int val) {
	if (!k) k = ++tot; t[k].cnt += val;
	if (l == r) return;
	int mid = (l + r) >> 1;
	if (x <= mid) ins(t[k].lc, l, mid, x, val);
		else ins(t[k].rc, mid + 1, r, x, val);
}
void Add(int pos, int x, int val) {
	for (int i = pos; i <= n; i += lowbit(i))
		ins(rt[i], 1, 1e8, x, val);
}
int query(int l, int r, int k) {
	if (l == r) return l;
	int mid = (l + r) >> 1, sum = 0;
	for (int i = 1; i <= l1; i++) sum += s(t[tx[i]].lc);
	for (int i = 1; i <= l2; i++) sum -= s(t[ty[i]].lc);
	if (k <= sum) {
		for (int i = 1; i <= l1; i++) tx[i] = t[tx[i]].lc;
		for (int i = 1; i <= l2; i++) ty[i] = t[ty[i]].lc;
		return query(l, mid, k);
	}
	for (int i = 1; i <= l1; i++) tx[i] = t[tx[i]].rc;
	for (int i = 1; i <= l2; i++) ty[i] = t[ty[i]].rc;
	return query(mid + 1, r, k - sum);
}
int main() {
	read(n), read(q); cnt = n;
	for (int i = 1; i <= n; i++) read(a[i]);
	for (int i = 1; i < n; i++) {
		int x, y; read(x), read(y);
		add(x, y), add(y, x);
	}
	dfs(1, 0);
	for (int i = 1; i <= n; i++)
		Add(dfn[i], a[i], 1), Add(dfn[i] + siz[i], a[i], -1);
	while (q--) {
		int k, x, y;
		read(k), read(x), read(y);
		if (k == 0) {
			Add(dfn[x], a[x], -1), Add(dfn[x] + siz[x], a[x], 1);
			Add(dfn[x], y, 1), Add(dfn[x] + siz[x], y, -1); a[x] = y;
		} else {
			int xx = lca(x, y), yy = f[xx][0]; l1 = l2 = 0;
			int s = d[x] + d[y] - d[xx] - d[yy];
			if (k > s) {cout << "invalid request!\n"; continue;}
			for (int i = dfn[x]; i; i -= lowbit(i)) tx[++l1] = rt[i];
			for (int i = dfn[y]; i; i -= lowbit(i)) tx[++l1] = rt[i];
			for (int i = dfn[xx]; i; i -= lowbit(i)) ty[++l2] = rt[i];
			for (int i = dfn[yy]; i; i -= lowbit(i)) ty[++l2] = rt[i];
			cout << query(1, 1e8, s - k + 1) << "\n";
		}
	}
	return 0;
}
posted @ 2018-08-14 23:22  谜のNOIP  阅读(82)  评论(0编辑  收藏  举报