链接:

对于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的更多相关文章

  1. G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题

    题意 给出一个序列  每次查询区间的max-min是多少 思路:直接维护max 和min即可  写两个query分别查最大最小值 #include<cstdio> #include< ...

  2. 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 ...

  3. Balanced Lineup POJ - 3264

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  4. 算法手记 之 数据结构(线段树详解)(POJ 3468)

    依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...

  5. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  6. 线段树单点更新poj 2828

    n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...

  7. 线段树(区间操作) 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 ...

  8. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  9. Gold Balanced Lineup POJ - 3274

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

随机推荐

  1. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  2. 大型运输行业实战_day03_1_基于intellij idea的非maven spring+springMVC+mybatis搭建

    1.搭建标准web项目结构 搭建完成后的项目结构如图 1.创建普通web项目(略) 2.在lib中添加jar包 3.在resources中添加spring-config.xml主配置文件 <?x ...

  3. maven tomcat7 远程热部署

    在maven项目开发中,一般推荐使用jetty进行开发调试.但是在项目发布的时候要求使用tomcat7作为发布服务器,为此在maven中配置了tomcat7插件,以支持项目在外部tomcat7进行远程 ...

  4. MVC001之权限校验

    参考网址:http://bbs.csdn.net/topics/370002739 http://blog.csdn.net/chenloveyue/article/details/7095522 h ...

  5. cloudrea manager 调整datanode数据存储目录

    由于datanode所需磁盘空间较大,所以工作中可能会涉及到给datanode增加磁盘目录或者更改数据目录 CM停止该datanode节点 CM页面增加目录或者修改目录 如果是修改目录的话 需要将服务 ...

  6. Web标准:六、html列表

    Web标准:六.html列表 知识点: 1.ul无序和ol有序列表 2.改变项目符号样式或用图片定义项目符号 3.横向图文列表 4.浮动后父容器高度自适应 5.IE6的双倍边距bug   1)ul无序 ...

  7. Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)

    题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后, ...

  8. python内置函数之attr【反射】

    #Auther Bob#--*--conding:utf-8 --*-- #我们来循序渐进的学习反射 import s1 #阶段1# def run():# url = input("请输入 ...

  9. maven不存在jar包解决

    win7环境 下载:https://maven.apache.org/download.cgi 提取文件,并cmd 转到bin目录 假设要添加的jar包是jbarcode-0.2.8.jar, 可执行 ...

  10. No handlers could be found for logger “apscheduler.executors.default”?

    Call logging.basicConfig() before instantiating the scheduler. That lets you see what the real probl ...