Q个数

问区间最大值-区间最小值

 // #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N=;
const int MOD = 1e9+;
#define LL long long
double const pi = acos(-);
void fre() {
freopen("in.txt","r",stdin);
}
// inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
int a[N];
int Min,Max;
struct Edge{
int l,r;
int minx,maxx;
}e[*N]; void pushup(int rt){
e[rt].minx=min(e[rt<<].minx,e[rt<<|].minx);
e[rt].maxx=max(e[rt<<].maxx,e[rt<<|].maxx);
} void build(int l,int r,int rt){
e[rt].l=l;
e[rt].r=r;
if(l==r){
e[rt].minx=e[rt].maxx=a[l];
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void query(int l,int r,int rt,int L,int R){
if(e[rt].maxx<=Max&&e[rt].minx>=Min) return;
if(L<=l&&R>=r){
Min=min(e[rt].minx,Min);
Max=max(e[rt].maxx,Max);
return;
}
int mid=(l+r)>>;
if(L<=mid) query(l,mid,rt<<,L,R);
if(R>mid) query(mid+,r,rt<<|,L,R);
// if(r<=mid) query(lson,L,R);
// else if(l>mid) query(rson,L,R);
// else{
// query(lson,L,R);
// query(rson,L,R);
// }
} int main(){
// fre();
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
while(q--){
int L,R;
scanf("%d%d",&L,&R);
Min=inf,Max=-inf;
// cout<<"!!"<<endl;
// getch();
query(,n,,L,R);
printf("%d\n",Max-Min);
}
return ;
}

POJ3264 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: 34140   Accepted: 16044 ...

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

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

  4. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

  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. nyoj 119 士兵杀敌(三)【线段树区间最大值最小值差】

    士兵杀敌(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...

随机推荐

  1. firefly 环境配置所需工具

    原地址:http://www.9miao.com/question-15-59032.html http://down.9miao.com/attachment/forum/201405/19/120 ...

  2. Notifications Nagios

    Introduction I've had a lot of questions as to exactly how notifications work. This will attempt to ...

  3. GIT:本地有更改,但强制作远程仓库里作更新

    有时,紧急线上修改时,这个功能有用处的. git fetch --all git reset --hard origin/master ================ git reset --har ...

  4. DJANGO的ORM的Q查询作多字段外键的模糊查询样码

    工作中用到的,存照一下. from django.db.models import Q if self.kwargs.has_key('search_pk'): search_pk = self.kw ...

  5. 【转】linux C++ 获取文件信息 stat函数详解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  6. [企业级linux安全管理]- 安全管理基础(1)

    1. 操作条件:  (1)装有 Cent OS Linux 操作系统的虚拟机一台 2. 背景: 某企业有一台服务器,其信息如下: (1)  该服务器上存在管理员 root,密码为 root,另存有一些 ...

  7. php 23种设计模式的趣味解释

    http://wenku.baidu.com/link?url=GwvuvSOdJneZQc-DSKoGmPcxTtzn3cdtIp3fRaCNbkg1zJDZZZTx2NwEK5IsqU996fG3 ...

  8. python学习笔记二--列表的使用

    一.基本列表操作 1. 合并‘+’:左右两边必须均为列表 可以用str(),%,list()做类型的转换后再做合并 2. 重复‘*’: 3. 迭代和解析: x作为for循环里步进变量,由于列表是序列, ...

  9. 安装TokuDB引擎

    前言:TokuDB 是一个高性能.支持事务处理的 MySQL 和 MariaDB 的存储引擎.TokuDB 的主要特点是高压缩比,高 INSERT 性能,支持大多数在线修改索引.添加字段,非常适合日志 ...

  10. Java汉字排序(2)按拼音排序

    对于包含汉字的字符串来说,排序的方式主要有两种:一种是拼音,一种是笔画. 本文就讲述如何实现按拼音排序的比较器(Comparator). 作者:Jeff 发表于:2007年12月21日 11:27 最 ...