点线段树--2022-4-29

点线段树

在这里插入图片描述

建立

//线段树的建立,是一段区间,不是点
void build(int rt, int l, int r)
{
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].color = 0;
	if (r==l)return;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	build(rt*2,l,mid);                                                                
	build(rt*2+1,mid,r);       //中间的数字,分别表示左边的终止位置,表示右边的起始位置
	return; 
}

插入新的颜色

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 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);
	}
}

线段树

在这里插入图片描述

插入

在这里插入图片描述
在这里插入图片描述

约瑟夫问题

用线段树来解决,就是加一个key值,保存该段含有的数字个数。
在这里插入图片描述

PTA

zoj 2451

Minimizing maximizer

Time Limit: 5000 msMemory Limit: 32768 KB

The company Chris Ltd. is preparing a new sorting hardware called Maximizer. Maximizer has N inputs numbered from 1 to N. Each input represents one integer. Maximizer has one output which represents the maximum value present on Maximizer’s inputs.

Maximizer is implemented as a pipeline of sorters Sorter(i[1], j[1]), …, Sorter(i[k], j[k]). Each sorter has N inputs and N outputs. Sorter(i, j) sorts values on inputs i, i+1, …, j in non-decreasing order and lets the other inputs pass through unchanged. The N-th output of the last sorter is the output of the Maximizer.

An intern (a former ACM contestant) observed that some sorters could be excluded from the pipeline and Maximizer would still produce the correct result. What is the length of the shortest subsequence of the given sequence of sorters in the pipeline still producing correct results for all possible combinations of input values?

Task

Write a program that:

reads the description of a Maximizer, i.e. the initial sequence of sorters in the pipeline,
computes the length of the shortest subsequence of the initial sequence of sorters still producing correct results for all possible input data,
writes the result. 

Input Specification

The input consists of several test cases.
For each case, the first line contains two integers N and M (2 <= N <= 50 000, 1 <= M <= 500 000) separated by a single space. Integer N is the number of inputs and integer M is the number of sorters in the pipeline. The initial sequence of sorters is described in the next M lines. The k-th of these lines contains the parameters of the k-th sorter: two integers i[k] and j[k] (1 <= i[k] < j[k] <= N) separated by a single space.
Output Specification

For each test case, print in one line the length of the shortest subsequence of the initial sequence of sorters still producing correct results for all possible data.

题目要求:最短需要多少个子序列可以覆盖整个区间。

最少需要多少个区间能完全覆盖整个区间[1,n]

可以理解为接水管的问题,可以浪费水管。

在这里插入图片描述

找到能够包含N的范围的sorter器,把所有数据都排好

给你m个机器,n个数,每个机器可以给n个数的某一段排序。求最少使用几个机器,保证可以把这个n个数排好序。

在这里插入图片描述
在这里插入图片描述

思路
1.建立一个点线段树,每一个点的值都设置为无穷大。我们要找的是最小的值,这样在更新的时候就会把无穷大修改为更小的值。

在这里插入图片描述

点线段树的建立

//点线段树的建立
void build(int rt, int l, int r)
{
	tree[rt] = inf; //初始化为无穷大
	if (l == r)return;
	int mid = (l + r) >> 1;
	build(rt << 2, l, mid);
	build(rt >>2 | 1, mid + 1, r);
}
每次插入新结点都需要更新数量
#include<iostream>
#include<string.h>
using namespace std;
const int  maxn = 200521;
const int inf = 0x3f3f3f3f;
int tree[maxn];


//点线段树的建立
void build(int rt, int l, int r)
{
	tree[rt] = inf; //初始化为无穷大
	if (l == r)return;
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt <<1 | 1, mid + 1, r);
}

void update(int i,int pos, int val, int left, int right)
{
	if (tree[i] > val) tree[i] = val;
	if (left == right) return; //点线段树的状态

	int mid = (left + right) >> 1;//向下维护
	if (pos <= mid) 
		update(i << 1, pos, val, left, mid);
	else update(i << 1 | 1, pos, val, mid + 1, right);
}

//查询新输入的x~y区间中可以连接新sorter的最优(小)值并返回 
int Inquiry(int i, int x, int y, int left, int right) {  //x~y是新的sorter区间,left~right是线段树的跨度区间 
	if (x <= left && y >= right) 
		return tree[i];  //若x~y覆盖left~right区间,则返回tree[i]

	int ans1 = inf, ans2 = inf;

	int mid = (left + right) >> 1;
	if (x <= mid)  ans1 = Inquiry(i << 1, x, y, left, mid);
	if (y > mid)   ans2 = Inquiry(i << 1 | 1, x, y, mid + 1, right);
	return ans1 < ans2 ? ans1 : ans2;
}



int main()
{
	int n, m;
	while (cin >> n>>m)
	{
		build(1, 1, n);
		update(1, 1, 0, 1, n);
		while (m--)
		{
			int x, y;
			cin >> x >> y;
			int temp = Inquiry(1, x, y, 1, n);
			update(1, y, ++temp, 1, n);
		}
		cout << Inquiry(1, n, n, 1, n) << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值