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 OC应用异常捕获,崩溃退出前返回信息给后台
			
第三方的了,有友盟,腾讯的bugly 查了一下网上类似的代码很多,在借鉴前辈的代码后,组合了一下: 1.捕获异常信息 2.获得当前日期,版本,系统 3.获得出bug的视图控制器转为字符串 4.将前3条 ...
 - geoserver集成以及部署arcgis server瓦片数据
			
关注重点: 一般来说,geoserver是不支持arcgis server格式瓦片数据部署的,至少我本机的geoserver版本(2.8.5)以及之前的版本并没有集成进来,不知道目前官网的最新版是否支 ...
 - openstack操作之一 命令行
			
在openstack环境中提供了多种操作虚拟机的方法,有最简单直接的dashborad界面,有不直观但高效的命令行,还有进阶版的postman调用openstack restfulapi和命令行中使用 ...
 - Lucene分词停用词库stopwords
			
! " $ % & ' ( ) * + , - -- . .. ... ...... ................... ./ .一 .数 .日 / // 0 1 2 3 4 5 ...
 - js 停止事件冒泡 阻止浏览器的默认行为(阻止a标签跳转 )
			
在前端开发工作中,由于浏览器兼容性等问题,我们会经常用到"停止事件冒泡"和"阻止浏览器默认行为". 1..停止事件冒泡 JavaScript代码 //如果提供了 ...
 - 在一个终端后台运行的进程在新的终端中使用job不会被发现
			
我在一个终端后台运行了一个程序.之后由于工作需要又新开了一个新的终端.在新的终端中,我想查看后台程序的运行状态,输入jobs时,发现没有后台任务运行.难道我的程序死掉了?我接着top了下,发现我的后台 ...
 - JavaScript的DOM编程--09--节点的替换
			
节点的替换: 1). replaceChild(): 把一个给定父元素里的一个子节点替换为另外一个子节点 var reference = element.replaceChild(newChild,o ...
 - Android动画(一)-视图动画与帧动画
			
项目中好久没用过动画了,所以关于动画的知识都忘光了.知识总是不用则忘.正好最近的版本要添加比较炫酷的动画效果,所以也借着这个机会,写博客来整理和总结关于动画的一些知识.也方便自己今后的查阅. Andr ...
 - 微信跳一跳辅助自动跳Python
			
一.说明 此代码借鉴github一位大神所写,已经做了简化合并处理,如果能成功连上手机并运行,可以实现程序自动玩游戏,刷个1000+的分数轻轻松松 github源码地址 https://github. ...
 - 【HTML初识】
			
一.BS模式 BS(Browser-Server)模式:顾名思义为浏览器-服务器的意思,对比的话类似我们PC上面浏览器使用的产品即为BS模式产品,例如google doc.各类网站等. 服务端开启一个 ...