kuangbin——线段树专题 F - Balanced Lineup
来源:互联网 发布:淘宝网婴幼儿童车 编辑:程序博客网 时间:2024/06/11 22:45
F - Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluDescription
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.
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 31734251 54 62 2
Sample Output
630
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;#define M 50005#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef struct SegTreeNode{ int maxx; int minn;}segtree;segtree arr[M<<2];void build(int l,int r,int rt){ if(l == r){ scanf("%d",&arr[rt].maxx); arr[rt].minn = arr[rt].maxx; return; } int m = (l+r)>>1; build(lson); build(rson); arr[rt].maxx = max(arr[rt<<1].maxx,arr[rt<<1|1].maxx); arr[rt].minn = min(arr[rt<<1].minn,arr[rt<<1|1].minn);}int query_minn(int L,int R,int l,int r ,int rt){ if(L<=l && r<=R) { return arr[rt].minn; } int m = (l + r) >>1; int ret = INF; if(L<=m) ret = min(ret,query_minn(L,R,lson)); if(R>m) ret = min(ret,query_minn(L,R,rson)); return ret;}int query_maxx(int L,int R,int l,int r ,int rt){ if(L<=l && r<=R) { return arr[rt].maxx; } int m = (l + r) >>1; int ret = 0; if(L<=m) ret = max(ret,query_maxx(L,R,lson)); if(R>m) ret = max(ret,query_maxx(L,R,rson)); return ret;}int main(int argc,char *argv[]){ int a,b,n,m; while(scanf("%d %d",&n,&m) == 2) { memset(arr,0,sizeof(arr)); build(1,n,1); char op[10]; for(int i=1;i<=m;i++) { scanf("%d %d",&a,&b); printf("%d\n",abs(query_maxx(a,b,1,n,1)-query_minn(a,b,1,n,1))); } } return 0;}
0 0
- kuangbin——线段树专题 F - Balanced Lineup
- nbut线段树专题I - Balanced Lineup
- poj3264 Balanced Lineup——线段树
- Balanced Lineup(线段树)
- 线段树 Balanced Lineup
- POJ3264——Balanced Lineup(线段树)
- Balanced Lineup(线段树——根据区间找最值)
- POJ 3264 Balanced Lineup——线段树
- Balanced Lineup(线段树—指针实现)
- [kuangbin带你飞]专题七 线段树 F
- Balanced Lineup (基本线段树)
- Balanced Lineup poj3264 线段树
- 3264 Balanced Lineup 线段树
- poj3264,Balanced Lineup,线段树
- poj3264 Balanced Lineup(线段树)
- poj3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup(线段树)
- Balanced Lineup(线段树)
- leetcode_c++: Regular Expression Matching(010)
- 从Windows到Linux(二)
- js函数
- node-webkit js 复制粘贴
- C++之stl::string写时拷贝导致的问题
- kuangbin——线段树专题 F - Balanced Lineup
- 安卓百分比布局之RelativeLayout
- C语言二维数组(第十天)
- MATLAB使用基础
- oracle查询转换在mysql中实现对比
- 使用DbUtils实现增删改查——ResultSetHandler接口的实现类
- 实现memcpy函数
- 什么是熵?
- 胜者树与败者树