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 ...
随机推荐
- [CF543D]Road Improvement
题目大意:给定一个无根树,给每条边黑白染色,求出每个点为根时,其他点到根的路径上至多有一条黑边的染色方案数,模$1e9+7$. 题解:树形$DP$不难想到,记$f_u$为以$1$为根时,以$u$为根的 ...
- [洛谷P2568]GCD
题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ...
- 洛谷 P3203 [HNOI2010]弹飞绵羊 解题报告
P3203 [HNOI2010]弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一 ...
- ext radiogroup如何取值和设值
var radios = Ext.create('Ext.form.Panel', { title: 'RadioGroup Example', width: 300, height: 125, bo ...
- SQLMap的前世今生(Part1)
http://www.freebuf.com/sectool/77948.html 一.前言 谈到SQL注入,第一时间就会想到神器SQLMAP,SQLMap是一款用来检测与利用的SQL注入开源工具.那 ...
- 2016广东工业大学校赛 E题 GDUT-oj1173
Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...
- jsonArray与jsonObject
最近两个星期接触最多的就是json和map了. 之前用到的json,就是一个键对应一个值,超级简单的一对一关系.现在用到的json那可以层层嵌套啊,刚开始接触的时候,确实有种崩溃的赶脚,不想去理,取个 ...
- bzoj 2525 [Poi2011]Dynamite 二分+树形dp
[Poi2011]Dynamite Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 270 Solved: 138[Submit][Status][D ...