1636: [Usaco2007 Jan]Balanced Lineup

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 772  Solved: 560线段树裸题。。。

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.

每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别.

注意: 在最大数据上, 输入和输出将占用大部分运行时间. 

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.

第1行:N,Q
第2到N+1行:每头牛的身高
第N+2到N+Q+1行:两个整数A和B,表示从A到B的所有牛。(1<=A<=B<=N)

Output

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

Sample Input

* 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 Output

6
3
0
 

  1. #include<cstdio>
  2. #include <iostream>
  3. #define M 50010
  4. int max(int a,int b){return a>b?a:b;}
  5. int min(int a,int b){return a<b?a:b;}
  6. struct tree{int l,r,minn,maxx;}tr[*M];
  7. int a[M];
  8. void make(int l,int r,int p)
  9. {
  10. tr[p].l=l;
  11. tr[p].r=r;
  12. if(l==r){
  13. tr[p].minn=a[l];
  14. tr[p].maxx=a[l];
  15. return ;
  16. }
  17. int mid=(l+r)>>;
  18. make(l,mid,p<<);
  19. make(mid+,r,p<<|);
  20. tr[p].minn=min(tr[p<<].minn,tr[p<<|].minn);
  21. tr[p].maxx=max(tr[p<<].maxx,tr[p<<|].maxx);
  22. }
  23. int fmin(int l,int r,int x)
  24. {
  25. if(tr[x].l==l&&tr[x].r==r) return tr[x].minn;
  26. int mid=(tr[x].l+tr[x].r)>>,q=x<<;
  27. if(r<=mid) return fmin(l,r,q);
  28. else if(l>mid) return fmin(l,r,q+);
  29. else return min(fmin(l,mid,q),fmin(mid+,r,q+));
  30. }
  31. int fmax(int l,int r,int x)
  32. {
  33. if(tr[x].l==l&&tr[x].r==r) return tr[x].maxx;
  34. int mid=(tr[x].l+tr[x].r)>>;
  35. if(r<=mid) return fmax(l,r,x<<);
  36. else if(l>mid) return fmax(l,r,x<<|);
  37. else return max(fmax(l,mid,x<<),fmax(mid+,r,x<<|));
  38. }
  39. int main()
  40. {
  41. int n,m,i,x,y;
  42. scanf("%d%d",&n,&m);
  43. for(i=;i<=n;i++) scanf("%d",&a[i]);
  44. make(,n,);
  45. for(i=;i<m;i++){
  46. scanf("%d%d",&x,&y);
  47. printf("%d\n",fmax(x,y,)-fmin(x,y,));
  48. }
  49. }
 

bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树的更多相关文章

  1. BZOJ 1636: [Usaco2007 Jan]Balanced Lineup

    noip要来了,刷点基础水题. 题意: RMQ,给你N个数,Q个询问,每次查询[l,r]内,最大值减最小值是多少. 写的ST. 代码: #include<iostream> #includ ...

  2. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  3. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  4. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  5. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  6. BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

  7. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  8. 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...

  9. BZOJ1636: [Usaco2007 Jan]Balanced Lineup

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[ ...

随机推荐

  1. perl6中的hash定义(2)

    use v6; , :b, :!c; say %ha; say %ha<a>; #这里不能用%ha{a}, {a}表示调用a()函数了, 在perl6中, {}有特别函义 say %ha{ ...

  2. Linux后台研发面试题

    本系列给出了在复习过程中一些C++后台相关面试题,回答内容按照笔者的知识点掌握,故有些问题回答较为简略 1.信号的生命周期 一个完整的信号生命周期可以用四个事件刻画:1)信号诞生:2)信号在进程中注册 ...

  3. 設定 gpio 為 讀取用途,需注意的參數

    Schematic 解說 上面的 線路圖, R1 R2 只能有一個被接上, R3 R4 只能有一個被接上, 是使用 gpio 讀取 電壓 判斷為0 或是 1 這時的 gpio 設定,其中一個參數需設為 ...

  4. HDU-2487

    Ugly Windows Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. [PAT] 1146 Topological Order(25 分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  6. 小程序的一个tab切换

    <view class="tab-left" bindtap="tab"> <view class="{{tabArr.curHdI ...

  7. OpenCL学习笔记(一):摩尔定律,异构计算与OpenCL初印象

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld.  技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 关于摩尔定律: 摩尔定律19 ...

  8. hdu 3371(kruskal)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. Two Sum ——经典的哈希表的题

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. 【hdoj_2189】来生一起走(母函数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2189 本题的数学模型如下: 分解的问题,常用母函数求解,这里要求每个"硬币"的价值必须 ...