POJ3264(线段树求区间最大最小值)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 41162 | Accepted: 19327 | |
| Case Time Limit: 2000MS | ||
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
/*
* @Author: LinK
* @Date: 2015-10-31 18:20:11
* @Last Modified by: LinK
* @Last Modified time: 2015-10-31 18:27:30
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; struct Node {
int ma, mi;
} tree[maxn << ]; int num[maxn];
int n, q; void build(int rt, int l, int r) {
if (l == r) {
tree[rt].ma = num[l];
tree[rt].mi = num[l];
return;
}
int mid = (l + r) >> ;
build(rt << , l, mid);
build(rt << | , mid + , r);
tree[rt].ma = max(tree[rt << ].ma, tree[rt << | ].ma);
tree[rt].mi = min(tree[rt << ].mi, tree[rt << | ].mi);
} int query_ma(int rt, int l, int r, int L, int R) {
if (L <= l && R >= r) return tree[rt].ma;
int mid = (l + r) >> ;
if (R <= mid) return query_ma(rt << , l, mid, L, R);
if (L > mid) return query_ma(rt << | , mid + , r, L, R);
return max(query_ma(rt << , l, mid, L, R), query_ma(rt << | , mid + , r, L, R));
} int query_mi(int rt, int l, int r, int L, int R) {
if (L <= l && R >= r) return tree[rt].mi;
int mid = (l + r) >> ;
if (R <= mid) return query_mi(rt << , l, mid, L, R);
if (L > mid) return query_mi(rt << | , mid + , r, L, R);
return min(query_mi(rt << , l, mid, L, R), query_mi(rt << | , mid + , r, L, R));
} int main() {
while (~scanf("%d %d", &n, &q)) {
for (int i = ; i <= n; i ++) scanf("%d", &num[i]);
build(, , n);
int a, b;
while (q --) {
scanf("%d %d", &a, &b);
int ma = query_ma(, , n, a, b);
int mi = query_mi(, , n, a, b);
printf("%d\n", ma - mi);
}
} return ;
}
POJ3264(线段树求区间最大最小值)的更多相关文章
- poj 3264 线段树 求区间最大最小值
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps
https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...
- BZOJ 4127: Abs (树链剖分 线段树求区间绝对值之和 带区间加法)
题意 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d(d>=0) 2 u v 表示询问路径 (u,v) 上点权绝对值的和 分析 绝对值之和不好处理,那么我们开 ...
- HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 1e5个点,问 ...
- hdu1166 敌兵布阵(线段树 求区间和 更新点)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu3074 线段树求区间乘积(单点更新)
题意: 给你n个数,两种操作,(1) 把第b个数改成c (2)算出b-c的乘积,结果对1000000007取余. 思路: 线段树单点更新,简单题目,不多解释,具体看代码. #i ...
随机推荐
- 常用模块(xml)
XML(可扩展性标记语言)是一种非常常用的文件类型,主要用于存储和传输数据.在编程中,对XML的操作也非常常见. 本文根据python库文档中的xml.etree.ElementTree类来进行介绍X ...
- 9.0 toast定位+WebDriverWait显示等待
Toast 判断-----基本操作问题 首先基本操作,进入安卓市场的账号密码页面--- from appium import webdriver from selenium.webdriver.su ...
- (原创)像极了爱情的详解排序二叉树,一秒get
排序二叉树(建立.查找.删除) 二叉树我们已经非常熟悉了,但是除了寻常的储存数据.遍历结构,我们还能用二叉树做什么呢? 我们都知道不同的遍历方式会对相同的树中产生不同的序列结果,排序二叉树就是利用二叉 ...
- day-10 sklearn库实现SVM支持向量算法
学习了SVM分类器的简单原理,并调用sklearn库,对40个线性可分点进行训练,并绘制出图形画界面. 一.问题引入 如下图所示,在x,y坐标轴上,我们绘制3个点A(1,1),B(2,0),C(2,3 ...
- kaldi基于GMM的单音素模型 训练部分
目录 1. gmm-init-mono 模型初始化 2. compile-train-graghs 训练图初始化 3. align-equal-compiled 特征文件均匀分割 4. gmm-acc ...
- 主外键多表查询demo
https://www.cnblogs.com/DragonFire/p/6949767.html mySQL练习-主外键多表查询 MySQL练习-主外键多表查询 练习: 1.建立表关系: 请创建如下 ...
- poi excel导出 xssf 带下拉框
需求:导出之后带有二级级联的下拉框.(类似于省市). 最初的思路是怀疑是不是数组内串太多了,导出之后的excel有36行,调试的误区在于刚开始认为对行数有限制,后自己写了一个测试类,才发现不是行数,而 ...
- nodejs安装错误
network错误: npm ERR! network tunneling socket could not be established, cause=connect ECONNREFUSED 12 ...
- [洛谷P2657][SCOI2009]windy数
题目大意:不含前导零且相邻两个数字之差至少为$2$的正整数被称为$windy$数.问$[A, B]$内有多少个$windy$数? 题解:$f_{i, j}$表示数有$i$位,最高位为$j$(可能为$0 ...
- 【TMD模拟赛】上低音号 链表
这道题一看有两个出发现点,一枚举点去找边界,想了一会就Pass了...,二是枚举相框,我们最起码枚举两个边界,然后发现平行边界更好处理,然而仍然只有30分,这个时候就来到了链表的神奇应用,我们枚举上界 ...