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
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
Source
题目链接:http://poj.org/problem?id=3264
分析:线段树求最大值和最小值,然后最大值减去最小值即为正解!貌似这题好像有暴力写法?
下面给出AC代码:
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("%dn",find(1,x,y));
79 //else
80 //{
81 // num[x]=y;
82 // update(1,x,y);
83 //}
84 find(1,x,y);
85 printf("%dn",Max-Min);
86 }
87 }
88 return 0;
89 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android内存优化 | LeakCanary/Profiler & 非静态内部类耗时操作 实战分析
- 使用keycloak实现k8s用户权限的统一管理
- python魔法方法是什么
- 如何同步上游分支代码?
- 在 Pycharm 中安装及使用 Jupyter (图文详解)
- 【经验分享】如何使用keras进行多主机分布式训练
- 分享一种接口的日志格式
- Python 基础(二):基本语句
- javaScript代码飘红报错看不懂?读完这篇文章再试试!
- Synchronized简述
- PythonforResearch | 2_数据处理
- 程序员过关斩将--Http请求中如何保持状态?
- 如何有效恢复误删的HDFS文件
- 别再用OFFSET和LIMIT分页了
- 别再用大小比较时间了