Poj 3246 Balanced Lineup(线段树基础)
依旧是线段树基础题
询问区间的最大值和最小值之差,只有询问,没有插入删除。继续理解基础线段树
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 50005
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define L(x) x<<1
#define R(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
using namespace std;
struct node
{
int l,r,mid,max,min;
}tree[4*MAX];
int hei[MAX],a,b;
int maxx(int a,int b) {
if(a > b) return a;
return b;
}
int minn(int a,int b) {
if(a < b) return a;
return b;
}
void up(int num) {
if(tree[num].max < tree[L(num)].max) tree[num].max = tree[L(num)].max;
if(tree[num].max < tree[R(num)].max) tree[num].max = tree[R(num)].max;
if(tree[num].min > tree[L(num)].min) tree[num].min = tree[L(num)].min;
if(tree[num].min > tree[R(num)].min) tree[num].min = tree[R(num)].min;
} void build(int l,int r,int num) {
tree[num].l = l;
tree[num].r = r;
tree[num].mid = (l + r) >> 1;
tree[num].max = 0;
tree[num].min = INF;
if(l == r) {
tree[num].max = hei[l];
tree[num].min = hei[l]; return ;
}
build(l,tree[num].mid ,L(num));
build(tree[num].mid+1,r,R(num)); up(num);
} void query(int l,int r,int num) {
if(l <= tree[num].l && r >= tree[num].r) {
a = maxx(a,tree[num].max);
b = minn(b,tree[num].min);
return ;
} if(r <= tree[num].mid ) {
query(l,r,L(num));
}
else if(l > tree[num].mid) {
query(l,r,R(num));
}
else {
query(l,tree[num].mid,L(num));
query(tree[num].mid + 1,r,R(num));
}
} void test(int n)
{
for(int i=1; i<=2*n+1; i++){
printf("l:%d r:%d max:%d min:%d\n",tree[i].l,tree[i].r,tree[i].max,tree[i].min);
}
} int main() { int n,q,i;
int l,r;
cin >> n >> q;
for(i=1; i<=n; i++) {
scanf("%d",&hei[i]);
}
build(1,n,1);
//test(n);
for(i=1; i<=q; i++) {
a = 0;
b = INF;
scanf("%d%d",&l,&r);
query(l,r,1);
printf("%d\n",a - b);
}
return 0;
}
Poj 3246 Balanced Lineup(线段树基础)的更多相关文章
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- poj 3246 Balanced Lineup(线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 38942 Accepted: 18247 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
随机推荐
- Leetcode 动态规划 Unique Paths
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submi ...
- VC++6.0打开文件出错的解决办法
1.下载http://support.microsoft.com/kb/241396里面的一个叫FileTool.exe的文件并解压,解压后是一个vc工程,用vc6.0打开工程,编译,得到FileTo ...
- [译]SSRS 编写带参数的MDX报表
编写MDX报表长久以来对于报表人员来说都比较痛苦. 当然如果你用查询设计器(Query Designer) 直接拖拉数据集那就很方便,但是你们有没有想过查询设计器是怎么创建MDX的.或者创建的参数是如 ...
- 如何在macox下面配置集成ios和android游戏教程
教程截图: 1.准备工作,配置开发环境: 开发环境:mac ox 10.7.3 + xcode4.2 + ndk r7 + eclipse helios 部署环境:中兴v880 root过了 ...
- PHP 时间和日期 总结
PHP 时间戳 UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和. 可以使用time()函数来获取 ...
- centos安装epel源
用163的源,但是我发现这个源里面,根本没有libmcrypt libmcrypt-devel这二个包,装php扩展mcrypt时,又要用到这二个包,所以我手动装了libmcrypt包,但是给我的感觉 ...
- 织梦dedeCMS|系统基本配置标签
站点根网址: {dede:global.cfg_basehost/}调出网站的名称: {dede:field.title/}-{dede:global.cfg_webname/} 网站 ...
- Oracle中的EXCEPTION
Oracle系统预定义的异常 比如:SELF_IS_NULL.VALUE_ERROR.ZERO_DIVIDE等Oracle中自带的异常类型 使用方法: DECLARE V_Result ); BEGI ...
- Amazon Hiring Campus 2013 - Final 6
Let's assume that there is a simple market for beans. Every day there is a published bean price in t ...
- 转:不会定义jQuery插件,不要说会jQuery
一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...