POJ 3264 Balanced Lineup //简单线段树

港控/mmm° 2022-09-20 15:24 234阅读 0赞








Balanced Lineup


















Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 33405   Accepted: 15671
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

  1. 6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

Sample Output

  1. 6
    3
    0

Source


[Submit] [Go Back] [Status] [Discuss]

home.gifHome Page goback.gifGo Back top.gifTo top

  1. #include <stdio.h>
  2. struct node
  3. {
  4. int l, r;
  5. int max, min;
  6. }tree[200005];
  7. int h[50004];
  8. int max, min;
  9. int MAX(int a, int b)
  10. {
  11. if(a>b)
  12. return a;
  13. return b;
  14. }
  15. int MIN(int a, int b)
  16. {
  17. if(a<b)
  18. return a;
  19. return b;
  20. }
  21. void build(int l, int r, int root) //建树
  22. {
  23. int mid;
  24. tree[root].l = l; //将区间位置 赋给 树
  25. tree[root].r = r;
  26. if(l==r)
  27. {
  28. tree[root].max = tree[root].min = h[l];//当叶子结点区间长度为1时,赋值
  29. return ;
  30. }
  31. mid = (l + r)>>1; //除2
  32. build(l, mid, 2*root); //建立左子树
  33. build(mid+1, r, 2*root+1); //建立右子树
  34. tree[root].max = MAX(tree[root*2].max, tree[root*2+1].max);//当前结点的最大最小值等于他的 左子树与右子树 中的最大最小值。
  35. tree[root].min = MIN(tree[root*2].min, tree[root*2+1].min);
  36. }
  37. void findmax(int l, int r, int root)
  38. {
  39. int mid;
  40. if(tree[root].l==l && tree[root].r==r)//当查找的区间与结点区间吻合时,这区间的最大最小值就是这个结点的最大最小值。
  41. {
  42. max = MAX(max, tree[root].max);
  43. min = MIN(min, tree[root].min);
  44. return ;
  45. }
  46. mid = (tree[root].l + tree[root].r)>>1;
  47. if(mid >= r) //当查找的区间在此结点的 左子树时;
  48. findmax(l, r, root*2);
  49. else if(mid < l) //当查找的区间在此结点的 右子树时;
  50. findmax(l, r, root*2+1);
  51. else //当查找的区间横跨此结点的 左右子树时;
  52. {
  53. findmax(l, mid, root*2);
  54. findmax(mid+1, r, root*2+1);
  55. }
  56. }
  57. int main()
  58. {
  59. int n, q;
  60. int l, r;
  61. int i;
  62. scanf("%d%d", &n, &q);
  63. for(i=1; i<=n; i++)
  64. scanf("%d", &h[i]);
  65. build(1, n, 1); //从第1个结点开始建树。
  66. while(q--)
  67. {
  68. scanf("%d%d", &l, &r);
  69. max = -0xfffff; //赋初值
  70. min = 0xfffff;
  71. findmax(l, r, 1);
  72. printf("%d\n", max-min);
  73. }
  74. return 0;
  75. }

发表评论

表情:
评论列表 (有 0 条评论,234人围观)

还没有评论,来说两句吧...

相关阅读