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 ...
随机推荐
- restful风格url Get请求查询所有和根据id查询的合并成一个controller
restful风格url Get请求查询所有和根据id查询的合并成一个controller的方法 原代码 // 127.0.0.1:8080/dep/s @ApiOperation(value=&qu ...
- Linux之 sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- UVA - 10825 Anagram and Multiplication
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34594 有一个m位n进制的数,它的特性是这个数依次乘以2,3... ...
- 【Codeforces 1019A】Elections
[链接] 我是链接,点我呀:) [题意] 每个人都有自己喜欢的队员 但是如果贿赂他们可以让他们更改自己喜欢的队员 问你最少要花多少钱贿赂队员才能让1号队员严格有最多的人喜欢? [题解] 除了1号之外, ...
- 使用ajax传值,后台乱码
spring mvc,使用ajax传值,后台发现是乱码 解决方法: 后台的方法里加入 request.setCharacterEncoding("UTF-8"); 就行了 我前 ...
- 通过代码学习python之@property,@staticmethod,@classmethod
URL: https://www.the5fire.com/python-property-staticmethod-classmethod.html #coding=utf-8 class MyCl ...
- C - How Many Tables 并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- ubuntu无法update
ubuntu系统执行sudo apt-get update报错解决方法: 编辑源列表文件 sudo vi /etc/apt/sources.list 将原来的列表删除,添加如下内容(中科大镜像源) d ...
- Sublime Text 3配置支持Markdown编辑
继上一篇http://www.cnblogs.com/EasonJim/p/7119304.html文章安装好之后,对Markdown支持需要做如下处理: 1.按下[Ctrl]+[Shift]+[P] ...
- 22、Java并发性和多线程-Java中的读/写锁
以下内容转自http://ifeve.com/read-write-locks/: 相比Java中的锁(Locks in Java)里Lock实现,读写锁更复杂一些.假设你的程序中涉及到对一些共享资源 ...