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 ...
随机推荐
- ORACLE中date类型字段的处理
(1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如'21-8月-2003'或'21-8月 ...
- sqlite性能简单測试
主要測试sqlite在大数据量下的插入及查询性能: 測试环境:Centos6.4 1G内存 单核 数据量 大小 索引字段检索(耗时) 非索引字段检索(耗时) 总插入时间 10W 19M 0.001 ...
- 2014多校3 Wow! Such Sequence!段树
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=4893 这个问题还真是纠结啊--好久不写线段树的题了.由于这几天学伸展树.然后认为线段树小case了. ...
- Sql语句之select 5种查询
select 5种子句:注意顺序where / group by /having / order by / limit / 清空表中的数据:truncate 表名: 导入表结构(不含数据): crea ...
- js设置奇偶行数样式
$(document).ready(function () { odd = { "background": "none" }; //奇数样式 even = { ...
- 【转】emulator: ERROR: Could not load OpenGLES emulation library: lib64OpenglRender.so
[转]emulator: ERROR: Could not load OpenGLES emulation library: lib64OpenglRender.so ./emulator64-arm ...
- UILabel显示html文本
NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\ ...
- Java创建线程的细节分析
转载:http://shmilyaw-hotmail-com.iteye.com/blog/1880902 前言 关于线程创建的问题,可以说是老生常谈了.在刚开始学习Thread的时候基本上都会接触到 ...
- kali linux
1. Kali 2.0使用SSH进行远程登录 http://jingyan.baidu.com/article/eae07827a3e6bc1fec5485e3.html 一.配置SSH参数修改ss ...
- c语言: 文件io, 拷贝文件(二进制)
#include <stdio.h> #include <stdlib.h> #define TRAN_SZIE 1024 int copy_bin(char* from, c ...