http://poj.org/problem?id=3264

题意:n个数,q个询问,输出[l,r]中最大值与最小值的差。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5. const int N=;
  6. struct node
  7. {
  8. int l,r;
  9. int Max,Min;
  10. } Tree[N*];
  11. int h[N],max1=,min1=N;
  12. void build(int l,int r,int rt)
  13. {
  14. Tree[rt].l = l;
  15. Tree[rt].r = r;
  16. if (l==r)
  17. {
  18. Tree[rt].Max=Tree[rt].Min=h[r];
  19. return ;
  20. }
  21. int mid = (l+r)>>;
  22. build(l,mid,rt<<);
  23. build(mid+,r,rt<<|);
  24. Tree[rt].Max=max(Tree[rt<<].Max,Tree[rt<<|].Max);
  25. Tree[rt].Min=min(Tree[rt<<].Min,Tree[rt<<|].Min);
  26. }
  27. void Query(int l,int r,int rt)
  28. {
  29. if(Tree[rt].l==l&&Tree[rt].r==r)
  30. {
  31. max1=max(max1,Tree[rt].Max);
  32. min1=min(min1,Tree[rt].Min);
  33. return ;
  34. }
  35. int mid = (Tree[rt].l+Tree[rt].r)>>;
  36. if (r <= mid)
  37. Query(l,r,rt<<);
  38. else if (l > mid)
  39. Query(l,r,rt<<|);
  40. else
  41. {
  42. Query(l,mid,rt<<);
  43. Query(mid+,r,rt<<|);
  44. }
  45. }
  46. int main()
  47. {
  48. int n,q;
  49. while(~scanf("%d%d",&n,&q))
  50. {
  51. for (int i = ; i <= n; i++)
  52. scanf("%d",&h[i]);
  53. build(,n,);
  54. while(q--)
  55. {
  56. max1=-,min1=N;
  57. int l,r;
  58. scanf("%d%d",&l,&r);
  59. Query(l,r,);
  60. printf("%d\n",max1-min1);
  61. }
  62. }
  63. return ;
  64. }

Balanced Lineup(线段树)的更多相关文章

  1. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  2. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  3. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  4. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  5. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  6. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  7. POJ 3264 Balanced Lineup (线段树)

    Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...

  8. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  9. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  10. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

随机推荐

  1. asp.net mvc,基于aop实现的接口访问统计、接口缓存等

    其实asp.net 上aop现有的框架应该蛮多的,比如静态注入式的PostSharp(新版本好像已经商业化了,旧版本又不支持.net4.0+),或者通过反射的(性能会降低). 本文则是通过mvc其中一 ...

  2. mesh topology for airfoil, wing, blade, turbo

    ref Ch. 5, Anderson, CFD the basics with applications numerical grid generation foundations and appl ...

  3. 数据库中间件MyCat学习总结(1)——MyCat入门简介

    为什么需要MyCat? 虽然云计算时代,传统数据库存在着先天性的弊端,但是NoSQL数据库又无法将其替代.如果传统数据易于扩展,可切分,就可以避免单机(单库)的性能缺陷. MyCat的目标就是:低成本 ...

  4. ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 Scores

    #1236 : Scores 时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 Kyle is a student of Programming Monkey Element ...

  5. [luoguP3203][HNOI2010]BOUNCE 弹飞绵羊(LCT)

    传送门 每个点都会跳到另一个点,连边就是一棵树. 更改弹力就是换边. 求一个点跳多少次跳到终点就是求这个点的深度,那么只需要维护 size 域,access(n + 1) 然后 splay(x),求 ...

  6. Promise 异步编程

    //1.解决异步回调问题 //1.1 如何同步异步请求 //如果几个异步操作之间并没有前后顺序之分,但需要等多个异步操作都完成后才能执行后续的任务,无法实现并行节约时间 const fs = requ ...

  7. 复习1背包dp

    背包问题是对于一个有限制的容器,一般计算可以装的物品的价值最值或数量.通常每个物品都有两个属性空间和价值,有时还有数量或别的限制条件,这个因体而异. 背包大概分成3部分,下面会细述这最经典的3种题型 ...

  8. nyoj_528_找球号(三)_201404152050

    找球号(三) 时间限制:2000 ms  |  内存限制:3000 KB 难度:2   描述 xiaod现在正在某个球场负责网球的管理工作.为了方便管理,他把每个球都编了号,且每个编号的球的总个数都是 ...

  9. Java使用JNA调用DLL库

    Java调用DLL方法有三种,JNI.JNA.JNative, 本文为JNA JNA为使用jna.jar包,下载地址:http://www.java2s.com/Code/Jar/j/Download ...

  10. 超简单的vue2.0分页组件

    1.组件代码 <template> <div class="pagination_comment_style" style="width: 100%;o ...