Poj3264 Balanced Lineup 线段树基础题
来源:互联网 发布:阿里旅行软件 编辑:程序博客网 时间:2024/06/07 07:58
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
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
Sample Input
6 31734251 54 62 2
Sample Output
630
Source
USACO 2007 January Silver
求一个区间内最大值与最小值的差值,用线段树去维护即可,包括三个操作,建树,插入元素,查找。本题用数组来实现会比较方便。
对于线段树上任意一个结点,所需要记录的是这段区间内的最大最小值。
#include <iostream>#include <cstdio>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define INF 0x3f3f3f3ftypedef long long LL;int maxn,minn;struct node{ int l,r; int ma,mi; int mid(){ return (l+r)/2; }};node tree[800005];void buildtree(int root,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].mi=INF; tree[root].ma=-INF; if(l!=r){ buildtree(2*root+1,l,(l+r)/2); buildtree(2*root+2,(l+r)/2+1,r); }}void vinsert(int root,int i,int v){ if(tree[root].l==tree[root].r){ tree[root].mi=tree[root].ma=v; return; } tree[root].ma=max(tree[root].ma,v); tree[root].mi=min(tree[root].mi,v); if(i<=tree[root].mid()){ vinsert(2*root+1,i,v); }else{ vinsert(2*root+2,i,v); }}void answersearch(int root,int l,int r){ if(tree[root].mi>=minn&&tree[root].ma<=maxn){ return; } if(tree[root].l==l&&tree[root].r==r){ minn=min(minn,tree[root].mi); maxn=max(maxn,tree[root].ma); return; } if(r<=tree[root].mid()){ answersearch(2*root+1,l,r); }else if(l>tree[root].mid()){ answersearch(2*root+2,l,r); }else{ answersearch(2*root+1,l,tree[root].mid()); answersearch(2*root+2,tree[root].mid()+1,r); }}int main(){ int n,m,v,a,b; scanf("%d%d",&n,&m); buildtree(0,1,n); for(int i=1;i<=n;i++){ scanf("%d",&v); vinsert(0,i,v); } while(m--){ maxn=-INF; minn=INF; scanf("%d%d",&a,&b); answersearch(0,a,b); printf("%d\n",maxn-minn); } return 0;}
- Poj3264 Balanced Lineup 线段树基础题
- POJ3264 Balanced Lineup 线段树基础
- POJ3264--Balanced Lineup(线段树模板题)
- Balanced Lineup poj3264 线段树
- poj3264,Balanced Lineup,线段树
- poj3264 Balanced Lineup(线段树)
- poj3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup 【线段树】
- POJ3264 Balanced Lineup(线段树应用)
- Balanced Lineup(poj3264,线段树入门)
- POJ3264 Balanced Lineup 【线段树】+【单点更新】
- POJ3264 Balanced Lineup RMQ 线段树
- POJ3264 Balanced Lineup(线段树入门)
- POJ3264 Balanced Lineup 线段树|ST表
- 线段树之POJ3264 Balanced Lineup
- POJ3264 Balanced Lineup 线段树||RMQ
- 【POJ3264】Balanced Lineup,线段树入门
- MTK6580-Psensor hal层驱动分析
- C++析构函数为什么要为虚函数
- 无坑安装使用godep(golang.org/x/tools/go/vcs的错误解决方案)
- Java 访问修饰符的浅谈
- 字符串的encode与decode解决乱码问题
- Poj3264 Balanced Lineup 线段树基础题
- La 4080 Warfare and logistics Dijkstra
- TOJ 3002.Vote [Chinese]
- java本地化
- (ios)UIScrollView和UIPageControl来实现app的欢迎界面
- SpringMVC 拦截器实现原理跟登录实现
- Web开发之-JSP学习总结-第三篇: JSTL标签
- 排序链表去重
- Android体系结构和虚拟机