poj 3264 线段树最值更新
2015-11-07 00:36
316 查看
Balanced Lineup
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
Sample Output
Source
USACO 2007 January Silver
题意:给你1-n的区间的值,给你m个区间,求区间的最大值-最小值。
题解:直接用线段树更新最大值,最小值就可以了,在这里我来解释一下在建树的时候是怎么更新区间的最值的,即Pushup函数的工作原理
先序遍历先建左子树,后建右子树如图,
向上更新
在这里我写错了一句错了10次
if (L == tree[rt].lc&&tree[rt].rc == R)
{
ans = max(ans, (tree[rt].max - tree[rt].min));
return;
}我理解为返回的是查找到的区间最大值-最小值,但是没有意识到当前查找到的区间的最值并不一定是整个区间的最值,后来查了题解才想到改为
if (L == tree[rt].lc&&tree[rt].rc == R)
{
Max= max(Max, tree[rt].max);
Min = min(Min, tree[rt].min);
return;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 101000
#define inf 0x3f3f3f3f
#define LL long long
LL Min,Max;
struct point
{
int lc, rc, va;
LL min, max;
int mid()
{
return (lc + rc) >> 1;
}
}tree[N << 2];
LL max(LL a, LL b)
{
return a>b ? a : b;
}
LL min(LL a, LL b)
{
return a<b ? a : b;
}
void Pushup(int rt)
{
tree[rt].max = max(tree[rt << 1].max, tree[rt << 1 | 1].max);
tree[rt].min = min(tree[rt << 1].min, tree[rt << 1 | 1].min);
}
void build(int L, int R, int rt)
{
tree[rt].lc = L;
tree[rt].rc = R;
if (tree[rt].lc== tree[rt].rc)
{
scanf("%lld", &tree[rt].va);
tree[rt].max = tree[rt].va;
tree[rt].min = tree[rt].va;
return;
}
int mid = (L + R) >> 1;
build(L, mid, rt << 1);
build(mid + 1, R, rt << 1 | 1);
Pushup(rt);
}
void query(int L, int R, int rt)
{
if (L == tree[rt].lc&&tree[rt].rc == R)
{
Max= max(Max, tree[rt].max);
Min = min(Min, tree[rt].min);
return;
}
int mid = tree[rt].mid();
if (R <= mid)query(L, R, rt << 1);
else if (L>mid)query(L, R, rt << 1 | 1);
else
{
query(L, mid, rt << 1);
query(mid + 1, R, rt << 1 | 1);
}
}
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
#endif
int a, b;
int n, m;
while (~scanf("%d%d", &n, &m))
{
build(1, n, 1);
while (m--)
{
Max=-(inf);
Min = inf;
scanf("%d%d", &a, &b);
query(a, b, 1);
printf("%lld\n", Max-Min);
}
}
return 0;
}
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 41295 | Accepted: 19386 | |
Case Time Limit: 2000MS |
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
USACO 2007 January Silver
题意:给你1-n的区间的值,给你m个区间,求区间的最大值-最小值。
题解:直接用线段树更新最大值,最小值就可以了,在这里我来解释一下在建树的时候是怎么更新区间的最值的,即Pushup函数的工作原理
先序遍历先建左子树,后建右子树如图,
向上更新
在这里我写错了一句错了10次
if (L == tree[rt].lc&&tree[rt].rc == R)
{
ans = max(ans, (tree[rt].max - tree[rt].min));
return;
}我理解为返回的是查找到的区间最大值-最小值,但是没有意识到当前查找到的区间的最值并不一定是整个区间的最值,后来查了题解才想到改为
if (L == tree[rt].lc&&tree[rt].rc == R)
{
Max= max(Max, tree[rt].max);
Min = min(Min, tree[rt].min);
return;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 101000
#define inf 0x3f3f3f3f
#define LL long long
LL Min,Max;
struct point
{
int lc, rc, va;
LL min, max;
int mid()
{
return (lc + rc) >> 1;
}
}tree[N << 2];
LL max(LL a, LL b)
{
return a>b ? a : b;
}
LL min(LL a, LL b)
{
return a<b ? a : b;
}
void Pushup(int rt)
{
tree[rt].max = max(tree[rt << 1].max, tree[rt << 1 | 1].max);
tree[rt].min = min(tree[rt << 1].min, tree[rt << 1 | 1].min);
}
void build(int L, int R, int rt)
{
tree[rt].lc = L;
tree[rt].rc = R;
if (tree[rt].lc== tree[rt].rc)
{
scanf("%lld", &tree[rt].va);
tree[rt].max = tree[rt].va;
tree[rt].min = tree[rt].va;
return;
}
int mid = (L + R) >> 1;
build(L, mid, rt << 1);
build(mid + 1, R, rt << 1 | 1);
Pushup(rt);
}
void query(int L, int R, int rt)
{
if (L == tree[rt].lc&&tree[rt].rc == R)
{
Max= max(Max, tree[rt].max);
Min = min(Min, tree[rt].min);
return;
}
int mid = tree[rt].mid();
if (R <= mid)query(L, R, rt << 1);
else if (L>mid)query(L, R, rt << 1 | 1);
else
{
query(L, mid, rt << 1);
query(mid + 1, R, rt << 1 | 1);
}
}
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
#endif
int a, b;
int n, m;
while (~scanf("%d%d", &n, &m))
{
build(1, n, 1);
while (m--)
{
Max=-(inf);
Min = inf;
scanf("%d%d", &a, &b);
query(a, b, 1);
printf("%lld\n", Max-Min);
}
}
return 0;
}
相关文章推荐
- 64位ubuntu14.04配置adb后提示没有那个文件或目录
- 64位ubuntu14.04配置adb后提示没有那个文件或目录
- heartbeat介绍以及通过heartbeat实现简单httpd高可用
- Ubuntu 安装 Latex 已及中文包
- Win7系统下如何配置Tomcat支持PHP和Perl
- 没有躲过的坑--头文件相互包含(Compiler error C2653: not a class or namespace name)
- 没有躲过的坑--头文件相互包含(Compiler error C2653: not a class or namespace name)
- 对于ppt设计的一些感悟
- TITLE: UVA 11292 Dragon of Loowater
- iOS监听键盘事件
- Properties类
- Linux命令 使用记录
- java入门程序100例学习笔记(008黑色星期五)
- 单源点最短路径问题(Dijkstra算法)
- Day2(11.1):相关代码
- java 工厂模式 详解
- linux之目录管理
- sass用法
- 三种光照模型的计算
- DB2在Linux下的安装