poj3264 Balanced Lineup——线段树
2017-08-01 20:12
393 查看
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 th
4000
e difference in height between the tallest and shortest cow in the range.
Sample Input
Sample Output
题意:给n个数字,q次询问,每次询问一个区间[a, b]内的最大值与最小值之差。
解析:很裸的一个线段树,而且不需要建好树之后都不用更新,只需查询。维护区间内的最大值和最小值即可。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 50001
struct node {
int l, r;
int Max, Min;
};
node a[N*4];
void push_up(int i){
a[i].Max = max(a[i<<1].Max, a[i<<1|1].Max);
a[i].Min = min(a[i<<1].Min, a[i<<1|1].Min);
}
void build(int l, int r, int i){
a[i].l = l;
a[i].r = r;
if(l == r){
int x;
scanf("%d", &x);
a[i].Max = a[i].Min = x;
return;
}
int mid = (l + r) >> 1;
build(l, mid, i<<1);
build(mid+1, r, i<<1|1);
push_up(i);
}
void query(int l, int r, int i, int &Max, int &Min){
if(a[i].l == l && a[i].r == r){
Max = a[i].Max;
Min = a[i].Min;
return;
}
int mid = (a[i].l + a[i].r)>>1;
if(l > mid) query(l, r, i<<1|1, Max, Min);
else if(r <= mid) query(l, r, i<<1, Max, Min);
else{
int Max0, Max1, Min0, Min1;
query(l, mid, i<<1, Max0, Min0);
query(mid+1, r, i<<1|1, Max1, Min1);
Max = max(Max0, Max1);
Min = min(Min0, Min1);
}
}
int main(){
int n, q, a, b, Max, Min;
while(~scanf("%d%d", &n, &q)){
build(1, n, 1);
for(int i = 0; i < q; i++){
scanf("%d%d", &a, &b);
if(a == b){
puts("0");
continue;
}
query(a, b, 1, Max, Min);
printf("%d\n", Max-Min);
}
}
return 0;
}
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 54929 | Accepted: 25726 | |
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 th
4000
e 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
题意:给n个数字,q次询问,每次询问一个区间[a, b]内的最大值与最小值之差。
解析:很裸的一个线段树,而且不需要建好树之后都不用更新,只需查询。维护区间内的最大值和最小值即可。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 50001
struct node {
int l, r;
int Max, Min;
};
node a[N*4];
void push_up(int i){
a[i].Max = max(a[i<<1].Max, a[i<<1|1].Max);
a[i].Min = min(a[i<<1].Min, a[i<<1|1].Min);
}
void build(int l, int r, int i){
a[i].l = l;
a[i].r = r;
if(l == r){
int x;
scanf("%d", &x);
a[i].Max = a[i].Min = x;
return;
}
int mid = (l + r) >> 1;
build(l, mid, i<<1);
build(mid+1, r, i<<1|1);
push_up(i);
}
void query(int l, int r, int i, int &Max, int &Min){
if(a[i].l == l && a[i].r == r){
Max = a[i].Max;
Min = a[i].Min;
return;
}
int mid = (a[i].l + a[i].r)>>1;
if(l > mid) query(l, r, i<<1|1, Max, Min);
else if(r <= mid) query(l, r, i<<1, Max, Min);
else{
int Max0, Max1, Min0, Min1;
query(l, mid, i<<1, Max0, Min0);
query(mid+1, r, i<<1|1, Max1, Min1);
Max = max(Max0, Max1);
Min = min(Min0, Min1);
}
}
int main(){
int n, q, a, b, Max, Min;
while(~scanf("%d%d", &n, &q)){
build(1, n, 1);
for(int i = 0; i < q; i++){
scanf("%d%d", &a, &b);
if(a == b){
puts("0");
continue;
}
query(a, b, 1, Max, Min);
printf("%d\n", Max-Min);
}
}
return 0;
}
相关文章推荐
- Balanced Lineup POJ - 3264(基础线段树)
- poj&nbsp;3264&nbsp;Balanced&nbsp;Lineup(线段树)
- POJ 3264 Balanced Lineup——线段树
- 查询线段树 Balanced Lineup POJ - 3264
- Balanced Lineup(POJ-3264)(线段树)
- poj 3264:Balanced Lineup(线段树,经典题)
- POJ_3264: Balanced Lineup线段树入门
- POJ 3264 Balanced Lineup RMQ / 线段树
- Balanced Lineup POJ - 3264 (线段树单点更新)
- poj 3264 Balanced Lineup——简单线段树
- Balanced Lineup POJ - 3264(线段树寻找区间最大值最小值)
- poj 3264 Balanced Lineup@
- E - Balanced Lineup POJ - 3264
- poj 3264 Balanced Lineup
- poj-3264 Balanced Lineup *
- POJ 3264 Balanced Lineup RMQ问题的ST解法
- Balanced Lineup POJ - 3264
- Balanced Lineup poj 3264
- POJ 3264 Balanced Lineup .
- POJ 3264 Balanced Lineup RMQ