POJ 3264.Balanced Lineup【線分樹】【4月28】
Balanced Lineup
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 43653
Accepted: 20473
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
Sample Output
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 43653
Accepted: 20473
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
は、基本的なセグメントツリーであり、更新操作はありません.#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 50005;
struct ss
{
int left, right, minh, maxh;
}segtree[MAX*4];
int N, Q, x, y, maxn, minx;
void build(int node, int l, int r)
{
segtree[node].left = l;
segtree[node].right = r;
if(l == r)
{
scanf("%d", &segtree[node].minh);
segtree[node].maxh = segtree[node].minh;
return;
}
build(node*2, l, (l+r)/2);
build(node*2+1, (l+r)/2+1, r);
segtree[node].minh = min(segtree[node*2].minh, segtree[node*2+1].minh);
segtree[node].maxh = max(segtree[node*2].maxh, segtree[node*2+1].maxh);
}
void query(int node, int l, int r)
{
if(l > segtree[node].right || r < segtree[node].left) return;
if(l <= segtree[node].left && r >= segtree[node].right)
{
minx = min(minx, segtree[node].minh);
maxn = max(maxn, segtree[node].maxh);
return;
}
query(node*2, l, r);
query(node*2+1, l ,r);
}
int main()
{
scanf("%d %d", &N, &Q);
build(1, 1, N);
while(Q--)
{
minx = 0x7FFFFFFF;
maxn = 0;
scanf("%d %d", &x, &y);
query(1, x, y);
cout << maxn-minx << endl;
}
return 0;
}