poj3264(线段树区间求最值)
题目连接:http://poj.org/problem?id=3264
题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差。
线段树功能:区间求最值,O(logN)复杂度查询
#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 50010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int mx[N<<],mn[N<<];
void Pushup(int rt)
{
mn[rt]=min(mn[rt<<],mn[rt<<|]);
mx[rt]=max(mx[rt<<],mx[rt<<|]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
int x;
scanf("%d",&x);
mn[rt]=mx[rt]=x;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
int querymin(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return mn[rt];
}
int m=(l+r)>>;
int res=inf;
if(L<=m)res=min(res,querymin(L,R,lson));
if(m<R)res=min(res,querymin(L,R,rson));
return res;
}
int querymax(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return mx[rt];
}
int m=(l+r)>>;
int res=;
if(L<=m)res=max(res,querymax(L,R,lson));
if(m<R)res=max(res,querymax(L,R,rson));
return res;
}
int main()
{
int n,m;
int a,b;
while(scanf("%d%d",&n,&m)>)
{
build(,n,);
while(m--)
{
scanf("%d%d",&a,&b);
int tallest=querymax(a,b,,n,);
int shortest=querymin(a,b,,n,);
printf("%d\n",tallest-shortest);
}
}
}
poj3264(线段树区间求最值)的更多相关文章
- hdu4521-小明系列问题——小明序列(线段树区间求最值)
题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为 线段树解法. #include ...
- hdu 1754 I Hate It(线段树区间求最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- [HDU] 2795 Billboard [线段树区间求最值]
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795.Billboard-完全版线段树(区间求最值的位置、区间染色、贴海报)
HDU2795.Billboard 这个题的意思就是在一块h*w的板子上贴公告,公告的规格为1*wi ,张贴的时候尽量往上,同一高度尽量靠左,求第n个公告贴的位置所在的行数,如果没有合适的位置贴则输出 ...
- HDU 1754 I Hate It(线段树区间求最值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!
D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- 线段树区间求最大值(点更新)---I Hate It
HDU 1754 Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的 ...
- Count the Colors-ZOJ1610(线段树区间求)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
随机推荐
- 程序实现LayoutAnimationController
在res/anim下新建anim_set.xml: <?xml version="1.0" encoding="utf-8"?> <set x ...
- Effective C++_笔记_条款06_若不想使用编译器自动生成的函数,就该明确拒绝
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 通常如果你不希望class支持某一特定机能,只要不声明对应函数就 ...
- MD5加密以及验证加密-加盐
加密与解密算法: /// <summary> /// 签名字符串 32位 /// </summary> /// <param name="input" ...
- Delphi的DLL里如何实现定时器功能?
一,首先引入“mmsystem”单元. 二,启动定时器: var MMTimerID: Integer; // 定时器ID MMTimerID := timeSetEvent(1000, 0, @Ti ...
- Delphi XE2 升级项目编译ralease版本,无法添加UAC解决方法
我今天把一个原来是Delphi2007的工程升级到了Delphi XE2,在编译ralease版本时候,发现无法添加UAC,我觉得可能是升级的原因,随后我用XE2新建了一个工程,但是在编译raleas ...
- 制作openstack用的centos6.5镜像
目的: 在centos6.5操作系统环境下制作一个centos6.5的kvm镜像,安装cloud-init,能自己主动扩展根分区 一.制作环境: 操作环境是在openstack平台开一个实例.装的是c ...
- 多个haproxy 之间跳转
C:\>ping wechatTest.winfae.com 正在 Ping wechatTest.winfae.com [120.55.118.6] 具有 32 字节的数据: 来自 120.5 ...
- [置顶] 小伙伴们来自己实现LinkedList
继前面实现ArrayList后,今天和小伙伴一起实现LinkedList,LinkedList实现我们采用双向链表来实现,在每次查找时候,如果该查找元素位于该链表的前半段,则从开始检索,如果位于链表的 ...
- 简单概率dp(期望)-zoj-3640-Help Me Escape
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 题目大意: 有n条路,选每条路的概率相等,初始能力值为f,每 ...
- 修改Hosts文件
Hosts文件是一个用于存储计算机网络中节点信息的文件,它可以将主机名映射到相应的IP地址,实现DNS的功能,它可以由计算机的用户进行控制. Hosts文件的存储位置在不同的操作系统中并不相同,甚至不 ...