POJ3264 Balanced Lineup 线段树区间最大值 最小值
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 线段树区间最大值 最小值的更多相关文章
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- nyoj 119 士兵杀敌(三)【线段树区间最大值最小值差】
士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...
随机推荐
- Java NIO(New I/O)的三个属性position、limit、capacity
Java NIO(New I/O)的三个属性position.limit.capacity 在缓冲区中,最重要的属性有下面三个,它们一起合作完成对缓冲区内部状态的变化跟踪: capacity posi ...
- Matlab中bsxfun和unique函数解析
一.问题来源 来自于一份LSH代码,记录下来. 二.函数解析 2.1 bsxfun bsxfun是一个matlab自版本R2007a来就提供的一个函数,作用是”applies an element-b ...
- [转载]jQuery 1.9 移除了 $.browser 的替代方法获取浏览器类型
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更新的 2.0 版本中,将不再支持 IE 6/7/8. ...
- The 6th Zhejiang Provincial Collegiate Programming Contest->ProblemK:K-Nice
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3212 题意:构造出一个n*m的有k个上下左右的和等于中间数的小矩阵的任意矩 ...
- ubuntu12 开机自动转到命令行
命令: sudo gedit /etc/default/grub 找到这一行 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"改成 GRUB_CM ...
- highcharts 折线图
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- SQLite入门与分析(四)---Page Cache之事务处理(1)
写在前面:从本章开始,将对SQLite的每个模块进行讨论.讨论的顺序按照我阅读SQLite的顺序来进行,由于项目的需要,以及时间关系,不能给出一个完整的计划,但是我会先讨论我认为比较重要的内容.本节讨 ...
- Retrofit初识
Retrofit Retrofit是一套RESTful架构的Android(Java)客户端实现,基于注解,提供JSON to POJO(Plain Ordinary Java Object,简单Ja ...
- 一个php类 Autoloader
php autoloader: This is a class for PHP that keeps the user from having to manually include classes ...
- [译]Atomic VS. Non-Atomic 操作
原文链接:atomic-vs-non-atomic-operations 在网上已经写了很多关于原子操作的文章,但是通常都集中在原子的读-修改-写(RMW. read-modify-write)操作. ...