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

Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 53703 Accepted: 25237
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 ≤ ABN), 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
题目链接:http://poj.org/problem?id=3264
分析:线段树求最大值和最小值,然后最大值减去最小值即为正解!貌似这题好像有暴力写法?
下面给出AC代码:
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 #define maxsize 200020
 6 typedef struct
 7 {
 8     int left,right;
 9     int maxn;
10     int minn;
11 }Node;
12 int n,m;
13 int Max,Min;
14 int num[maxsize];
15 Node tree[maxsize*20];
16 inline void buildtree(int root,int left,int right)// 构建线段树
17 {
18     int mid;
19     tree[root].left=left;
20     tree[root].right=right;// 当前节点所表示的区间
21     if(left==right)// 左右区间相同,则此节点为叶子,max 应储存对应某个学生的值
22     {
23         tree[root].maxn=num[left];
24         tree[root].minn=num[left];
25         return;
26     }
27     mid=(left+right)/2;
28     //int a,b;// 递归建立左右子树,并从子树中获得最大值
29     buildtree(2*root,left,mid);
30     buildtree(2*root+1,mid+1,right);
31     tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
32     tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
33 }
34 inline void find(int root,int left,int right)// 从节点 root 开始,查找 left 和 right 之间的最大值
35 {
36     int mid;
37     //if(tree[root].left>right||tree[root].right<left)// 若此区间与 root 所管理的区间无交集
38         //return;
39     if(left==tree[root].left&&tree[root].right==right)// 若此区间包含 root 所管理的区间
40     {
41         Max=max(tree[root].maxn,Max);
42         Min=min(tree[root].minn,Min);
43         return;
44     }
45     mid=(tree[root].left+tree[root].right)/2;
46     if(right<=mid)
47         find(root*2,left,right);
48     else if(left>mid)
49         find(root*2+1,left,right);
50     else
51     {
52         find(root*2,left,mid);
53         find(root*2+1,mid+1,right);
54         //tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
55         //tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
56         //return;
57     }
58 }
59 
60 int main()
61 {
62     //char c;
63     int i;
64     int x,y;
65     //scanf("d%d",&n,&m);
66     while(scanf("%d%d",&n,&m)!=EOF)
67     {
68         for(i=1;i<=n;i++)
69             scanf("%d",&num[i]);
70         buildtree(1,1,n);
71         for(i=1;i<=m;i++)
72         {
73             //getchar();
74             Max=-99999999999;
75             Min= 99999999999;
76             scanf("%d%d",&x,&y);
77             //if(c=='Q')
78                 //printf("%d\n",find(1,x,y));
79             //else
80             //{
81                // num[x]=y;
82                // update(1,x,y);
83             //}
84             find(1,x,y);
85             printf("%d\n",Max-Min);
86         }
87     }
88     return 0;
89 }

 

转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/7133096.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/254111.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

halcon测试一张图片是否过曝或过暗

read_image (Image, 1.bmp) count_obj (Image, Number) if(Number<0)return() endif min_max_gray (Image, Image, 0, Min, Max, Range) if(Min<1)*图像过暗 endif if(Max>254)*图像过曝 endif

真的要做一辈子的程序员吗?来自10年程序员的心声

经常听一些同学说&#xff1a;不知道下一份工作该去哪类公司做些什么&#xff0c;我的职场人际一团糟老板不重视我&#xff0c;我现在成长的非常慢所以又想跳槽了&#xff0c;我看不到公司的发展前景好迷茫&#xff0c;其实这一切的困惑都来源于没有做好职业规划或者你根本就没…

网络编程之 TCP / UDP 及其流程比较

TCP与UDP的区别 1、基于连接与无连接 2、对系统资源的要求&#xff08;TCP较多&#xff0c;UDP少&#xff09;3、UDP程序结构较简单 流模式与数据报模式 4、TCP保证数据正确性&#xff0c;UDP可能丢包 5、TCP保证数据顺序&#xff0c;UDP不保证具体编程时的区别 1、socket()的参…

Tomcat在Linux上的安装与配置

Tomcat在Linux上的安装与配置 1、 jdk下载地址&#xff1a; http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html tomcat下载地址:http://tomcat.apache.org/download-70.cg 2、jdk安装与配置.&#xff08;rpm包&#xff09; (1)jdk安装…

Spring在3.1版本后的bean获取方法的改变

xml配置不变&#xff0c;如下 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://…

使用halcon选择点拟合成直线求直线角度

原图 源码 read_image (Image, 0.bmp) dev_clear_window () dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image)binary_threshold (Image, Region, max_separability, dark, UsedThreshold) connection (Region, ConnectedRegions) select_s…

Linux网络/firewalld和netfilter/netfilter/iptables语法

为什么80%的码农都做不了架构师&#xff1f;>>> linux网络相关 查看网卡网络信息 ifconfig 命令查看网卡网络信息&#xff0c;比如ip、网关、子网掩码等&#xff0c;但是安装centos7的版本或者某些未知原因&#xff0c;此命令提示找不到&#xff0c;我们可以使用Yu…

Chrome开发者工具详解(4)-Profiles面板

Chrome开发者工具详解(4)-Profiles面板 如果上篇中的Timeline面板所提供的信息不能满足你的要求&#xff0c;你可以使用Profiles面板&#xff0c;利用这个面板你可以追踪网页程序的内存泄漏问题&#xff0c;进一步提升程序的JavaScript执行性能。 概述 当前使用的Chrome最新版为…

etcd raft library设计原理和使用

早在2013年11月份&#xff0c;在raft论文还只能在网上下载到草稿版时&#xff0c;我曾经写过一篇blog对其进行简要分析。4年过去了&#xff0c;各种raft协议的讲解铺天盖地&#xff0c;raft也确实得到了广泛的应用。其中最知名的应用莫过于etcd。etcd将raft协议本身实现为一个l…

halcon通过点拟合圆形,鼠标选点

原图 源码 read_image (Image, 0.bmp) dev_clear_window () dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image)binary_threshold (Image, Region, max_separability, dark, UsedThreshold) connection (Region, ConnectedRegions) select_s…

JDBC事务--软件开发三层架构--ThreadLocal

JDBC事务--软件开发三层架构--ThreadLocal 一.JDBC事务 1.概述: 事务是指逻辑上的一组操作!这一组操作,通常认为是一个整体,不可拆分! 特点:同生共死;事务内的这一组操作要么全部成功,要么全部失败! 作用:保证逻辑操作的完整性,安全性! 2.使用(3种方式) 1)面向数据库,使用S…

LINUX多播编程

一.单播&#xff0c;广播和多播 1.单播用于两个主机之间的端对端通信&#xff0c;广播用于一个主机对整个局域网上所有主机上的数据通信。单播和广播是两个极端&#xff0c;要么对一个主机进行通信&#xff0c;要么对整个局域网上的主机进行通信。实际情况下&#xff0c;经常需…

cas单点登录搭建

Cas Server下载&#xff1a;http://developer.jasig.org/cas/ Cas Client下载&#xff1a;http://developer.jasig.org/cas-clients/ 测试环境&#xff1a; jdk&#xff1a;java version "1.8.0_60" tomcat&#xff1a;apache-tomcat-7.0.65 mysql&#xff1a;mysql5…

新CIO:Mark Schwartz认为的领先IT

美国公民及移民服务局前任CIO&#xff0c;现任AWS企业战略师Mark Schwartz在伦敦举行的DevOps企业峰会上介绍了什么是领先的IT。\\Schwartz介绍说&#xff0c;老旧、传统的模型将业务和IT完全分开&#xff0c;他又提出了一种新的模型&#xff0c;在这种模型中&#xff0c;CIO担…

689D Magic Odd Square 奇数幻方

1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到nn止各数依次按下列规则存放&#xff1a;按 45方向行走&#xff0c;向右上&#xff0c;即每一个数存放的行比前一个数的行数减1&#xff0c;列数加1 (3) 如果行列范围超出矩阵范围&#xff0c;则回绕。例如1在第…

Java单例的常见形式

2019独角兽企业重金招聘Python工程师标准>>> Java单例的常见形式 本文目的&#xff1a;总结Java中的单例模式 本文定位&#xff1a;学习笔记 学习过程记录&#xff0c;加深理解&#xff0c;便于回顾。也希望能给学习的同学一些灵感 一、非延迟加载单例类 public cla…

运动控制卡的基类函数与实现例子

基类 namespace MotionCardDll {public abstract class IMotionCard{public Int32 m_Mode;public Int32 m_BoardId;//Card 号public Int32 m_Card_name;public Int32 m_StartAxisID

U-Boot启动过程完全分析

1.1 U-Boot 工作过程 U-Boot启动内核的过程可以分为两个阶段&#xff0c;两个阶段的功能如下&#xff1a; &#xff08;1&#xff09;第一阶段的功能 硬件设备初始化 加载U-Boot第二阶段代码到RAM空间 设置好栈 跳转到第二阶段代码入口 &#xff08;2&#x…

CJOJ 2171 火车站开饭店(树型动态规划)

CJOJ 2171 火车站开饭店&#xff08;树型动态规划&#xff09; Description 政府邀请了你在火车站开饭店&#xff0c;但不允许同时在两个相连的火车站开。任意两个火车站有且只有一条路径&#xff0c;每个火车站最多有 50 个和它相连接的火车站。 告诉你每个火车站的利润&#…

JavaWeb总结(十五)

AJAX&#xff08;Asynchronous JavaScript and XML&#xff08;异步的 JavaScript 和 XML&#xff09;&#xff09; AJAX的作用是什么&#xff1f; 在无需重新加载整个网页的情况下&#xff0c;能够更新部分网页的技术 是一种用于创建快速动态网页的技术 通过在后台与服务器进行…