Balanced Lineup(线段树求最大值,最小值)

news/2024/7/6 0:28:00 标签: c语言, 算法

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<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N=50010,INF=0x3f3f3f3f;
int n,y,x,ma,mi,m;
struct nn {
	int l,r,maxx,minn;
} p[N*4];
void build(int l,int r,int o) {
	p[o].l=l;
	p[o].r=r;
	if(l==r) {
		int x;
		scanf("%d",&x);
		p[o].maxx=x;
		p[o].minn=x;
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,o*2);
	build(mid+1,r,o*2+1);
	p[o].maxx=max(p[o*2].maxx,p[o*2+1].maxx);
	p[o].minn=min(p[o*2].minn,p[o*2+1].minn);
}
void quary(int L,int R,int o) {
	if(L==p[o].l&&p[o].r==R) {
		mi=min(mi,p[o].minn);
		ma=max(ma,p[o].maxx);
		return ;
	}
	int mid=(p[o].l+p[o].r)/2;
	if(R<=mid) quary(L,R,o*2);
	else if(mid<L) quary(L,R,o*2+1);
	else{
		quary(L,mid,o*2);
		quary(mid+1,R,o*2+1);
	}

}
int main() {
	int x,y;
	scanf("%d%d",&n,&m);
	build(1,n,1);
	while(m--) {
		ma=0;
		mi=INF;

		scanf("%d%d",&x,&y);
		quary(x,y,1);
		printf("%d\n",ma-mi);
	}
	return 0;
}

http://www.niftyadmin.cn/n/1413542.html

相关文章

架构,构件,组件,框架,中间件之间区别

中间件是一种独立的系统软件或服务程序&#xff0c;分布式应用软件借助这种软件在不同的技术之间共享资源&#xff1b;Web Services就是可以通过web描述、发布、定位和调用的模块化应用&#xff1b;组件就是对象&#xff1b;模式&#xff0c;即pattern。其实就是解决某一类问题…

css开发素材网址

1、border-collapse 为表格设置合并边框模型 2、border-spacing border-spacing 属性设置相邻单元格的边框间的距离 backface-visibility:hidden; 隐藏被旋转的 div 元素的背面 m.dx.com http://www.365mini.com/page/jquery-resize.htmjQuery 中文手册网址 W3CPluscss3 htt…

ASP.NET MVC + jQuery + Newtonsoft.Json 快樂的AJAX

這是目前我的方案&#xff0c;個人覺得還蠻輕巧自在的。 Controller負責把要輸出的資料序列成json。 Html.ActionUrl 這隻method原來的MVC Toolkit沒有&#xff0c;我隨手加的。 我 是用Newtonsoft.Json作物件序列成JSON&#xff0c;那為什麼不用MS Ajax內建的 System.Web.Sc…

菱形继承与菱形虚拟继承

“菱形继承与菱形虚拟继承” “继承”是c面向对象语言的特点之一&#xff0c;对于一个类&#xff0c;我们如果想对这个类的功能进行扩充&#xff0c;这就可以通过"继承"的方式重新增添或删除这个类中的某些功能。C语言支持单继承和多继承&#xff0c;如果不大清楚单…

微調一下 Json.net ,讓他可以序列基本型別

因為 Json.net 是有附原始碼的&#xff0c;他也附了單元測試的專案&#xff0c;底下是我額外增加的UnitTest&#xff0c;我的目標就是讓底下的測試可以pass&#xff0c;而且原來的Test 也要都能通過。 ValueTypeTest.cs using System;using NUnit.Framework;namespace Newtons…

.Net 底下,Json 相關套件的限制

Json.Net 無法序列基本型別(string, int)&#xff0c;Asp.Net Ajax 無法正確序列日期&#xff0c;AjaxPro序列出我不想要的_type字串 1. Json.Net 是我最常使用的序列/反序列json套件&#xff0c;標榜速度快&#xff0c;對於一對多關係的object 也都能正常運作, 己能滿足我平日…

SpringBoot开发案例之打造私有云网盘

前言 最近在做工作流的事情&#xff0c;正好有个需求&#xff0c;要添加一个附件上传的功能&#xff0c;曾找过不少上传插件&#xff0c;都不是特别满意。无意中发现一个很好用的开源web文件管理器插件 elfinder&#xff0c;功能比较完善&#xff0c;社区也很活跃&#xff0c;还…

MySQL TOO BAD row's Range Lock Compare with PostgreSQL and Oracle

MySQL的InnoDB引擎&#xff0c;当UPDATE一个范围的数据时&#xff0c;会锁住比预期更多的ROW&#xff0c;而Oracle和PostgreSQL没有这种现象.来自《High Performance MySQL》一书。测试版本:MySQL 5.5.10PostgreSQL 9.0.2Oracle 10.2.0.4举例如下:1. MySQL (有索引的情况)Sessi…