左神算法学习日记——搜索二叉树使用方法

class edge
{
public:
	int positon;
	int height;
	bool ifup;
	edge() = default;
	edge(int p, int h, int dir)
	{
		positon = p;
		height = h;
		ifup = dir;
	}
};

struct a
{
	bool operator()(edge* l, edge* r)
	{
		return l->positon < r->positon;
	}
};

vector<vector<int>> getprofile(vector<vector<int>> build)
{
	vector<vector<int>> res;
	if (build.empty())
		return res;
	vector<edge*> arr(build.size() * 2);
	int ind = 0;
	for (auto var : build)
	{
		arr[ind++] = new edge(var[0], var[2], 1);
		arr[ind++] = new edge(var[1], var[2], 0);
	}
	sort(arr.begin(), arr.end(), a());
	map<int, int> heimap;
	map<int, int> promap;
	for (int i = 0; i < arr.size(); i++)
	{
		if (arr[i]->ifup)
			heimap[arr[i]->height]++;
		else if (heimap[arr[i]->height] == 1)
			heimap.erase(arr[i]->height);
		else
			heimap[arr[i]->height]--;
		if (heimap.empty())
			promap[arr[i]->positon] = 0;
		else
			promap[arr[i]->positon] = (--heimap.end())->first;
	}
	int start = 0;
	int height = 0;
	for (auto var : promap)
	{
		if (var.second != height)
		{
			if (height != 0)
			{
				res.push_back({ start, var.first, height });
			}
			start = var.first;
			height = var.second;
		}
	}
	return res;
}

int maxlen(vector<int> arr,int aim)
{
	int sum = 0;
	int res = 0;
	hash_map<int, int> presum;
	presum[0] = -1;
	for (int i = 0; i < arr.size(); i++)
	{
		sum += arr[i];
		int p=0;
		if (presum.find(sum - aim) != presum.end())
		{
			p = presum[sum - aim];
		}
		if (presum.find(sum)==presum.end())
		{
			presum.insert(make_pair(sum,i));
		}
		res = max(res, i - p);
	}
	return res;
}

int maxdif(vector<int> arr)
{
	vector<int> dp(arr.size(), 0);
	hash_map<int, int> search;
	search[0] = -1;
	int xor = 0;
	for (int i = 0; i < arr.size(); i++)
	{
		xor ^= arr[i];
		if (search.find(xor) != search.end())
		{
			dp[i] = search[xor] == -1 ? 1 : dp[search[xor]]+1;
		}
		if (i>0)
			dp[i] = max(dp[i - 1], dp[i]);
		search[xor] = i;
	}
	return dp[arr.size() - 1];
}

猜你喜欢

转载自blog.csdn.net/w275412237/article/details/89813123