线段树--2022年4月22日----1610----调了好久---完结撒花

线段树

合并区间问题

也可以当做光线照盒子,影子长度问题

在这里插入图片描述

给了一组大数据,一组小数据

在这里插入图片描述

思路是:
生成一个线段树,保存所有可能的节点线段,每次都是对半分线段。

在这里插入图片描述
线段树的结构,简易版
在这里插入图片描述

线段树的节点(顺序存储)

struct TreeNode {
	int ld, rd;
	int cover; // 标记颜色的种类,-1表示该节点以下都没有颜色不用再继续找了
	//-2表示被拆分过。需要再往下找
}Tree[Maxsize];

静态线段树不做真删除

一、建立线段树

题目会给条件
All the numbers are in the range [0, 8000], and they are all integers.
这个就是告诉初始化的范围是多少。
void buildtree(int i, int a, int b)
{
	Tree[i].ld = a;
	Tree[i].rd = b;
	if (b - a == 1) return;
	buildtree(i * 2, a, (a + b) / 2);
	buildtree(i * 2 + 1, (a + b) / 2, b); 
	return;
}
插入线段,进行着色。
初级版着色方式,只有0,1选择。

在这里插入图片描述

作业变形升级版本

zoj 1610 Count the Colors

Time Limit: 2000 msMemory Limit: 65536 KB

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.

Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

x1 x2 c

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.

Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can’t be seen, you shouldn’t print it.

Print a blank line after every dataset.

Sample Input

5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1

Sample Output

1 1
2 1
3 1

1 1

0 2
1 1

题目大意:给一个分成n段的木板涂色,后序涂色可能会覆盖之前的涂色,问最终露出来的颜色和他们的段数
输入:(可以有很多case)
n(1 <= n <= 8000)
第i次涂色的左端点 右端点 所涂的颜色序号(共n次涂色各占一行,所有数据的范围都是 [0, 8000])
输出:(每个case的输出中间有一个空行)
最终漏出的颜色序号 涂了它的段数(按照颜色序号从小到大输出)
分析:线段树涂色问题。与poj2528类似,但是2528是点着色(一个数已经代表一小段了),这个是段着色(一个数就是一个端点)。要注意一点,不能用n来建树,而是用8000,也就是说其实树是固定的。
更新时区间左值加1则与2528类似可以看成点来染色了,例如0 4 4就相当于染色1,2,3,4这四个段所以update(1,4,4)。update和down的部分都一样,与2528不同的是这个代码中的query将区间都压到了叶节点再进行统计,但是像2528那样做也可以(遇到单色区间就统计)。

用点线段树做

#include<iostream>
#include<string.h>
using namespace std;
const int  maxn = 8005;
struct tree
{
	int l, r, color;
}tree[maxn << 2], TREE[maxn << 2];

int n, num;
int ans[maxn];

void pushdown(int rt)
{
	if (tree[rt].color > -1)
	{
		tree[rt << 1].color = tree[rt].color;
		tree[rt << 1 | 1].color = tree[rt].color;
		tree[rt].color = -1;
	}
}

void build(int rt, int l, int r)
{
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].color = -1;
	if (l == r)return;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	build(rt*2,l,mid);
	build(rt*2+1,mid+1,r);
}

void update(int rt, int l, int r, int color)
{
	if (l <= tree[rt].l && tree[rt].r <= r)
	{
		tree[rt].color = color;
		return;
	}
	pushdown(rt);
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (mid >= r) update(rt << 1, l, r, color);
	else if (mid < l) update(rt << 1 | 1, l, r, color);
	else {
		update(rt * 2, l, mid, color);
		update(rt * 2 + 1, mid + 1, r, color);
	}
}

void Count(int rt, int l, int r)
{
	if (l == tree[rt].l && tree[rt].r == r && tree[rt].color > -1)
	{
		TREE[num].l = l;
		TREE[num].r = r;
		TREE[num++].color = tree[rt].color;
		return;
	}
	if (l == r) return;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (mid >= r) Count(rt << 1, l, r);
	else if (mid < l) Count(rt << 1 | 1, l, r);
	else {
		Count(rt * 2, l, mid);
		Count(rt * 2 + 1, mid + 1,r);
	}
}

int main()
{
	int i=0, j=0;
	int l=0, r=0, color=0;
	while (cin>>n)
	{
		num = 0;
		build(1, 1, maxn);  //注意是maxn,不是n!!!
		for (i = 0; i < n; i++)
		{
			cin>>l>>r>>color;
			update(1, l + 1, r, color);
		}
		Count(1, 1, maxn);  //注意是maxn,不是n!!!
		memset(ans, 0,sizeof(ans));
		ans[TREE[0].color]++;
		for (i = 1; i < num; i++)
		{
			//前后两个线段颜色不一样或者不相邻
			if (TREE[i].color != TREE[i - 1].color || TREE[i].l - 1 > TREE[i - 1].r)
				ans[TREE[i].color]++;
		}
		for(i=0; i< maxn;i++)
		{
			if (ans[i])
				cout<<i<<" "<<ans[i]<<endl;
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

用区间树

正确做法


//1610
#include <iostream>
#include <string.h>
using namespace std;
#define MAX 8005
struct Node
{
    int l, r;
    int c;
};
Node tree[MAX * 4];
int ans[MAX];
int flag = -1;
void Initial(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].c = -1;
    if (r - l == 1)
        return;
    int mid = (tree[i].l + tree[i].r) >> 1;
    Initial(i * 2, l, mid);
    Initial(i * 2 + 1, mid, r);
}
//下放,到左、右子树
void add(int i, int l, int r, int c)//c 表示颜色
{
    if (c == tree[i].c||l==r)  //如果已经有相同的颜色可以不用标了
    {
        return;
    }
    if (l <= tree[i].l && r >= tree[i].r )
    {
        tree[i].c = c;
        return;
    }
   
    
        int mid = (tree[i].l + tree[i].r) >> 1;
        if (tree[i].c == -1 || tree[i].c >= 0)
        {
            tree[i * 2].c = tree[i].c;                //把该节点的颜色下放
            tree[i * 2 + 1].c = tree[i].c;            //把该节点的颜色下放
            tree[i].c = -2;
        }
        if (l >= mid)  //去右边
        {
            add(i * 2 + 1, l, r, c);
        }
        else
            if (r <= mid)   //去左边
            {
                add(i * 2, l, r, c);
            }
            else    //两边都去
            {
                add(i * 2, l, mid, c);
                add(i * 2 + 1, mid, r, c);
            }
    
}

void counts(int i, int l, int r)
{ 
    int mid = (tree[i].l + tree[i].r) >> 1;
    if (tree[i].c >= 0)
    {
        if (tree[i].c != flag && tree[i].l != tree[i].r)
        {
            flag = tree[i].c;
            // cout << tree[i].l << " " << tree[i].r <<" "<< tree[i].c<<endl;
            ans[tree[i].c]++;
        }
        return ;
    }
    if (tree[i].l+1!=tree[i].r)
    {
        counts(i * 2, l, mid);
        counts(i * 2 + 1, mid, r);
    }
else 
    flag=-1;
}

int main() {
    int  n = 0, x = 0, y = 0, c = 0;
    while (cin >> n)
    {
        flag = -1;
        memset(ans, 0, sizeof(ans));
        Initial(1, 0, MAX);
        while (n--)
        {
            cin >> x >> y >> c;
            add(1, x, y, c);
        }
        counts(1, 0, MAX);
        for (int i = 0; i < MAX; i++)
        {
            if (ans[i] != 0)
                cout << i << " " << ans[i] << endl;
        }
        cout << endl;
    }
    return 0;
}

/*
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1

*/

错误代码

问题一: 所有点要赋值为-1

问题二: 下放颜色的逻辑。

条件是不等于-2的时候,才能下放颜色。

  if (tree[i].c == -1 || tree[i].c >= 0)
        {
            tree[i * 2].c = tree[i].c;                //把该节点的颜色下放
            tree[i * 2 + 1].c = tree[i].c;            //把该节点的颜色下放
            tree[i].c = -2;
        }

问题三、统计的返回逻辑

flag要在无颜色的时候,返回赋值为-1.不然很可能出现
两段红的中间空了一个无颜色的,还被看成一整段
在这里插入图片描述

void counts(int i, int l, int r)
{
    int mid = (tree[i].l + tree[i].r) >> 1;
    if (tree[i].c >= 0)
    {
        if (tree[i].c != flag && tree[i].l != tree[i].r)
        {
            flag = tree[i].c;
            // cout << tree[i].l << " " << tree[i].r <<" "<< tree[i].c<<endl;
            ans[tree[i].c]++;
        }
        return;
    }
    if (tree[i].l + 1 != tree[i].r)
    {
        counts(i * 2, l, mid);
        counts(i * 2 + 1, mid, r);
    }
    else
        flag = -1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值