依旧是线段树基础题

询问区间的最大值和最小值之差,只有询问,没有插入删除。继续理解基础线段树

#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(线段树基础)的更多相关文章

  1. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  2. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  3. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  4. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  5. 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 ...

  6. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  7. poj 3246 Balanced Lineup(线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 38942   Accepted: 18247 ...

  8. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  9. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

随机推荐

  1. 1148 - Mad Counting(数学)

    1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...

  2. codeforces#FF DIV2C题DZY Loves Sequences(DP)

    题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...

  3. Jedis中的一致性hash

    Jedis中的一致性hash 本文仅供大家参考,不保证正确性,有问题请及时指出 一致性hash就不多说了,网上有很多说的很好的文章,这里说说Jedis中的Shard是如何使用一致性hash的,也为大家 ...

  4. Python:2D画图库matplotlib学习总结

    本文为学习笔记----总结!大部分为demo.一部分为学习中遇到的问题总结.包含怎么设置标签为中文等.matlab博大精深.须要用的时候再继续吧. Pyplot tutorial Demo地址为:点击 ...

  5. JNI(2)

    JNI(2) 访问字段和方法 JNI允许本地代码访问java 对象的字段和方法. 调用需要两个步骤: 例如调用cls类的f方法, 1. 获取方法ID jmethodID mid = env->G ...

  6. SharePoint网站集功能介绍

    SharePoint网站集功能介绍 https://support.office.com/zh-cn/article/%E5%90%AF%E7%94%A8%E6%88%96%E7%A6%81%E7%9 ...

  7. 「OC」类的深入研究、description方法和sel

    一.类的深入研究 (一)类的本质 类本身也是一个对象,是class类型的对象,简称“类对象”. Class类型的定义: Typedef struct obj class *class; 类名就代表着类 ...

  8. Qt 5.2.0 和 VS 2012集成

    下载两个安装包,后面一个add-in是必需的 Qt 5.2.0 for Windows 64-bit (VS 2012, 590 MB) (Info) Visual Studio Add-in 1.2 ...

  9. hdu 4782 Beautiful Soupz

    模拟.其实这题就是题目比较长而已...读完题目就差不多了.tag直接读就可以了,题目说了不用修改.然后整个题目就是让求text部分,严格按空格分开.注意每行前面空格个数. #include<al ...

  10. PHPExcel 生成图形报表

    db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0,  PHP代码: $dir = dirname(__FILE__); require $d ...