(线段树)Balanced Lineup --POJ --3264
链接:
对于POJ老是爆,我也是醉了, 链接等等再发吧!
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/G
只是简单的建树,每个节点上记录它的最大值和最小值,最后查询一下,就ok了
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
const int N = ;
using namespace std; #define Lson r<<1
#define Rson r<<1|1
#define mid a[r].Mid() struct node
{
int L, R;
int Max, Min;
int Mid()
{
return (L+R)>>;
}
int len()
{
return (R-L+);
};
} a[N<<]; void BuildTree(int r, int L, int R)
{
a[r].L=L, a[r].R=R; if(L==R)
{
scanf("%d", &a[r].Min);
a[r].Max = a[r].Min;
return ;
} BuildTree(Lson, L, mid);
BuildTree(Rson, mid+, R); a[r].Min = min(a[Lson].Min, a[Rson].Min);
a[r].Max = max(a[Lson].Max, a[Rson].Max);
} int QueryMax(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].Max; if(R<=mid)
return QueryMax(Lson, L, R);
else if(L>mid)
return QueryMax(Rson, L, R);
else
{
return max(QueryMax(Lson, L, mid), QueryMax(Rson, mid+, R));
}
} int QueryMin(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].Min; if(R<=mid)
return QueryMin(Lson, L, R);
else if(L>mid)
return QueryMin(Rson, L, R);
else
{
return min(QueryMin(Lson, L, mid), QueryMin(Rson, mid+, R));
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{ BuildTree(, , n); int L, R; while(m--)
{
scanf("%d%d", &L, &R);
printf("%d\n", QueryMax(, L, R)-QueryMin(, L, R));
}
} return ;
}
(线段树)Balanced Lineup --POJ --3264的更多相关文章
- G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题
题意 给出一个序列 每次查询区间的max-min是多少 思路:直接维护max 和min即可 写两个query分别查最大最小值 #include<cstdio> #include< ...
- Day6 - H - Balanced Lineup POJ - 3264
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- Balanced Lineup POJ - 3264
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- 算法手记 之 数据结构(线段树详解)(POJ 3468)
依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- 线段树单点更新poj 2828
n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...
- 线段树(区间操作) POJ 3325 Help with Intervals
题目传送门 题意:四种集合的操作,对应区间的01,问最后存在集合存在的区间. 分析:U T [l, r]填充1; I T [0, l), (r, N]填充0; D T [l, r]填充0; C T[0 ...
- Gold Balanced Lineup - poj 3274 (hash)
这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...
- Gold Balanced Lineup POJ - 3274
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
随机推荐
- How to Pronounce BEAUTIFUL
How to Pronounce BEAUTIFUL Share Tweet Share Tagged With: 3-Syllable Can you say this word beautiful ...
- MySQL Installation of system tables failed!
刚开始学习Linux,就遇到了问题. 当在RedHat下安装MySQL时提示如下错误,请高手给点指点,谢谢: rpm -vih MySQL-server-community-5.0.96-1.rhe ...
- hmm 软件的使用
1)使用HMM模型搜索序列数据库(以青蟹蛋白库为例,简写为qingxie.pep),同源参考序列(query.fas) hmmbuild: 用多重比对序列构建HMM模型:hmmsearch: 使用HM ...
- pymongo CursorNotFound错误
部分报错如下: File "D:\anaconda\lib\site-packages\pymongo\cursor.py", line 1189, in next if len( ...
- shell获取函数的返回值
背景:定义了一个函数,比对本地和线上服务器集群数量差别,想要获取不同集群的个数.shell和其他语言的函数返回值还是差别挺大的. 定义一个函数 functionname(){ 操作内容 ...
- New Game! (最短路+建图)
New Game! https://www.nowcoder.com/acm/contest/201/L 题目描述 Eagle Jump公司正在开发一款新的游戏.Hifumi Takimoto作为其中 ...
- MyBatis高级查询
-------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...
- mac常用软件
捕捉图片文字的软件:Picatext.v1.0 模拟网络环境的软件:hardware_io_tools_for_xcode__october_2013 读取PDF的软件:PDF Expert 20 马 ...
- .net VS2008 时间加减,时间段,时间格式化到秒
举个例子: DateTime time1 = DateTime.Now; DateTime time2 = time1.AddDays(1); time1是当前时间,time2比当前时间多一天.也就是 ...
- 过滤输入htmlentities与htmlspecialchars用法
过滤输入 (即来自所列数据源中的任何数据)是指,转义或删除不安全的字符.在数据到达应用的存储层之前,一定要过滤输入数据.这是第一道防线.假如网站的评论表单接收html,默认情况下 访客可以毫无阻拦地在 ...