Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 50004 Accepted: 23434
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
Source

USACO 2007 January Silver
用线段树查询每个区间上最大值与最小值的差值,用一个线段树维护最小值,一个维护最大值。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 200000 + 10;
int data[MAXN];
struct node {int l, r, value;
} segmin[MAXN], segmax[MAXN];
int build_segm_max(int x, int lson, int rson) {segmax[x].l = lson;segmax[x].r = rson;if (lson != rson) {int a = build_segm_max(x << 1, lson, (rson + lson)/2);int b = build_segm_max((x << 1) + 1, (rson + lson)/2 + 1, rson);return segmax[x].value = max(a, b);}return segmax[x].value = data[lson];
}
int build_segm_min(int x, int lson, int rson) {segmin[x].l = lson;segmin[x].r = rson;if (lson != rson) {int a = build_segm_min(x << 1, lson, (rson + lson)/2);int b = build_segm_min((x << 1) + 1, (rson + lson)/2 + 1, rson);return segmin[x].value = min(a, b);}return segmin[x].value = data[lson];
}
int query_seg_max(int x, int lson, int rson) {if (segmax[x].l > rson || segmax[x].r < lson) return 0;if (segmax[x].l >= lson && segmax[x].r <= rson) return segmax[x].value;int a = query_seg_max(x << 1, lson, rson);int b = query_seg_max((x << 1) + 1, lson, rson);return max(a, b);
}
int query_seg_min(int x, int lson, int rson) {if (segmin[x].l > rson || segmin[x].r < lson) return INF;if (segmin[x].l >= lson && segmin[x].r <= rson) return segmin[x].value;int a = query_seg_min(x << 1, lson, rson);int b = query_seg_min((x << 1) + 1, lson, rson);return min(a, b);
}
int main() {int N, Q, a, b;scanf("%d%d", &N, &Q);for (int i = 1; i <= N; i++) scanf("%d", &data[i]);build_segm_max(1, 1, N); build_segm_min(1, 1, N);for (int i = 0; i < Q; i++) {scanf("%d%d", &a, &b);printf("%d\n", query_seg_max(1, a, b) - query_seg_min(1, a, b));}return 0;
}

转载于:https://www.cnblogs.com/cniwoq/p/6770747.html

POJ 3264 Balanced Lineup 【线段树】相关推荐

  1. POJ 3264 Balanced Lineup

    POJ 3264 Balanced Lineup 题目链接 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00 ...

  2. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  3. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  4. POJ 3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53629   Accepted: 25223 ...

  5. POJ 3264: Balanced Lineup

    2019独角兽企业重金招聘Python工程师标准>>> 题目在此 解题思路:查询区间最大值/最小值之差,最基础的线段树应用. 代码: #include <cstdio>/ ...

  6. POJ 3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 24349   Accepted: 11348 ...

  7. 【RMQ】POJ 3264 Balanced Lineup

    前言 这题出现在RMQRMQRMQ的WordWordWord文档中,我就直接把以前ACACAC过的程序交上去,结果WAWAWA了,然后才发现这道题还要求最小值... 链接 http://poj.org ...

  8. POJ 3264 Balanced Lineup (RMQ)

    Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...

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

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

最新文章

  1. windows10远程桌面连接及问题解决
  2. html5知识总结,HTML5初级知识总结
  3. C#委托使用详解(Delegates)
  4. python 东哥 with open_Python一行代码搞定炫酷可视化,你需要了解一下Cufflinks
  5. html中文乱码_Nginx目录浏览的中文显示问题订正
  6. SQL SERVER 打开脚本报“未能完成操作,存储空间不足”
  7. oracle函数大全指数运算,Oracle 基础语句 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数......
  8. Python complex()
  9. [转]js模块化(一)
  10. day3--numpy
  11. ArcGISEngine二次开发(5):添加矢量要素
  12. EntityFramework6 用 PostgreSQL
  13. CF991E Bus Number
  14. c语言软件下载与配置
  15. 如何得到JavaVM,JNIEnv接口
  16. 益智类游戏关卡设计:逆推法--巧解益智类游戏关卡设计
  17. 淘宝直通车怎样设置定向推广出价问题总结
  18. 移动混合开发框架+Android原生模块化/组件化
  19. 网络摄像机内部结构图
  20. 【2019秋招】携程数据分析岗笔试编程题——画心形

热门文章

  1. 物联网通信协议——比较-MQTT、 DDS、 AMQP、XMPP、 JMS、 REST、 CoAP
  2. Spring框架Runtime介绍(导包)
  3. 安全测试chicklist
  4. 洛谷 P1129 [ZJOI2007]矩阵游戏 解题报告
  5. Asp.Net MVC中使用ACE模板之Jqgrid
  6. visual studio 2010运行速度提速
  7. SYSTEM32 下的几乎所有文件的简单说明
  8. 什么是大数据「实时流计算」?深度解析它的4大应用及4个特点
  9. 资源 | 想进行数据科学项目却没有数据集?26个数据集网站汇总
  10. STM32之定时器原理