POJ 3264 简单线段树
来源:互联网 发布:java 正则 去除中括号 编辑:程序博客网 时间:2024/04/29 03:04
纠结了一天半的时间,写出来一道线段树+lazy的题目后,再看这道题,当真是水之又水啊,,基本就是道模板题啊。。建树的时候存下来区间内的最大值,最小值,查找的时候,找到最大值,最小值,直接相减就可以了。。题目:
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
630ac代码:
#include <iostream>#include <cstdio>using namespace std;const int N=50010;struct tree{int left,right,maxvalue,minvalue;}tt[N*4];int n,numask,height[N];int max(int x,int y){return x>y?x:y;}int min(int x,int y){return x<y?x:y;}void built_tree(int lp,int rp,int pos){tt[pos].left=lp;tt[pos].right=rp;if(lp==rp){tt[pos].maxvalue=height[lp];tt[pos].minvalue=height[rp];return ;}int mid=(tt[pos].left+tt[pos].right)/2;built_tree(lp,mid,2*pos);built_tree(mid+1,rp,2*pos+1);tt[pos].maxvalue=max(tt[2*pos].maxvalue,tt[2*pos+1].maxvalue);tt[pos].minvalue=min(tt[2*pos].minvalue,tt[2*pos+1].minvalue);}int findmax(int lp,int rp,int pos){if(tt[pos].left==lp&&tt[pos].right==rp){return tt[pos].maxvalue;}int mid=(tt[pos].left+tt[pos].right)/2;if(rp<=mid){ return findmax(lp,rp,2*pos);}else if(lp>mid)return findmax(lp,rp,2*pos+1);else{ return max( findmax(lp,mid,2*pos), findmax(mid+1,rp,2*pos+1));}}int findmin(int lp,int rp,int pos){if(tt[pos].left==lp&&tt[pos].right==rp){return tt[pos].minvalue;}int mid=(tt[pos].left+tt[pos].right)/2;if(rp<=mid)return findmin(lp,rp,2*pos);else if(lp>mid)return findmin(lp,rp,2*pos+1);elsereturn min( findmin( lp,mid,2*pos ),findmin( mid+1,rp,2*pos+1 ) );}int main(){//freopen("1.txt","r",stdin);while(~scanf("%d%d",&n,&numask)){ for(int i=1;i<=n;++i) scanf("%d",&height[i]); built_tree(1,n,1); int x,y; while(numask--){ scanf("%d%d",&x,&y);int mmax=findmax(x,y,1);int mmin=findmin(x,y,1);printf("%d\n",mmax-mmin); }}return 0;}
- POJ 3264 简单线段树
- POJ 3264 Balanced Lineup //简单线段树
- poj 3264 Balanced Lineup简单线段树
- poj 3264 Balanced Lineup(简单线段树)
- POJ 3264 Balanced Lineup(简单线段树)
- poj 3264 Balanced Lineup(简单线段树 或 rmq)
- poj 两道简单线段树 3264 && 3468
- POJ 3264 线段树
- poj(3264+线段树)
- POJ 3264 线段树
- POJ 3264 线段树
- POJ 3264 线段树
- poj 3264 线段树
- poj-3264-线段树
- poj 3468(简单线段树区间更新)
- POJ 2823 线段树简单操作
- poj 3264 线段树方法
- poj 3264 ( 线段树)
- socket编程——sockaddr_in结构体操作
- static_cast、dynamic_cast、reinterpret_cast、和const_cast
- 编译mtd-utils-1.4.5 (
- Java处理http协议相关初步(二)——httpserver
- android 中集成支付宝
- POJ 3264 简单线段树
- 查看宏展开后的内容
- 多重集合的排列和组合问题
- oracle opreation instruction(表空间,用户etc)
- DebugView打印日志
- 第九周任务一
- Mac OS X配置android过程中遇到“R cannot be resolved to a variable”问题的解决
- 关于W3C验证
- input子系统 KeyPad-Touch上报数据格式与机制