POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值。
这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点,不断维护,最好每次询问维护一个询问区间的最大值和最小值,最后相减即可。其实就相当于,线段树找区间的最大值和最小值。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define INF 0x3f3f3f3f
using namespace std;
int minv=INF;
int maxv=-INF;
struct node{
int l,r;
int minv,maxv;
int mid()//中间值
{
return (l+r)/;
}
}tree[];
void buildtree(int root,int l,int r)
{
tree[root].l=l;//区间左边
tree[root].r=r;//区间右边
tree[root].minv=INF;//区间内部的最小值
tree[root].maxv=-INF;//区间内部的最大值
if(l!=r)//不是根节点
{
buildtree(root*+,l,(l+r)/);
buildtree(root*+,(l+r)/+,r);
}
}
void insert(int root,int i,int v){//单点修改
if (tree[root].l==tree[root].r)
{
tree[root].r=i;
tree[root].minv=tree[root].maxv=v;
return;
}
tree[root].minv=min(tree[root].minv,v);//不是根节点,那么当插入的数不断往下更新时,需要不断对树上的节点范围内的最大值和最小值进行更新
tree[root].maxv=max(tree[root].maxv,v);
if(i<=tree[root].mid())
insert(*root+,i,v);
else
insert(*root+,i,v);
}
void query(int root,int s,int e)
{
if(tree[root].minv>minv && tree[root].maxv<maxv)//如果我前面维护的最小值已经比这一段的值还要小了,最大值也比这一段还要大了,那么无需再往下查询
return ;
if(tree[root].l==s && tree[root].r==e)//维护最大值和最小值
{
minv=min(minv,tree[root].minv);
maxv=max(maxv,tree[root].maxv);
return;
}
if(e<=tree[root].mid())//如过给出询问的区间不能通过一分为二分开,仍在区间的左边
query(*root+,s,e);//仍然继续递归这个区间
else if (s>tree[root].mid())
query(*root+,s,e);//反之也继续递归这个区间
else//需要把区间分开
{
query(*root+,s,tree[root].mid());//对半分
query(*root+,tree[root].mid()+,e);
}
}
int main(){
int n,q,h;
scanf("%d%d",&n,&q);
buildtree(,,n);
for (int i=;i<=n;i++){
scanf("%d",&h);
insert(,i,h);
}
for (int i=;i<q;i++)
{
int s,e;
scanf("%d%d",&s,&e);
minv=INF;
maxv=-INF;
query(,s,e);
printf("%d\n",maxv-minv);
}
return ;
}
POJ - 3264 线段树模板题 询问区间最大最小值的更多相关文章
- 洛谷3372线段树模板题 对区间+k或者查询区间和
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- poj 3468 线段树模板题
#include<iostream> #include<algorithm> #include<stdio.h> using namespace std; #def ...
- HDU 1698 Just a Hook (线段树模板题-区间求和)
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
- [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- hdu1823(二维线段树模板题)
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...
- POJ 3468 线段树裸题
这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...
- UESTC - 1057 秋实大哥与花 线段树模板题
http://acm.uestc.edu.cn/#/problem/show/1057 题意:给你n个数,q次操作,每次在l,r上加上x并输出此区间的sum 题解:线段树模板, #define _CR ...
- poj 3264 线段树 求区间最大最小值
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
随机推荐
- SpringBoot实现热部署(修改class不需要重启)
热部署: devtools可以实现页面热部署(即页面修改后会立即生效, 这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实 ...
- windows7家庭版,专业版,旗舰版,企业版版本区别
Windows 7包含6个版本,分别为Windows 7 Starter(初级版).Windows 7 Home Basic(家庭普通版).Windows 7 Home Premium(家庭高级版). ...
- March 02nd, 2018 Week 9th Friday
Make hay while the sun shines. 勿失良机. Last night the toothache woke me up and it was very difficult f ...
- 028实现strStr()
#pragma once #include "000库函数.h" /*********************自解**************/ //使用算法中的find 12ms ...
- 06.Python网络爬虫之requests模块(2)
今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...
- (转)Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
http://www.ityouknow.com/springboot/2018/03/28/dockercompose-springboot-mysql-nginx.html 我知道大家这段时间看了 ...
- mongoDB python 操作
mongoDB python 操作 import pymongo mongo_client = pymongo.MongoClient(host="127.0.0.1",port= ...
- go 调用windows dll 的三种方法
参考:https://blog.csdn.net/qq_39584315/article/details/81287669 大部分代码参考:https://studygolang.com/articl ...
- Apache 项目列表功能分类便于技术选型
big-data (49): Apache Accumulo Apache Airavata Apache Ambari Apache Apex Apache Avro Apache Be ...
- maven tomcat jstl 异常
在跑一个带jstl的例子的时候,遇到了这样一个错误: org.springframework.web.util.NestedServletException: Handler processing f ...