Solve this interesting problem(线段树逆二分模拟的DFS递归操作)

本文深入探讨如何解决涉及线段树的复杂问题,通过详细解释线段树的基本概念及其构建过程,指导读者掌握逆向操作模拟技巧,最终提供解决实际问题的方法。包括输入输出规范及AC代码演示。


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5323


Solve this interesting problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1696    Accepted Submission(s): 503


Problem Description
Have you learned something about segment tree? If not, don’t worry, I will explain it for you.
Segment Tree is a kind of binary tree, it can be defined as this:
- For each node u in Segment Tree, u has two values:  Lu  and  Ru .
- If  Lu=Ru , u is a leaf node. 
- If  LuRu , u has two children x and y,with  Lx=Lu , Rx=Lu+Ru2 , Ly=Lu+Ru2+1 , Ry=Ru .
Here is an example of segment tree to do range query of sum.



Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node's value  Lroot=0  and  Rroot=n  contains a node u with  Lu=L  and  Ru=R .
 

Input
The input consists of several test cases. 
Each test case contains two integers L and R, as described above.
0LR109
LRL+12015
 

Output
For each test, output one line contains one integer. If there is no such n, just output -1.
 

Sample Input
  
6 7 10 13 10 11
 

Sample Output
  
7 -1 12
 

Source
 


分析:要做这题,首先需要知道线段树的构建过程,然后只要对构建线段树的逆向操作进行模拟即可,DFS递归时注意左右区间边界条件。


AC code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
#define LL long long
#define MAXN 1000010
using namespace std;
const LL INF=0x3f3f3f3f;
LL L,R,ans;
void dfs(LL l,LL r)
{
	if(l<=0||r>=2*R)
	{
		if(l==0)
		{
			ans=min(ans,r);
		}
		return;
	}
	int len=r-l+1;
	if(len>l)//jianzhi
		return;
	dfs(l-len,r);
	dfs(l-len-1,r);
	dfs(l,r+len);
	if(len>1)
	dfs(l,r+len-1);
}
int main()
{
	while(scanf("%I64d%I64d",&L,&R)!=EOF)
	{
		ans=INF;
		dfs(L,R);
		if(ans==INF)
			printf("-1\n");
		else
			printf("%I64d\n",ans);
	}
	return 0;
}


### Python 中的深度优先搜索 (DFS) 实现 对于树或图结构中的遍历,深度优先搜索是一种常用的算法。下面是一个基于邻接表表示法的无向图上的简单 DFS 的实现: ```python def dfs(graph, start): visited, stack = set(), [start] while stack: vertex = stack.pop() if vertex not in visited: visited.add(vertex) stack.extend(set(graph[vertex]) - visited) return visited ``` 此函数接受两个参数:`graph` 是字典形式的图数据结构,其中键是节点而值是由相邻节点组成的列表;`start` 表示起始顶点[^1]。 为了更深入理解如何应用 DFS 解决实际问题,可以考虑迷宫求解的例子,在这种情况下,DFS 可用于寻找从起点到终点的一条路径: ```python def solve_maze(maze, start, end): rows, cols = len(maze), len(maze[0]) def valid(r, c): return 0 <= r < rows and 0 <= c < cols and maze[r][c] == ' ' path = [] directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] def helper(current_position): row, col = current_position if not valid(row, col): return False if current_position == end: path.append((row, col)) return True maze[row][col] = '#' # mark as seen to avoid cycles. for dr, dc in directions: next_row, next_col = row + dr, col + dc if helper((next_row, next_col)): path.append((row, col)) return True maze[row][col] = ' ' return False helper(start) return list(reversed(path)) if path else None ``` 这段代码定义了一个 `solve_maze()` 函数来尝试找到通过给定迷宫的一个解决方案,并返回所经过的位置序列作为结果。如果找不到任何有效的路径,则会返回 `None`[^2]。 ### Python 中的二分查找实现 当处理已排序数组时,二分查找提供了一种高效的检索方法。这里有一个简单的例子展示了如何在一个有序整数列表中执行二分查找操作: ```python def binary_search(sorted_list, target): low, high = 0, len(sorted_list)-1 while low <= high: mid = (low + high) >> 1 guess = sorted_list[mid] if guess < target: low = mid + 1 elif guess > target: high = mid - 1 else: return mid # key found at index mid return -(low + 1) # key not found ``` 上述程序实现了类似于 Java 版本的功能,但是采用了更加简洁的方式编写,并且适用于 Python 列表类型的输入。请注意,该版本同样遵循了相同的逻辑流程并保持了相似的时间复杂度 O(log n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值