Balanced Lineup(线段树)
来源:互联网 发布:数据库逻辑数据模型 编辑:程序博客网 时间:2024/05/01 23:48
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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
http://blog.csdn.net/xieshimao/article/details/6241399这篇文章的代码写的非常好,我直接看懂了,赞!
贴上自己的代码:里面有自己的注释。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <stack>#include <iomanip>using namespace std;void build(int l,int r,int root);int Max(int a,int b);int Min(int a,int b);void Findmax(int l,int r,int root);void Findmin(int l,int r,int root);#define inf 0x3f3f3f3f//相当于无穷大struct node{ int l; int r; int maxn; int minn;};node tree[200005];int h[50005];int maxn,minn;int main(){ int a,b,N,Q; scanf("%d%d",&N,&Q); for(int i=1;i<=N;i++) scanf("%d",&h[i]); build(1,N,1);//建立树 while(Q--) { maxn=0; minn=inf; scanf("%d%d",&a,&b); Findmax(a,b,1);//找最大值 Findmin(a,b,1);//找最小值 printf("%d\n",maxn-minn); } return 0;}//建立树,一步一步的自己画图。void build(int l,int r,int root){ tree[root].l=l; tree[root].r=r; if(l==r) { tree[root].maxn=h[l]; tree[root].minn=h[l]; return; } int mid=(l+r)/2; build(l,mid,root*2); build(mid+1,r,root*2+1); tree[root].maxn=Max(tree[2*root].maxn,tree[2*root+1].maxn); tree[root].minn=Min(tree[2*root].minn,tree[2*root+1].minn);}int Max(int a,int b){ if(a>b) return a; else return b;}int Min(int a,int b){ if(a>b) return b; else return a;}void Findmax(int l,int r,int root){ if(tree[root].l==l&&tree[root].r==r) { if(tree[root].maxn>maxn) maxn=tree[root].maxn; return; } int mid=(tree[root].l+tree[root].r)/2; if(mid>=r) Findmax(l,r,root*2); else if(mid<l) Findmax(l,r,root*2+1); else { Findmax(l,mid,root*2); Findmax(mid+1,r,root*2+1); }}void Findmin(int l,int r,int root){ if(tree[root].l==l&&tree[root].r==r) { if(tree[root].minn<minn) minn=tree[root].minn; return; } int mid=(tree[root].l+tree[root].r)>>1; if(mid>=r) Findmin(l,r,root*2); else if(mid<l) Findmin(l,r,root*2+1); else { Findmin(l,mid,root*2); Findmin(mid+1,r,root*2+1); }}
- Balanced Lineup(线段树)
- 线段树 Balanced Lineup
- Balanced Lineup (基本线段树)
- Balanced Lineup poj3264 线段树
- 3264 Balanced Lineup 线段树
- poj3264,Balanced Lineup,线段树
- poj3264 Balanced Lineup(线段树)
- poj3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup(线段树)
- Balanced Lineup(线段树)
- POJ3264 Balanced Lineup 【线段树】
- Balanced Lineup 线段树 RMQ
- pku 3264 Balanced Lineup(线段树)
- poj 3264 Balanced Lineup rmq/线段树
- pku 3264 Balanced Lineup线段树
- POJ3264 Balanced Lineup(线段树应用)
- POJ 3264 Balanced Lineup RMQ / 线段树
- POJ3264 Balanced Lineup 线段树基础
- poj 2533 Longest Ordered Subsequence
- 不接入sdk分享到第三方平台
- Android studio集成svn详细介绍
- spark-机器学习及SparkMLlib简介
- 《用tcp/ip进行网际互联》第9章 无分类和子网地址扩展(CIDR)
- Balanced Lineup(线段树)
- leetcode 328. Odd Even Linked List 16ms beats 96.25% c++
- 1.shell 编程(1) --- 简介
- day07_shutil模块学习
- tjut 2222
- 移动页面HTML5自适应手机屏幕宽度
- UVA1583 UVALive3355 Digit Generator
- C++中string和int之间的转换
- 字体设计规范