求区间最值…(基础,继续)

code………..

#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; const int N=50007;
struct st{
int left,right;
int mina;
int maxa;
};
st q[N*4];
int n,m; void build(int num,int L,int R)
{
q[num].left=L;
q[num].right=R;
if(L==R)
{
scanf("%d",&q[num].maxa);
q[num].mina=q[num].maxa;
return;
}
build(2*num,L,(L+R)/2);
build(2*num+1,(L+R)/2+1,R);
q[num].mina=min(q[2*num].mina,q[2*num+1].mina);
q[num].maxa=max(q[2*num].maxa,q[2*num+1].maxa);
}
int ans1;
int ans2;
int get_max(int num,int s,int t)
{
if(q[num].left>=s&&q[num].right<=t)
return q[num].maxa;
int mid=(q[num].left+q[num].right)/2;
if(mid>=t)
return get_max(2*num,s,t);
else if(mid<s)
return get_max(2*num+1,s,t);
else{
return max(get_max(2*num,s,mid),get_max(2*num+1,mid+1,t));
}
} int get_min(int num,int s,int t)
{
if(q[num].left>=s&&q[num].right<=t)
return q[num].mina;
int mid=(q[num].left+q[num].right)/2;
if(mid>=t)
return get_min(2*num,s,t);
else if(mid<s)
return get_min(2*num+1,s,t);
else{
return min(get_min(2*num,s,mid),get_min(2*num+1,mid+1,t));
}
} int main()
{
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",get_max(1,a,b)-get_min(1,a,b));
}
return 0;
}

POJ3264【线段树】的更多相关文章

  1. Balanced Lineup poj3264 线段树

    Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最 ...

  2. POJ3264线段树求最值

    刚开始还觉得有点怪怪的.因为想着如果每个树只是单纯地记录它所在的区间的话会不会有不在区间内的数据给更新了,但是我好像是傻掉了因为如果有这种情况出现的话在父亲节点就会分成l,mid和mid+1,r两个区 ...

  3. poj3264 线段树

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44121   Accepted: 20715 ...

  4. POJ3264(线段树入门题)

    Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     ...

  5. poj3264(简单线段树)

    题目链接:https://vjudge.net/problem/POJ-3264 题意:线段树简单应用题,区间查询最大值和最小值的差. 思路:用线段树维护区间的最大值和最小值即可. AC代码: #in ...

  6. 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40687   Accepted: 19137 ...

  7. [POJ3264]Balanced Lineup(线段树,区间最值差)

    题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...

  8. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  9. poj3264(线段树区间求最值)

    题目连接:http://poj.org/problem?id=3264 题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差. ...

  10. POJ3264(线段树求区间最大最小值)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 41162   Accepted: 19327 ...

随机推荐

  1. [BLE]CC2640之ADC功能实现和供电电压的採集

    一.开篇 Write programs that do one thing and do it well ~~~~~ 发现非常多人关于使用CC2640/CC2650的过程中比較难以应对的问题就是实现A ...

  2. Java学习之基本概念

    1.java体系结构: j2se:是Java的标准版,用于标准的应用开. j2ee:是Java的企业版,用于企业级的应用服务,web服务开发. j2me:是Java的微型版,用户手机或嵌入式开发. p ...

  3. UBUntu 软件 源配置方法

        近期公司产品须要添加一个功能,就是版本号自己主动更新.使用apt-get 实现. apt-get 软件源配置的方法,參见本人资源里的共享.以下是代码中作为升级的一部分.  FILE *fp; ...

  4. iOS 相似淘宝商品详情查看翻页效果的实现

    基本思路: 1.设置一个 UIScrollView 作为视图底层,而且设置分页为两页 2.然后在第一个分页上加入一个 UITableView 而且设置表格可以上提载入(上拉操作即为让视图滚动到下一页) ...

  5. vue 安装与起步

    vue安装: 1.官网下载vue,在script标签里引用(去下载) 2.使用CDN(建议下载到本地,不推荐这种方法): BootCDN:https://cdn.bootcss.com/vue/2.2 ...

  6. 嵌入式开发之davinci--- 8168 question about capture PAL on 8168

    http://e2e.ti.com/support/dsp/davinci_digital_media_processors/f/717/t/340483.aspx?pi199607=2

  7. 流媒体开发之开源项目live555---更改server端的帧率大小和码率大小

    -----------------------------qq:1327706646 010101010101010110010101010101010101010author:midu 010101 ...

  8. python -- day 11 考试题

    1. 文件t1.txt里面的内容为:(6分) 1,alex,22,13651054608,IT 2,wusir,23,13304320533,Tearcher 3,taibai,18,13332353 ...

  9. monitor and move the log content to our big data system

    Apache Flume HDFS Sink Tutorial | HowToProgram https://howtoprogram.xyz/2016/08/01/apache-flume-hdfs ...

  10. A Practical Introduction to Blockchain with Python

    A Practical Introduction to Blockchain with Python // Adil Moujahid // Data Analytics and more http: ...