NOJ1042 线段树区间最值

中文题啊,由于一次比赛线段树查不出错误,外加自己博客没有留几篇,现在重新刷一遍线段树,无论简单难的都留下,以后偶尔看看,先由简单的开始回顾起来!

这题不难,很裸的寻找区间最值,连更新都没有,就是建树然后寻找最值


题目:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1042



#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>

#define ll long long

#define LL __int64

#define eps 1e-8

#define inf 0xfffffff

//const LL INF = 1LL<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;


const int N = 10000 + 5;

int num[N];

int n;

typedef struct Node {
	int a;
};

Node tree[N * 4];

void init() {
	memset(tree,0,sizeof(tree));
	memset(num,0,sizeof(num));
}

void cal(int id) {
	tree[id].a = min(tree[id<<1].a,tree[id<<1|1].a);
}

void build(int l,int r,int id) {
	if(l == r) {
		tree[id].a = num[l];return;
	}
	int mid = (l + r)/2;
	build(l,mid,id<<1);
	build(mid+1,r,id<<1|1);
	cal(id);
}

int query(int L,int R,int l,int r,int id) {
	if(L <= l && R >= r)return tree[id].a;
	int mid = (l + r)/2;
	int ans = inf;
	if(L <= mid)ans = min(ans,query(L,R,l,mid,id<<1));
	if(R > mid) ans = min(ans,query(L,R,mid+1,r,id<<1|1));
	return ans;
}

int main() {
	while(scanf("%d",&n) == 1) {
		init();
		for(int i=1;i<=n;i++)
			scanf("%d",&num[i]);
		build(1,n,1);
		int q;
		scanf("%d",&q);
		while(q--) {
			int x,y;
			scanf("%d %d",&x,&y);
			int ans = query(x,y,1,n,1);
			printf("%d\n",ans);
		}
	}
	return 0;
}


### NOJ 中与树相关的题目及解法 #### 树的概念及其重要性 树是一种重要的数据结构,在计算机科学中有广泛的应用。它由节点和边组成,具有层次化的特性。常见的树有二叉树、平衡二叉树(AVL)、红黑树以及堆等变体[^1]。 #### 常见的树操作 对于树的操作通常包括但不限于以下几个方面: - **构建树**:通过输入的数据建立一棵树。 - **遍历树**:前序、中序、后序以及层序遍历是基本的四种方式[^2]。 - **查找节点**:在给定条件下寻找特定节点。 - **更新或删除节点**:修改或者移除某些节点并保持树的整体性质不变。 下面是一些可能涉及这些操作的具体例子: --- #### 题目一:二叉搜索树 (Binary Search Tree) 的构造与查询 ##### 描述 给出一组整数序列,按照顺序依次插入到初始为空的一棵二叉搜索树中,并完成如下任务: 1. 构建该二叉搜索树; 2. 查询某个是否存在; 3. 输出按某种次序遍历的结果。 ##### 解决方案 可以采用递归方法实现二叉搜索树的插入功能。以下是 Python 实现的一个简单版本: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def insert_into_bst(root, val): if not root: return TreeNode(val) if val < root.val: root.left = insert_into_bst(root.left, val) elif val > root.val: root.right = insert_into_bst(root.right, val) return root def inorder_traversal(root): result = [] stack = [(root, False)] while stack: node, visited = stack.pop() if node: if visited: result.append(node.val) else: stack.append((node.right, False)) stack.append((node, True)) stack.append((node.left, False)) return result ``` 上述代码实现了二叉搜索树的插入函数 `insert_into_bst` 和基于栈的中序遍历算法 `inorder_traversal`[^3]。 --- #### 题目二:小生成树 (Minimum Spanning Tree) ##### 描述 在一个无向加权图中,求连接所有顶点且总权重小的子集边集合。 ##### 解决方案 常用的两种算法分别是 Kruscal 算法和 Prim 算法。这里展示 Kruscal 算法的一种 C++ 实现: ```cpp #include <bits/stdc++.h> using namespace std; struct Edge { int u, v, w; }; bool cmp(const Edge &a, const Edge &b) { return a.w < b.w; } int find_set(int parent[], int x) { if(parent[x] != x){ parent[x] = find_set(parent,parent[x]); } return parent[x]; } void union_set(int parent[], int rank[], int x, int y) { int fx = find_set(parent,x); int fy = find_set(parent,y); if(fx == fy) return ; if(rank[fx]>rank[fy]){ parent[fy] = fx; }else{ parent[fx] = fy; if(rank[fx]==rank[fy]) rank[fy]++; } } int kruskal(vector<Edge> edges,int n){ sort(edges.begin(),edges.end(),cmp); int res = 0,cnt = 0; int parent[n],rank_arr[n]; for(int i=0;i<n;i++){ parent[i] = i; rank_arr[i] = 0; } for(auto e : edges){ if(find_set(parent,e.u)!=find_set(parent,e.v)){ cnt++; res +=e.w; union_set(parent,rank_arr,e.u,e.v); if(cnt==n-1) break ; } } return res; } ``` 此程序利用了并查集的思想来解决连通性和环检测问题[^4]。 --- #### 题目三:近公共祖先 (Lowest Common Ancestor) ##### 描述 在一棵树上找到两个指定结点之间的低共同祖先。 ##### 解决方案 可以通过深度优先搜索的方式自底向上回溯得到 LCA 结果。下面是伪代码描述过程的一部分逻辑框架: ```pseudo function lca(node, p, q): if node is null or node equals either p or q then return node let left be the recursive call to this function on the left child of 'node' let right be the same but with respect to its right subtree instead if both are non-null it means one lies in each branch so our current position must indeed represent their lowest common ancestor hence we should output that fact immediately otherwise propagate whichever answer was found upwards accordingly. ``` 实际编码时需注意边界条件处理以及性能调优等问题[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值