首页 > 其他分享 >POJ 3264 Balanced Lineup

POJ 3264 Balanced Lineup

时间:2023-06-12 14:37:06浏览次数:39  
标签:Lineup int height cows 000 POJ st Balanced include


思路:线段树求最大值减最小值,每个结点分别维护最大值和最小值即可。



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 50010
#define LL long long
#define lson i<<1,l,m
#define rson (i<<1)|1,m+1,r
struct ST
{
	int l,r;
	int Max,Min;
}st[maxn<<2];
void pushup(int i)
{
	st[i].Max=max(st[i<<1].Max,st[(i<<1)|1].Max);
	st[i].Min=min(st[i<<1].Min,st[(i<<1)|1].Min);
}
void build(int i,int l,int r)
{
	st[i].l=l;
	st[i].r=r;
	if (st[i].l==st[i].r)
	{
		scanf("%d",&st[i].Max);
		st[i].Min = st[i].Max;
		return;
	}
	int m = (st[i].l+st[i].r)>>1;
	build(lson);
	build(rson);
	pushup(i);
}
int query1(int i,int l,int r)
{
	if (st[i].l==l && st[i].r==r)
		return st[i].Max;
	int m = (st[i].l+st[i].r)>>1;
	if (r<=m)
		return query1(i<<1,l,r);
	else if (l>m)
		return query1((i<<1)|1,l,r);
	else
		return max(query1(lson),query1(rson));
}
int query2(int i,int l,int r)
{
	if (st[i].l==l && st[i].r==r)
		return st[i].Min;
	int m = (st[i].l+st[i].r)>>1;
	if (r<=m)
		return query2(i<<1,l,r);
	else if (l>m)
		return query2((i<<1)|1,l,r);
	else
		return min(query2(lson),query2(rson));
}
int n,q;
int cas=1,T;
int main()
{
	while (scanf("%d%d",&n,&q)!=EOF)
	{
		build(1,1,n);
		while (q--)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			printf("%d\n",query1(1,l,r)-query2(1,l,r));
		}
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


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..  NQ+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






标签:Lineup,int,height,cows,000,POJ,st,Balanced,include
From: https://blog.51cto.com/u_16156555/6462534

相关文章

  • POJ1837 DP
    POJ1837DP题题目一开始看了N久…意思大概是有一个天平,左边臂长是-15到0,右边臂长是0到15,给你c个挂钩,g个砝码,每一个砝码重量都在1到25,问将所有砝码挂到天平上并使之平衡的方案有多少个。要使之平衡由物理知识可知力矩=0,左边重量X左边臂长+右边重量X右边臂长=0,故状态一共有25*15......
  • POJ 2352 HDU1541 Stars(树状数组)
    题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi,yi)满足xi<=x且yi<=y。思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的......
  • POJ 3498 March of the Penguins(枚举+最大流)
    题意:在X,Y坐标系中有N(N<=100)个冰块...有些冰块上有若干只企鹅..每只企鹅一次最多跳M距离..一个冰块在有Mi个企鹅离开..就会消失..问有哪些冰块可以作为集合点..就是所有企鹅都能成功到这个冰块上来.思路:枚举每一块冰块,看看最大流能否等于企鹅总数即可      建图:把每块......
  • POJ 2019 Cornfields(简单二维RMQ)
    思路:二维RMQ#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintMAXN=255;intval[MAXN][MAXN];intdmin[MAXN][MAXN][8][8];intdmax[MAXN][MAXN][8][8];voidinitRMQ(intn,intm){for(inti=1;i<=n;i++)......
  • pojo层
    Answerpackagecom.example.academicadministration.pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importjava.sql.Timestamp;@Data@AllArgsConstructor@NoArgsConstructorpublicclassAnswer{privateStr......
  • Java开发手册中为什么禁止使用isSuccess作为布尔类型变量名以及POJO中基本类型与包装
    场景Java开发手册中关于POJO的布尔类型的变量名的要求是:【强制】POJO类中的任何布尔类型的变量,都不要加is前缀,否则部分框架解析会引起序列化错误。说明:在本文MySQL规约中的建表约定第一条,表达是与否的变量采用is_xxx的命名方式,所以,需要在<resultMap>设置从is_xxx到......
  • [USACO07JAN] Balanced Lineup G(树状数组)
    题目大意:给出长度为n的数组和q个询问,每次问(x,y)区间内最大值和最小值的差是多少思路:1.适合用树状数组做此区间求值,首先要明白普通的树状数组的tree[x]表示区间(x-(x&-x),x]的区间和,现在改为求最值,则tree[x]表示为区间(x-(x&-x),x]的最值,建树部分稍作改变即可,询问部分......
  • POJO简介【pojo模块】
    DTO(DataTransferObject):数据传输对象,用于接收数据和传输数据,属性和请求参数对应。VO(ViewObject):视图对象,返回给客户端展示用的数据,例如分页对象PageResult{total,List}。PO(PersistantObject):持久化对象,对象属性和数据库表中的字段一一对应,一张表对应一个PO。POJO(PlainOrdi......
  • POJ3608(旋转卡壳--求两凸包的最近点对距离)
    题目:BridgeAcrossIslands  考虑如下的算法,算法的输入是两个分别有m和n个顺时针给定顶点的凸多边形P和Q。1.计算P上y坐标值最小的顶点(称为yminP)和Q上y坐标值最大的顶点(称为ymaxQ)。2.为多边形在yminP和ymaxQ处构造两条切线LP和LQ使得他们对应的多边形位于他们的右侧......
  • SBT模版(Size Balanced Tree)
    关于SBT的介绍及学习,请戳这里。 SBT模版:/*************************************************数据结构:SBT(SizeBalancedTree),又称傻逼树;数据域:值域key,左孩子left,右孩子right,保持平衡的size;性质:每棵子树的大小不小于其兄弟的子树大小;插入:插入算法先简单插入节......