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,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...
随机推荐
- 让wordpress投稿作者在后台只看到自己的文章
wordpress支持多作者撰写,让更多的人参与网站内容的创建是个不错的想法,UGC(User-generated content)使网站主题更丰富,不同的内容吸引不同的受众,一个好的网站应该多产生U ...
- Mybatis代码生成器 xml配置文件 连接SQL SERVER 2005
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...
- codeforces #310 div1 C
操作无论是U还是L,都会使原图形分裂成两个图形,且两个图形的操作互不影响 我们又发现由于操作点只可能在下斜线上,如果将操作按x排序 那么无论是U还是L,都会将操作序列完整分割成两半,且两个操作序列互不 ...
- Java的ResultSet中rs.next()含义
- 压力测试的轻量级具体做法 Apache ab
http://www.cnblogs.com/luminji/archive/2011/09/02/2163525.html
- P55、面试题6:重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字,例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- web.xml的说明
<!--DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 2000-2007 Sun Microsystems ...
- 安装TokuDB引擎
前言:TokuDB 是一个高性能.支持事务处理的 MySQL 和 MariaDB 的存储引擎.TokuDB 的主要特点是高压缩比,高 INSERT 性能,支持大多数在线修改索引.添加字段,非常适合日志 ...
- Android开发之parseSdkContent failed Could not initialize class android.graphics.Typeface
在进行android开发过程中,忽然发现经常弹出来parseSdkContent failed 这个错误,然后google了下解决办法 方法1: 删除.android文件 重启eclipse. 该方法 ...
- nf_contrack_netlink.c
/* Connection tracking via netlink socket. Allows for user space * protocol helpers and general trou ...