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

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

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=;
struct node
{
int l,r;
int Max,Min;
} Tree[N*];
int h[N],max1=,min1=N;
void build(int l,int r,int rt)
{
Tree[rt].l = l;
Tree[rt].r = r;
if (l==r)
{
Tree[rt].Max=Tree[rt].Min=h[r];
return ;
}
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
Tree[rt].Max=max(Tree[rt<<].Max,Tree[rt<<|].Max);
Tree[rt].Min=min(Tree[rt<<].Min,Tree[rt<<|].Min);
}
void Query(int l,int r,int rt)
{
if(Tree[rt].l==l&&Tree[rt].r==r)
{
max1=max(max1,Tree[rt].Max);
min1=min(min1,Tree[rt].Min);
return ;
}
int mid = (Tree[rt].l+Tree[rt].r)>>;
if (r <= mid)
Query(l,r,rt<<);
else if (l > mid)
Query(l,r,rt<<|);
else
{
Query(l,mid,rt<<);
Query(mid+,r,rt<<|);
}
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
for (int i = ; i <= n; i++)
scanf("%d",&h[i]);
build(,n,);
while(q--)
{
max1=-,min1=N;
int l,r;
scanf("%d%d",&l,&r);
Query(l,r,);
printf("%d\n",max1-min1);
}
}
return ;
}

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. UVA - 11175 From D to E and Back(思路)

    题目: 思路: 如图E:图中a.b.c.d是有向图D中的顶点,如果ac.bc都指向cd,而ac又指向ce,那bc同样应该有一条指向ce的边不然就不能从图D转换来.所以直接枚举顶点就可以了. 代码: # ...

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

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

  3. Intellij idea 生成for循环代码块

    itar 生成array for代码块 for (int i = 0; i < array.length; i++) { = array[i]; } itco 生成Collection迭代 fo ...

  4. 【Python实践-9】将字符串转化为浮点型

    利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456. 思路:计算小数位数--->将字符串中的小数点去掉--->字符串转换为整数-- ...

  5. [bzoj1042][HAOI2008][硬币购物] (容斥原理+递推)

    Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...

  6. 关于 startup_stm32f10x_hd.s 这个文件的一些说明

    关于 startup_stm32f10x_hd.s 这个文件的一些说明 startup_stm32f10x_hd.s 是一个启动文件,里面是使用汇编语言写好的基本程序,当STM32 芯片上电启动的时候 ...

  7. 升级 HTTPS,价值何在?

    HTTPS 实质上是一种面向安全信息通信的协议.从最终的数据解析的角度上看,HTTPS 与 HTTP 没有本质上的区别.对于接收端而言,SSL/TSL 将接收的数据包解密,将数据传给 HTTP 协议层 ...

  8. nyoj 8 一种排序(用vector,sort,不用set)

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数 ...

  9. [codeVS3943] 数学奇才琪露诺

    题目描述 Description 作为上白泽慧音老师的出色弟子,数学奇才琪露诺在算术方面有很深的造诣.今天,codevs有幸请到了这位数学界的奇葩作为本场考试的第一题主考官. 琪露诺喜欢0-9之间的数 ...

  10. POJ——T3417 Network

    http://poj.org/problem?id=3417 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5294   A ...