Poj 3264 Balanced Lineup ( 线段树
来源:互联网 发布:http 服务器默认端口 编辑:程序博客网 时间:2024/06/09 13:50
Balanced Lineup
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 31734251 54 62 2
Sample Output
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Hint
630
题意
求区间的最大最小值
题解:
板子 好像写的太麻烦了唉
AC代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 200000*4+10;struct node{ int left, right; int sum, num;}tree[N];int arr[N];void build(int l,int r,int step){ tree[step].left = l; tree[step].right = r; tree[step].sum = tree[step].num = 0; if(l == r) { tree[step].sum = tree[step].num = arr[l]; return ; } int mid = (l+r) >> 1; build(l,mid,step<<1); build(mid+1,r,step<<1|1); tree[step].sum = max(tree[step<<1].sum,tree[step<<1|1].sum); tree[step].num = min(tree[step<<1].num,tree[step<<1|1].num);}int query_1(int l, int r,int step){ if(l==tree[step].left && r==tree[step].right) return tree[step].sum; int mid = (tree[step].left+tree[step].right) >> 1; if(r <= mid) return query_1(l,r,step<<1); if(l > mid) return query_1(l,r,step<<1|1); else return max(query_1(l,mid,step<<1),query_1(mid+1,r,step<<1|1));}int query_2(int l, int r,int step){ if(l==tree[step].left && r==tree[step].right) return tree[step].num; int mid = (tree[step].left+tree[step].right) >> 1; if(r <= mid) return query_2(l,r,step<<1); if(l > mid) return query_2(l,r,step<<1|1); else return min(query_2(l,mid,step<<1),query_2(mid+1,r,step<<1|1));}int main(){ int n, m; char ch; while(~scanf("%d%d",&n,&m)) { for(int i = 1;i <= n; i++) { scanf("%d",&arr[i]); } build(1,n,1); int u, v; while(m--) { scanf("%d%d",&u,&v); printf("%d\n",query_1(u,v,1)-query_2(u,v,1)); } }return 0;}
- poj 3264 Balanced Lineup rmq/线段树
- POJ 3264 Balanced Lineup RMQ / 线段树
- POJ 3264 Balanced Lineup 线段树基础
- poj 3264 Balanced Lineup[线段树,,水]
- 【线段树】 POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup 基础线段树
- poj 3264 Balanced Lineup(线段树)
- POJ 3264 Balanced Lineup 线段树
- Poj 3264 Balanced Lineup - 线段树
- POJ 3264 - Balanced Lineup(线段树)
- POJ 3264 Balanced Lineup (RMQ线段树)
- POJ 3264 Balanced Lineup 线段树RMQ
- [线段树] POJ 3264 balanced lineup
- poj 3264 Balanced Lineup 线段树
- POJ 3264 Balanced Lineup //简单线段树
- poj 3264 Balanced Lineup(线段树)
- POJ 3264 Balanced Lineup( 线段树&&RMQ )
- POJ 3264 Balanced Lineup 线段树
- 课程大纲
- C
- Membership Service Providers (MSP)
- 【ARM】Exynos 4412 中断理论分析
- GNU Autotools构建系统
- Poj 3264 Balanced Lineup ( 线段树
- 用C++设计一个不能被继承的类
- Deeper Depth Prediction with Fully Convolutional Residual Networks
- 2017/7/31训练日记(并查集基础)
- Java_5类型转换
- 面试常见的css问题
- python
- 杭电acm—2073 无限的路
- service命令