Balanced Lineup(线段树)
http://poj.org/problem?id=3264
题意:n个数,q个询问,输出[l,r]中最大值与最小值的差。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=;
struct node
{
int l,r;
int Max,Min;
} Tree[N*];
int h[N],max1=,min1=N;
void build(int l,int r,int rt)
{
Tree[rt].l = l;
Tree[rt].r = r;
if (l==r)
{
Tree[rt].Max=Tree[rt].Min=h[r];
return ;
}
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
Tree[rt].Max=max(Tree[rt<<].Max,Tree[rt<<|].Max);
Tree[rt].Min=min(Tree[rt<<].Min,Tree[rt<<|].Min);
}
void Query(int l,int r,int rt)
{
if(Tree[rt].l==l&&Tree[rt].r==r)
{
max1=max(max1,Tree[rt].Max);
min1=min(min1,Tree[rt].Min);
return ;
}
int mid = (Tree[rt].l+Tree[rt].r)>>;
if (r <= mid)
Query(l,r,rt<<);
else if (l > mid)
Query(l,r,rt<<|);
else
{
Query(l,mid,rt<<);
Query(mid+,r,rt<<|);
}
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
for (int i = ; i <= n; i++)
scanf("%d",&h[i]);
build(,n,);
while(q--)
{
max1=-,min1=N;
int l,r;
scanf("%d%d",&l,&r);
Query(l,r,);
printf("%d\n",max1-min1);
}
}
return ;
}
Balanced Lineup(线段树)的更多相关文章
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- 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(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
随机推荐
- mac os 10.10解决pod问题
转一下 http://leancodingnow.com/how-to-get-cocoapods-work-on-yosemite/
- Codeforces Round #470 Div. 2题解
A. Protect Sheep time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- swiper.animate--css3翻页动画
基于swiper 的 animate动画,适用于Swiper2.x和Swiper3.x . 1. 使用Swiper Animate需要先加载swiper.animate.min.js和animate. ...
- 通过request对象获取客户端的相关信息
通过request对象获取客户端的相关信息 制作人:全心全意 通过request对象可以获取客户端的相关信息.例如HTTP报头信息.客户信息提交方式.客户端主机IP地址.端口号等等. request获 ...
- LINUX驱动、系统底层
就业模拟测试题-LINUX驱动.系统底层工程师职位 本试卷从考试酷examcoo网站导出,文件格式为mht,请用WORD/WPS打开,并另存为doc/docx格式后再使用 试卷编号:143921试卷录 ...
- linux设置系统时间与各种阻塞
前阵子做了一个P2P的通信系统,发现开机的时候和中间运行的时候会莫名报错,这个问题找了好久,后来从日志中看出来,所有节点上阻塞的操作同时超时. 而在超时左右,有新节点自动加入系统. 在新节点加入系统的 ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- JUnit 深入
Fixture 何谓 Fixture ?它是指在执行一个或者多个测试方法时需要的一系列公共资源或者数据,例如测试环境,测试数据等等.在编写单元测试的过程中,您会发现在大部分的测试方法在进行真正的测试之 ...
- MVC 中如果js,css文件放到视图文件夹无法访问的解决办法
在视图配置文件web.config 中添加如下节点,注意:此web.config 是视图文件夹中的,在你的views视图中可以找到<system.webServer>开头的<hand ...
- 【Codeforces 382C】Arithmetic Progression
[链接] 我是链接,点我呀:) [题意] 让你在n个数字中再加入一个数字 使得这n+1个数字排序之后 相邻两个数字的差都相同 [题解] 注意相邻为0的情况 这种情况 只有全都相同才行 只有一种情况 然 ...