ZOJ 1610 Count the Colors——线段树

网友投稿 600 2022-11-29

ZOJ 1610 Count the Colors——线段树

ZOJ 1610 Count the Colors——线段树

题意:用不同颜色覆盖区间,问最后每种颜色有多少段

思路:线段树维护区间的颜色,线段树和平常的模板不同,只需要维护叶子节点就可以,其余节点放在那里不用考虑了,进而我们可以不用建线段树数组,只需要慵懒标记就可以得到最终结果。

注意线段维护的是区间的颜色,不是点的点的颜色,所以用输入的左界+1代表其区间

#include #include #include #include using namespace std;const int maxn = 1e4 + 10;int n, segTree[maxn<<2], lazy[maxn<<2];int last, ans[maxn];void pushdown(int root) { if (lazy[root] != -1) { lazy[root<<1] = lazy[root<<1|1] = lazy[root]; lazy[root] = -1; }}void update_interval(int L, int R, int root, int uL, int uR, int val) { if (uL <= L && R <= uR) { lazy[root] = val; return; } int mid = (L + R)>>1; pushdown(root); if (uL <= mid) { update_interval(L, mid, root<<1, uL, uR, val); } if (uR > mid) { update_interval(mid + 1, R, root<<1|1, uL, uR, val); }}void query(int L, int R, int root) { if (L == R) { if (lazy[root] != -1 && lazy[root] != last) { ans[lazy[root]]++; } last = lazy[root]; return; } int mid = (L + R)>>1; pushdown(root); query(L, mid, root<<1); query(mid + 1, R, root<<1|1);}int main(){ while (scanf("%d", &n) == 1) { memset(lazy, -1, sizeof(lazy)); memset(ans, 0, sizeof(ans)); while (n--) { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a <= b) update_interval(1, 8000, 1, a + 1, b, c); } last = -1; query(1, 8000, 1); for (int i = 0; i <= 8000; i++) { if (ans[i]) { printf("%d %d\n", i, ans[i]); } } printf("\n"); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CodeForces - 558E A Simple Task——线段树 + 计数排序
下一篇:@PathVariable获取路径中带有 / 斜杠的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~