Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
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

Line 1: Two space-separated integers, N and Q.
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

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output

6
3
0
写在前面:仅作了解
——————————————————————————————————————————————
思路:无

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{int maxn,minn,x,y;
}tree[800010];
int n,q,x,y;
int head,tail;
int a[200010];
int in()
{int t=0;char ch=getchar();while (ch>'9'||ch<'0') ch=getchar();while (ch>='0'&&ch<='9') t=t*10+ch-'0',ch=getchar();return t;
}
void build_tree(int now,int l,int r)
{tree[now].x=l;tree[now].y=r;if (l==r) {tree[now].maxn=tree[now].minn=a[l];return;}int mid=(l+r)/2;build_tree(now*2,l,mid);build_tree(now*2+1,mid+1,r);tree[now].maxn=max(tree[now*2].maxn,tree[now*2+1].maxn);tree[now].minn=min(tree[now*2].minn,tree[now*2+1].minn);
}
int findmin(int now,int begin,int end)
{if (begin>tail||end<head) return 9999999;if (begin>=head&&end<=tail) return tree[now].minn;int mid=(begin+end)/2;return min(findmin(now*2,begin,mid),findmin(now*2+1,mid+1,end));
}
int findmax(int now,int begin,int end)
{if (begin>tail||end<head) return -9999999;if (begin>=head&&end<=tail) return tree[now].maxn;int mid=(begin+end)/2;return max(findmax(now*2,begin,mid),findmax(now*2+1,mid+1,end));
}
main()
{n=in();q=in();for (int i=1;i<=n;i++) a[i]=in();build_tree(1,1,n);for (int i=1;i<=q;i++){head=in();tail=in();printf("%d\n",findmax(1,1,n)-findmin(1,1,n));}
}

【POJ3264】Balanced Lineup,线段树入门相关推荐

  1. POJ3264 Balanced Lineup【线段树】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 63040   Accepted: 29405 ...

  2. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  3. POJ——3624 Balanced Lineup(线段树入门——区间最值问题)

    原题链接:http://poj.org/problem?id=3264 每天挤奶时,农夫John的N头奶牛(1≤N≤50,000头)总是按照相同的顺序排列.一天,农夫约翰决定和几头牛组织一场极限飞盘游 ...

  4. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  5. szu 寒训个人复习第一天 线段树入门单点修改,区间修改,以及线段树的扩展运用[线段树+dp][区间最大公约数]

    寒讯内容有点过多(其实是我太菜了)水一波怕忘了(人老了)**什么是线段树** 线段树是本蒟蒻感觉用处特别大的算法 那么线段树上面的节点表示什么意思呢? 线段树,上面的节点表示一个区间,父亲节点表示的区 ...

  6. poj1195 Mobile phones 二维线段树入门

    二维线段树就是树套树,线段树套线段树... #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  7. hdu1166敌兵布阵hdu1754I Hate It(线段树入门)

    单点更新是最最基础的线段树,只更新叶子节点,然后把信息用pushup这个函数更新上来. http://acm.hdu.edu.cn/showproblem.php?pid=1166 update单点更 ...

  8. 数据结构之线段树入门(单点更新区间查询)

    线段树是学习数据结构必须学习的一种数据结构,在ACM,蓝桥等比赛中是经常出现的.利用线段树解题,会使得题目简单易理解.而且线段树是数据结构中比较基础而且用的很多的一种. 线段树定义 线段树是一种二叉搜 ...

  9. 线段树入门 (zz)

    从简单说起,线段树其实可以理解成一种特殊的二叉树.但是这种二叉树较为平衡,和静态二叉树一样,都是提前已经建立好的树形结构.针对性强,所以效率要高.这里又想到了一句题外话:动态和静态的差别.动态结构较为 ...

最新文章

  1. SQL SERVER2005加密解密数据
  2. python 报ImportError: Install xlrd = 1.0.0 for Excel support错误
  3. Doug Cutting—访谈录
  4. OpenCV_006-OpenCV 轨迹栏作为调色板
  5. C# 客户端内存优化分析
  6. 数学建模1(历年问题与模型)
  7. Redis:哨兵模式(针对某一模块,数据量有限)
  8. 类与对象- 课后作业1
  9. 聊聊Linux2038年问题
  10. python采集世界大学排名并作数据可视化, 来看看你的母校上榜没~
  11. vue的tap插件_vue移动端touch插件
  12. java 中Shallow Heap与Retained Heap的区别
  13. java 8 stream 的学习
  14. 吸拖一体机和扫地机器人哪个好,吸拖一体机值得买吗
  15. 蓝牙笔记《蓝牙技术基础》
  16. 短视频去水印微信小程序,免费去除视频水印
  17. debian 更换源 使用国内源 配置方法
  18. 2019-详细Android Studio开发百度地图(4)—百度地图_路线规划的实现
  19. 李筱懿:视频号如何运营才能出爆款?
  20. 《智能经济时代初现雏形:数据赋能至上,数据共享先行》阅读笔记

热门文章

  1. 补习系列(8)-springboot 单元测试之道
  2. python matplotlib 柱状图点击事件_Python:matplotlib分组Bar柱状图
  3. Android笔记 notification
  4. HDFView 3.1.2 在WIN10系统安装后打开出现黑框闪退的解决方法
  5. docker中使用git_如何在 Docker 中使用 Docker
  6. (组合数学笔记)Pólya计数理论_Part.3_置换群及其性质
  7. Spark vs. MapReduce 时间节约66%,计算节约40%
  8. VMware中安装deepin虚拟机
  9. quartz框架_定时任务调度框架Quartz
  10. python 矩阵除法_Python线性代数学习笔记——矩阵的基本运算和基本性质,实现矩阵的基本运算...