BZOJ4999: This Problem Is Too Simple!树链剖分+动态开点

Description
给您一颗树,每个节点有个初始值。
现在支持以下两种操作:
C i x(0<=x<2^31) 表示将i节点的值改为x。
Q i j x(0<=x<2^31) 表示询问i节点到j节点的路径上有多少个值为x的节点。


Sample Input
5 6
10 20 30 40 50
1 2
1 3
3 4
3 5
Q 2 3 40
C 1 40
Q 2 3 40
Q 4 5 30
C 3 10
Q 4 5 30


Sample Output
0
1
1
0


这题动态开点+树链剖分直接上就可以了。。。
没什么好说的


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
int _min(int x, int y) {return x < y ? x : y;}
int _max(int x, int y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct edge {
	int x, y, next;
} e[210000]; int len, last[110000];
struct tnode {
	int lc, rc, c;
} t[310000 * 20]; int cnt, rt[310000];
struct query {
	int opt, x, y, c;
} q[210000];
int n, tp, a[110000], b[310000];
int fa[110000], son[110000], tot[110000], dep[110000];
int id, ys[110000], top[110000];
char ss[3];

int Pos(int x) {
	int l = 1, r = tp, ans = 0;
	while(l <= r) {
		int mid = (l + r) / 2;
		if(b[mid] <= x) l = mid + 1, ans = mid;
		else r = mid - 1;
	} if(b[ans] != x) return -1;
	return ans;
}

void ins(int x, int y) {
	e[++len].x = x; e[len].y = y;
	e[len].next = last[x]; last[x] = len;
}

void pre_tree_node(int x) {
	tot[x] = 1; son[x] = 0;
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(y != fa[x]) {
			fa[y] = x;
			dep[y] = dep[x] + 1;
			pre_tree_node(y);
			tot[x] += tot[y];
			if(tot[y] > tot[son[x]]) son[x] = y;
		}
	}
}

void pre_tree_edge(int x, int tp) {
	ys[x] = ++id; top[x] = tp;
	if(son[x]) pre_tree_edge(son[x], tp);
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(y != fa[x] && y != son[x]) pre_tree_edge(y, y);
	}
}

void Link(int &u, int l, int r, int p, int c) {
	if(!u) u = ++cnt;
	t[u].c += c;
	if(l == r) return ;
	int mid = (l + r) / 2;
	if(p <= mid) Link(t[u].lc, l, mid, p, c);
	else Link(t[u].rc, mid + 1, r, p, c);
}

int query(int u, int l, int r, int ll, int rr) {
	if(l == ll && r == rr) return t[u].c;
	int mid = (l + r) / 2;
	if(rr <= mid) return query(t[u].lc, l, mid, ll, rr);
	else if(ll > mid) return query(t[u].rc, mid + 1, r, ll, rr);
	else return query(t[u].lc, l, mid, ll, mid) + query(t[u].rc, mid + 1, r, mid + 1, rr);
}

int solve(int x, int y, int c) {
	int tx = top[x], ty = top[y], ans = 0;
	while(tx != ty) {
		if(dep[tx] > dep[ty]) swap(tx, ty), swap(x, y);
		ans += query(rt[c], 1, n, ys[ty], ys[y]);
		y = fa[ty]; ty = top[y];
	} if(x == y) return ans + query(rt[c], 1, n, ys[x], ys[x]);
	else {
		if(dep[x] > dep[y]) swap(x, y);
		return ans + query(rt[c], 1, n, ys[x], ys[y]);
	}
}

int main() {
	n = read(); int Q = read();
	for(int i = 1; i <= n; i++) a[i] = read(), b[i] = a[i];
	for(int i = 1; i < n; i++) {
		int x = read(), y = read();
		ins(x, y), ins(y, x);
	} tp = n;
	for(int i = 1; i <= Q; i++) {
		scanf("%s", ss + 1);
		if(ss[1] == 'C') q[i].opt = 1, q[i].x = read(), q[i].c = read(), b[++tp] = q[i].c;
		else {
			q[i].x = read(), q[i].y = read();
			q[i].c = read();
		}
	} sort(b + 1, b + tp + 1);
	pre_tree_node(1);
	pre_tree_edge(1, 1);
	for(int i = 1; i <= n; i++) {
		a[i] = Pos(a[i]);
		Link(rt[a[i]], 1, n, ys[i], 1);
	}
	for(int i = 1; i <= Q; i++) {
		if(q[i].opt == 1) {
			int c = Pos(q[i].c);
			Link(rt[a[q[i].x]], 1, n, ys[q[i].x], -1);
			a[q[i].x] = c;
			Link(rt[a[q[i].x]], 1, n, ys[q[i].x], 1);
		} else {
			int c = Pos(q[i].c);
			if(c == -1) puts("0");
			else printf("%d\n", solve(q[i].x, q[i].y, c));
		}
	}
	return 0;
}

发布了222 篇原创文章 · 获赞 63 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览