前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >牛客国庆集训派对Day6 I.清明梦超能力者黄YY(树剖)「建议收藏」

牛客国庆集训派对Day6 I.清明梦超能力者黄YY(树剖)「建议收藏」

作者头像
全栈程序员站长
发布2022-09-14 09:48:00
1580
发布2022-09-14 09:48:00
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

题目:https://www.nowcoder.com/acm/contest/206/I 正难则反。 问你倒数第k次的颜色,正着来搞不定,那就转换成“倒着来的第k次”。 使用树剖将这棵树丢进线段树里,不维护染色,而是维护更新的次数(因为除了倒数第k次的颜色,其他的根本没用啊!!!),然后把区间最小值pushUp到树顶。 更新完染色次数之后,用树顶来判整个区间里是否存在已经被更新了k次的节点,如果存在,就一步步往下走去找到那个节点。由于节点不止一个,进树之后两边都要检查能否往下走。 这和我之前做的:南京网络赛里的一题是同一种操作,可以点进去查看。

赠送祖传样例:

代码语言:javascript
复制
9 5 2
1 2
2 3
1 4
4 5
4 6
6 8
6 7
8 9
1 6 3
4 9 1
5 7 4
3 6 2
2 8 3

答案:2 2 0 2 0 2 0 1 0

ac代码:

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 100005;
const int inf = 1e9 + 7;

vector<int> G[maxn];
int n, m, k;

int sz[maxn], dep[maxn];
int ch[maxn], fa[maxn];
int top[maxn], tid[maxn], tid2[maxn];
int ans[maxn];
int tot;

void dfs1(int u, int f, int d) { 
   
	sz[u] = 1;
	fa[u] = f;
	dep[u] = d;
	for(int i = 0; i < G[u].size(); i++) { 
   
		int v = G[u][i];
		if(v == f) { 
   
			continue;
		}
		dfs1(v, u, d + 1);
		sz[u] += sz[v];
		if(ch[u] == -1 || sz[v] > sz[ch[u]]) { 
   
			ch[u] = v;
		}
	}
}

void dfs2(int u, int tp) { 
   
	top[u] = tp;
	tid[u] = ++tot;
	tid2[tot] = u;
	if(ch[u] == -1) { 
   
		return;
	}
	dfs2(ch[u], tp);
	for(int i = 0; i < G[u].size(); i++) { 
   
		int v = G[u][i];
		if(v != ch[u] && v != fa[u]) { 
   
			dfs2(v, v);
		}
	}
}

struct SegmentTree { 
   
	int t[maxn << 2], lazy[maxn << 2];

	void pushUp(int i) { 
   
		t[i] = min(t[i << 1], t[i << 1 | 1]);
	}

	void pushDown(int i) { 
   
		if(!lazy[i]) { 
   
			return;
		}
		t[i << 1] -= lazy[i];
		t[i << 1 | 1] -= lazy[i];
		lazy[i << 1] += lazy[i];
		lazy[i << 1 | 1] += lazy[i];
		lazy[i] = 0;
	}

	void build(int i, int l, int r) { 
   
		lazy[i] = 0;
		if(l == r) { 
   
			t[i] = k;
			return;
		}
		int mid = (l + r) >> 1;
		build(i << 1, l, mid);
		build(i << 1 | 1, mid + 1, r);
		pushUp(i);
	}

	void update(int i, int l, int r, int L, int R) { 
   
		if(l >= L && r <= R) { 
   
			t[i] -= 1;
			lazy[i] += 1;
			return;
		}
		pushDown(i);
		int mid = (l + r) >> 1;
		if(L <= mid) { 
   
			update(i << 1, l, mid, L, R);
		}
		if(R > mid) { 
   
			update(i << 1 | 1, mid + 1, r, L, R);
		}
		pushUp(i);
	}

	void update_cor(int i, int l, int r, int cor) { 
   
		if(l == r && !t[i]) { 
   
			t[i] = inf;
			ans[tid2[l]] = cor;
			return;
		}
		pushDown(i);
		int mid = (l + r) >> 1;
		if(!t[i << 1]) { 
   
			update_cor(i << 1, l, mid, cor);
		}
		if(!t[i << 1 | 1]) { 
   
			update_cor(i << 1 | 1, mid + 1, r, cor);
		}
		pushUp(i);
	}

	void update(int u, int v, int c) { 
   
		int f1 = top[u], f2 = top[v];
		while(f1 != f2) { 
   
			if(dep[f1] < dep[f2]) { 
   
				swap(f1, f2);
				swap(u, v);
			}
			update(1, 1, n, tid[f1], tid[u]);
			u = fa[f1];
			f1 = top[u];
		}
		if(dep[u] < dep[v]) { 
   
			swap(u, v);
		}
		update(1, 1, n, tid[v], tid[u]);
		if(!t[1]) { 
   
			update_cor(1, 1, n, c);
		}
	}

} st;

struct Query { 
   
	int u, v, c;
} q[maxn];

int main() { 
   
	scanf("%d%d%d", &n, &m, &k);

	tot = 0;
	memset(ch, -1, sizeof(ch));

	int u, v;
	for(int i = 1; i <= n - 1; i++) { 
   
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}

	dfs1(1, 0, 0);
	dfs2(1, 1);
	st.build(1, 1, n);

	for(int i = 1; i <= m; i++) { 
   
		scanf("%d%d%d", &q[i].u, &q[i].v, &q[i].c);
	}

	for(int i = m; i >= 1; i--) { 
   
		st.update(q[i].u, q[i].v, q[i].c);
	}

	for(int i = 1; i <= n; i++) { 
   
		printf("%d", ans[i]);
		if(i < n) { 
   
			printf(" ");
		}
	}
	printf("\n");
	return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159270.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档