POJ 3264.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 ;
}
(╯°Д°)╯︵┻━┻
POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)的更多相关文章
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- ACM_最值差(线段树区间查询最值)
最值差 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定N个数A1A2A3A4...AN.求任意区间Ai到Aj中的最大数与最小 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
- POJ - 3264——Balanced Lineup(入门线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 68466 Accepted: 31752 ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- 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(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- poj 3264 Balanced Lineup (线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42489 Accepted: 20000 ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
随机推荐
- 集群hadoop ubuntu版
搭建ubuntu版hadoop集群 用到的工具:VMware.hadoop-2.7.2.tar.jdk-8u65-linux-x64.tar.ubuntu-16.04-desktop-amd64.is ...
- 【bzoj3653】谈笑风生 DFS序+树状数组
题目描述 给出一棵以1为根的有根树,q次询问,每次询问给出a和k,求点对 (b,c) 的数目,满足:a.b.c互不相同,b与a距离不超过k,且a和b都是c的祖先. 输入 输入文件的第一行含有两个正整数 ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- 停课day2
感觉今天好颓啊,我才把昨晚那五道题a了,(但我明明一直在学啊,为啥这么慢,难道是我太笨了?) 闲话少叙,先说做法 问题 A: C Looooops 题目描述 对于C的for(i=A ; i!=B ;i ...
- 【NOIP 模拟赛】钟 模拟+链表
biubiu~~ 这道题实际上就是优化模拟,就是找到最先死的让他死掉,运用时间上的加速,题解上说,要用堆优化,也就是这个意思. 对于链表,单项链表和循环链表都不常用,最常用的是双向链表,删除和插入比较 ...
- MFC随机数
void CMFCDemoDlg::OnClickedGetrand() { wchar_t str[]; //srand((unsigned)time(NULL)); int num = rand( ...
- js保存用户名与密码
<script> window.onload = function(){ var oForm = document.getElementById('loginForm'); ...
- wget下载HTTPS链接
wget -c -O master.zip --no-check-certificate https://github.com/mitsuhiko/flask/archive/master.zip # ...
- 异常message:There is no database named cloudera_manager_metastore_canary_test_db_hive_hivemetastore
NoSuchObjectException(message:There is no database named cloudera_manager_metastore_canary_test_db_h ...
- 7月16号day8总结
今天学习过程和小结 1.列举Linux常用命令 shutdown now Linux关机 rebot重启 mkdir mkdir -p递归创建 vi/touth filename rm -r file ...