POJ 3264 Balanced Lineup (线段树)
来源:互联网 发布:midi软件哪个好 编辑:程序博客网 时间:2024/05/23 01:29
Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
POJ 3264
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 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
线段树水题。节点里维护一个区间最大值和最小值。最后统计一下做个差就好了。
#include "iostream"#include "string.h"#include "cstdio"#include "algorithm"using namespace std;int num[50005];struct node{ int maxx,minn; int left,right;}tree[50005*4];int mymax,mymin;void build(int root,int lf,int rt){ tree[root].maxx=0; tree[root].minn=1000005; tree[root].left=lf; tree[root].right=rt; if(lf==rt) { tree[root].maxx=tree[root].minn=num[lf]; return; } int mid=(lf+rt)/2; build(root*2,lf,mid); build(root*2+1,mid+1,rt); tree[root].minn=min(tree[root].minn,tree[root*2].minn); tree[root].minn=min(tree[root].minn,tree[root*2+1].minn); tree[root].maxx=max(tree[root].maxx,tree[root*2].maxx); tree[root].maxx=max(tree[root].maxx,tree[root*2+1].maxx); return;}void find(int root,int lf,int rt){ if(mymin<=tree[root].minn&&mymax>=tree[root].maxx) return; if(lf<=tree[root].left&&rt>=tree[root].right) { mymax=max(mymax,tree[root].maxx); mymin=min(mymin,tree[root].minn); return; } if(tree[root].left==tree[root].right) return; int mid=(tree[root].left+tree[root].right)/2; if(rt<=mid) find(root*2,lf,rt); else if(lf>mid) find(root*2+1,lf,rt); else { find(root*2,lf,mid); find(root*2+1,mid+1,rt); } return;}int main(){ int n,q; while(~scanf("%d%d",&n,&q)) { for(int i=1;i<=n;i++) { scanf("%d",&num[i]); } build(1,1,n); int a,b; for(int i=1;i<=q;i++) { mymax=0; mymin=1000005; scanf("%d%d",&a,&b); find(1,a,b); printf("%d\n",mymax-mymin); } }}
- 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++教程(十七:Classes (I))
- LeetCode 412. Fizz Buzz
- 一款轻量的微信公众号组件
- scala-正则表达式
- VS下多项目管理与依赖项目作用
- POJ 3264 Balanced Lineup (线段树)
- isnull()和判断char类型是否为空以及trim()的使用
- 媒体查询
- 人脸识别说明
- C语言中堆和栈的区别
- 2016-10-20 会议总结及感悟
- Google图片加载库Glide的简单封装GlideUtils
- C++ fstream读写文件
- JAVA常见面试问题以及答案