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 ...
随机推荐
- Thinkphp 批量更新方法 saveALL
批量更新只适用于一个字段的更新,原理是用自定义函数拼接sql语句,然后再执行sql语句. //数据 $data[] = array('id'=>1,'value'=>value1); $d ...
- hdu poj KMP简单题目总结
hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i] ...
- swift bug 调试记(wsgi.input)
第一次指定纠删码策略,修改了一部分swift代码后,执行PUT object,就被一个bug拦住.产生bug代码段如下: try: with ChunkReadTimeout(self.client_ ...
- 什么是Spring Boot简介
1.什么是spring boot 简单的说,spring boot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然后进行配置. 从本质上来说,Spring Boot就是Spring,它 ...
- poj 2112
#include <cstdio> #include <cstring> ;//点数的最大值 ;//边数的最大值 const int INF=0x3fffffff; struc ...
- ISO 7064:1983.MOD11-2校验码计算法 : (身份证校验码-18位)
/* 假设某一17位数字是 17位数字 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 加权因子 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 计算17位 ...
- Ubuntu 16.04安装XMind 8
下载: http://www.xmind.net/download/linux/ 解压 sudo unzip xmind-8-update2-linux.zip -d xmind8 移动到/opt目录 ...
- CentosOS 7: 创建Nginx+Https网站
参考文章: 1. https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E 2. http://songchenwen.com/tech/ ...
- 从内核源代码配置文件预測泛泰新品(A920 ?)
泛泰的IRON2 A910刚公布不久, 但近期在最新的泛泰890的内核源代码的config文件中又看到了这种编号,例如以下图: 图上ef63s 明显是A910S. 但这ef65s 又是什么呢, 看到这 ...
- 类似于SVN的文档内容差异对比工具winmerge
原文:http://www.jianshu.com/p/99282a4f3870 https://sourceforge.net/projects/winmerge/?source=typ_redir ...