[NWPU][2014][TRN][13]线段树第一讲 B - 基础 POJ 3264
2014-09-06 20:33
323 查看
B - 基础
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
3264
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:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
3264
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> using namespace std; const int MAXN = 50005; int h[50005], MAX, MIN; struct node { int l, r; int MAX, MIN; }tree[MAXN * 4]; void build(int p, int l, int r) { tree[p].l = l; tree[p].r = r; if(l ==r) {tree[p].MAX = tree[p].MIN = h[l]; return ; } int mid = (l + r) / 2; build(p * 2, l, mid); build(p * 2 + 1, mid + 1, r); //tree[p] = tree[p * 2] + tree[p * 2 + 1]; tree[p].MAX = max(tree[p * 2].MAX, tree[p * 2 + 1].MAX); tree[p].MIN = min(tree[p * 2].MIN, tree[p * 2 + 1].MIN ); } void find(int l, int r, int p) { if(tree[p].l == l && tree[p].r == r) { MAX = max(MAX, tree[p].MAX); MIN = min(MIN, tree[p].MIN); return ; } int mid = (tree[p].r + tree[p].l) / 2; if(mid >= r) find(l, r, p * 2); else if(mid < l) find(l, r, p * 2 + 1); else { find(l, mid, p * 2); find(mid + 1, r, p * 2 + 1); } } int main() { int n, p; //cin >> n >> p; scanf("%d%d", &n, &p); for(int i = 1; i <= n; i++) { //cin >> h[i]; scanf("%d", &h[i]); } build(1, 1, n); int x, y; while(p--) { //cin >> x >> y; scanf("%d%d", &x, &y); MAX = -0xfffff; MIN = 0xfffff; find(x, y, 1); //cout << MAX - MIN << endl; printf("%d\n", MAX - MIN); } return 0; }
相关文章推荐
- [NWPU][2014][TRN][13]线段树第一讲 A - 基础 POJ 2352
- 7-12 [NWPU][2014][TRN][3]搜索 D - 广搜 基础 POJ 1915
- 7.12 [NWPU][2014][TRN][3]搜索 POJ 1562 H - 深搜/广搜 基础
- [NWPU][2014][TRN][5]二分和贪心 M - 贪心 基础 POJ 2709
- [NWPU][2014][TRN][17]最小生成树 B - 基础 POJ 2421
- poj 3264 Balanced Lineup(基础线段树)
- 7.12 [NWPU][2014][TRN][4]搜索 C - 广搜 基础
- [NWPU][2014][TRN][16]图论拓扑排序 H - 基础
- [NWPU][2014][TRN][22]RMQ和LCA C - RMQ POJ 3264
- poj 3264线段树基础
- poj 3264 Balanced Lineup(基础线段树)
- POJ3264 Balanced Lineup 线段树基础
- poj 3264 Balanced Lineup(基础线段树)
- Poj3264 Balanced Lineup 线段树基础题
- poj 3264 Balanced Lineup(基础线段树)
- poj 3264 Balanced Lineup(基础线段树)
- Balanced Lineup POJ - 3264(基础线段树)
- POJ 3264 线段树 基础入门
- [NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
- [NWPU][2014][TRN][18]最短路问题 A - 模板 POJ 2387