POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 53703 | Accepted: 25237 | |
Case Time Limit: 2000MS |
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 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
Source
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 #define maxsize 200020 6 typedef struct 7 { 8 int left,right; 9 int maxn; 10 int minn; 11 }Node; 12 int n,m; 13 int Max,Min; 14 int num[maxsize]; 15 Node tree[maxsize*20]; 16 inline void buildtree(int root,int left,int right)// 构建线段树 17 { 18 int mid; 19 tree[root].left=left; 20 tree[root].right=right;// 当前节点所表示的区间 21 if(left==right)// 左右区间相同,则此节点为叶子,max 应储存对应某个学生的值 22 { 23 tree[root].maxn=num[left]; 24 tree[root].minn=num[left]; 25 return; 26 } 27 mid=(left+right)/2; 28 //int a,b;// 递归建立左右子树,并从子树中获得最大值 29 buildtree(2*root,left,mid); 30 buildtree(2*root+1,mid+1,right); 31 tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn); 32 tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn); 33 } 34 inline void find(int root,int left,int right)// 从节点 root 开始,查找 left 和 right 之间的最大值 35 { 36 int mid; 37 //if(tree[root].left>right||tree[root].right<left)// 若此区间与 root 所管理的区间无交集 38 //return; 39 if(left==tree[root].left&&tree[root].right==right)// 若此区间包含 root 所管理的区间 40 { 41 Max=max(tree[root].maxn,Max); 42 Min=min(tree[root].minn,Min); 43 return; 44 } 45 mid=(tree[root].left+tree[root].right)/2; 46 if(right<=mid) 47 find(root*2,left,right); 48 else if(left>mid) 49 find(root*2+1,left,right); 50 else 51 { 52 find(root*2,left,mid); 53 find(root*2+1,mid+1,right); 54 //tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn); 55 //tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn); 56 //return; 57 } 58 } 59 60 int main() 61 { 62 //char c; 63 int i; 64 int x,y; 65 //scanf("d%d",&n,&m); 66 while(scanf("%d%d",&n,&m)!=EOF) 67 { 68 for(i=1;i<=n;i++) 69 scanf("%d",&num[i]); 70 buildtree(1,1,n); 71 for(i=1;i<=m;i++) 72 { 73 //getchar(); 74 Max=-99999999999; 75 Min= 99999999999; 76 scanf("%d%d",&x,&y); 77 //if(c=='Q') 78 //printf("%d\n",find(1,x,y)); 79 //else 80 //{ 81 // num[x]=y; 82 // update(1,x,y); 83 //} 84 find(1,x,y); 85 printf("%d\n",Max-Min); 86 } 87 } 88 return 0; 89 }
转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/7133096.html
POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】相关推荐
- POJ 3264 Balanced Lineup
POJ 3264 Balanced Lineup 题目链接 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00 ...
- poj 3264 Balanced Lineup RMQ问题 线段树
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- POJ 3264 Balanced Lineup 【线段树】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...
- POJ 3264 Balanced Lineup
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53629 Accepted: 25223 ...
- POJ 3264: Balanced Lineup
2019独角兽企业重金招聘Python工程师标准>>> 题目在此 解题思路:查询区间最大值/最小值之差,最基础的线段树应用. 代码: #include <cstdio>/ ...
- POJ 3264 Balanced Lineup(RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 24349 Accepted: 11348 ...
- 【RMQ】POJ 3264 Balanced Lineup
前言 这题出现在RMQRMQRMQ的WordWordWord文档中,我就直接把以前ACACAC过的程序交上去,结果WAWAWA了,然后才发现这道题还要求最小值... 链接 http://poj.org ...
- POJ 3264 Balanced Lineup (RMQ)
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- 卿学姐与公主(线段树区间求最大值)
A - 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
最新文章
- 有源淹没分析arcgis_基于ArcGIS的洪水淹没分析与三维模拟
- 编程python用什么软件比较好-新手入门Python编程的8个实用建议
- 持续集成、持续交付(CI/CD)开篇,先来唠唠嗑
- 怎么看服务器上jdk安装位置,查看云服务器jdk安装路径
- 叮咚周边优惠卡券小程序V6.0.4 完整安装包
- 【HDU1754】I HATE IT,线段树练习
- python编程入门教程100例_Python3入门经典100例(60-70)
- 24. 使用GitHub
- html页面打开字都有蓝色背景,为什么有些网页打不开?网页空白页、白底蓝字问题怎么解决?...
- 九九乘法表c语言四种,C语言实现九九乘法表(四种情况)
- Appium系列教程
- rtmp直播推流软件推荐
- ARM与裸机开发教程
- 推广链接生成html操作流程,拼多多生成商城推广链接接口
- 决策树算法——ID3算法,C4.5算法
- 负反馈放大器电路详解
- android qq截屏快捷键是什么,手机截屏的快捷键是什么,超过3种截图的快捷键操作方法!...
- 完整的大数据知识体系,大数据学习路线图
- 【HTTP】请求方法
- html进行语音播报,JQuery插件制作:[2]语音播报jspeech
热门文章
- Source Insight常用快捷键
- 《走着瞧》:另类的知青电影
- 如何从ios酷我音乐盒中导出已下载的音乐文件(使用Java编程实现)
- 转载:Windows CE内存管理
- 度秘语音引擎app_语音机器人哪家强 度秘/小冰/Siri/小娜横向评测
- 错误:不能继续进行下一步操作 openfire 设置._如何为MacBook或Mac电脑恢复出厂设置...
- MKL学习——矩阵向量操作
- 《神经网络:回到未来》(Neural Nets Back to the Future)-ICML 2016
- AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
- 为什么mysql 5.7.24启停不显示错误信息?log-error_verbosity参数