POJ3264-Balanced Lineup-线段树
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 53721 | Accepted: 25244 | |
Case Time Limit: 2000MS |
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
题意就是在一堆数里找一定范围里的最大值和最小值,计算差值。
因为知道这个题肯定不能瞎写就写对,所以还很认真的写了一个代码,真是智障,不管怎么写,反正T了。
线段树,不会用,瞎写。。。
线段树大法好,可惜智障,改了12遍(此刻内心¥…&%……*(*&),出现各种问题嘛,存最小值要再有东西存人家啊。。。
最后实在改不出来了,求助了大佬,最后问题还是出在最小值问题上,要求最小值,一开始比较的ans就要比数组里的最大数要大啊。。。要审清题和题意。。。
线段树!!!
代码(垃圾写的乱七八糟):
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
#define N 300000
struct node{
int left,right,maxx,minn;
}tree[N*];
int n,m,minn,maxx;
void build(int l,int r,int pos){
tree[pos].left=l;
tree[pos].right=r;
tree[pos].maxx=;
tree[pos].minn=1e9;
if(tree[pos].left==tree[pos].right)return;
int mid=(l+r)>>;
build(l,mid,pos*);
build(mid+,r,pos*+);
}
void update(int x,int y,int pos){
if(tree[pos].left==x&&tree[pos].right==x){
tree[pos].maxx=y;
tree[pos].minn=y;
return;
}
int mid=(tree[pos].left+tree[pos].right)>>;
if(x>mid)
update(x,y,pos*+);
else
update(x,y,pos*);
tree[pos].maxx=max(tree[pos*].maxx,tree[pos*+].maxx);
tree[pos].minn=min(tree[pos*].minn,tree[pos*+].minn);
}
void query(int x,int y,int pos){
if(tree[pos].left==x&&tree[pos].right==y){
maxx=max(maxx,tree[pos].maxx);
minn=min(minn,tree[pos].minn);
return ;
}
int mid=(tree[pos].left+tree[pos].right)>>;
if(y<=mid)query(x,y,pos*);
else if(x>mid)query(x,y,pos*+);
else{
query(x,mid,pos*);
query(mid+,y,pos*+);
}
}
int main(){
int m,n;
int ans1,ans2;
while(~scanf("%d%d",&n,&m)){
getchar();
build(,n,);
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
update(i,x,);
}
int a,b;
while(m--){
scanf("%d%d",&a,&b);
maxx=;
minn=1e9;
query(a,b,);
printf("%d\n",maxx-minn);
}
}
return ;
}
(╯°Д°)╯︵┻━┻
POJ3264-Balanced Lineup-线段树的更多相关文章
- 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 ...
- 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线 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- 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 ...
随机推荐
- iOS 进阶—— iOS 内存管理
1 似乎每个人在学习 iOS 过程中都考虑过的问题 alloc retain release delloc 做了什么? autoreleasepool 是怎样实现的? __unsafe_unretai ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...
- 利用aop插入异常日志的2种方式
AOP是面向切面编程,利用这个技术可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分的耦合性降低,提高代码的可重用性,同时提高开发效率(来自百度百科). Spring AOP有两种实现方式,一 ...
- ArcGIS API for JavaScript 4.2学习笔记[12] View的弹窗(Popup)
看本文前最好对第二章(Mapping and Views)中的Map和View类有理解. 视图类有一个属性是Popup类型的popup,查阅API知道这个就是视图的弹窗,每一个View的实例都有一个p ...
- JavaScript构造函数、继承的理解
前两天稍微深入一点点理解了原型和原型链,然后就开始有挺多疑问的: function dog() { this.name = "huahua"; } var cat = new do ...
- MyBatis中批量插入数据对插入记录数的限制
<基于Mybatis框架的批量数据插入的性能问题的探讨>(作者:魏静敏 刘欢杰 来源:<计算机光盘软件与应用> 2013 年第 19 期)中提到批量插入的记录数不能超过1000 ...
- java推送数据到app--极光推送
之前项目有用到需要把数据推送到app端 采用的是极光推送 特此把工具类和pom.xml需要的jar整理如下 pom.xml需要jar如下 <!-- 极光推送 --> <depende ...
- 6、投资的一些思考 - CEO之公司管理经验谈
对于投资,前面笔者写过一个文:IT人经济思维之投资 - 创业与投资系列文章 ,里面列举了笔者自己做过的投资方面的内容.今天就说说公司投资的一些思考问题. 公司投资的问题,笔者还是那句话:关键是找出适合 ...
- C#窗口传值(CSDN实例)
//非模式窗体 相较独立From qform=new Form(); qform.Show(); //模式窗体 子依赖父Form qform=new Form();qform.Show ...
- 在macOS上通过pyenv安装和切换多版本Python
1. 安装homebrew 官网 http://brew.sh/index_zh-cn.html 打开终端,在终端中粘贴如下脚本 /usr/bin/ruby -e "$(curl -fsSL ...