POJ3264(线段树入门题)
Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Output
Sample Input
- 6 3
- 1
- 7
- 3
- 4
- 2
- 5
- 1 5
- 4 6
- 2 2
Sample Output
- 6
- 3
- 0
- #include"cstdio"
- #include"cstring"
- #include"algorithm"
- #define lson (rt<<1),l,mid
- #define rson (rt<<1)|1,mid+1,r
- #define gmid (a[rt].l+a[rt].r)>>1;
- using namespace std;
- const int MAXN=;
- struct node{
- int l,r;
- int maxn,minn;
- }a[MAXN*];
- void build(int rt,int l,int r)
- {
- a[rt].l=l;
- a[rt].r=r;
- if(l==r)
- {
- scanf("%d",&a[rt].maxn);
- a[rt].minn=a[rt].maxn;
- return ;
- }
- int mid=gmid;
- build(lson);
- build(rson);
- a[rt].maxn=max(a[rt<<].maxn,a[(rt<<)|].maxn);
- a[rt].minn=min(a[rt<<].minn,a[(rt<<)|].minn);
- }
- int query(int rt,int l,int r,int op)
- {
- if(a[rt].l==l&&a[rt].r==r)
- {
- if(op==) return a[rt].maxn;
- else return a[rt].minn;
- }
- int mid=gmid;
- if(r<=mid) return query(rt<<,l,r,op);
- else if(mid<l) return query((rt<<)|,l,r,op);
- else{
- if(op==) return max(query(rt<<,l,mid,op),query((rt<<)|,mid+,r,op));
- else return min(query(rt<<,l,mid,op),query((rt<<)|,mid+,r,op));
- }
- }
- int main()
- {
- int n,m;
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- build(,,n);
- while(m--)
- {
- int l,r;
- scanf("%d%d",&l,&r);
- int diff=query(,l,r,)-query(,l,r,);//1表示求最大值,2表示求最小值
- printf("%d\n",diff);
- }
- }
- }
POJ3264(线段树入门题)的更多相关文章
- 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40687 Accepted: 19137 ...
- hdu 1166敌兵布阵(线段树入门题)
>>点击进入原题测试<< 思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记.入门题 cin,cout会超时,记得加std::ios::sync_wit ...
- [poj2104]可持久化线段树入门题(主席树)
解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序 ...
- hiho1079 - 数据结构 线段树(入门题,离散化)
题目链接 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho所在的学校举办社团文化节,各大社团都在宣传栏上贴起了海报,但是贴来贴去 ...
- Mosaic HDU 4819 二维线段树入门题
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
- A Simple Problem with Integers(线段树入门题)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- HDU1698(线段树入门题)
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- hiho1080 - 数据结构 线段树(入门题,两个lazy tag)
题目链接 维护区间和,两个操作:一个是将某个区间设置成一个值,一个是将某个区间增加一个固定值 /**************************************************** ...
- Balanced Lineup poj3264 线段树
Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最 ...
随机推荐
- 49 个jquery代码经典片段
49 个jquery代码经典片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一 些则是真正有用的函数或方法,他们能够帮助你又快又 ...
- servletResponse 文件下载
package response; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOEx ...
- 精彩回顾 HUAWEI HiAI 亮相华为北研所
从普通照片变成艺术品,仅需3秒: 从随手拍下的讲解胶片到生成规整清晰的ppt,只要瞬间…… 5月25日在华为北京研究所举办的HUAWEI HiAI技术合作交流会上,伴随着一声声惊叹,数款接入HUA ...
- python之Matplotlib 和Numpy
1.matplotlib http://www.cnblogs.com/TensorSense/p/6802280.html https://wenku.baidu.com/view/e1c15c9d ...
- EasyPlayer RTSP Windows播放器D3D,GDI的几种渲染方式的选择区别
EasyPlayer-RTSP windows播放器支持D3D和GDI两种渲染方式,其中D3D支持格式如下: DISPLAY_FORMAT_YV12 DISPLAY_FORMAT_YUY2 DISPL ...
- input file 选择Excel文件 相关操作
1.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFo ...
- php计算数组的维数
function array_dim($arr){ if(!is_array($arr)) return 0; else{ $max1 = 0; foreach($arr as $item1){ $t ...
- 【题解】[CF718C Sasha and Array]
[题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...
- 洛谷3243 [HNOI2015]菜肴制作
题目戳这里 Solution 错误的想法:正向建图,然后从入度为0的点选出最小u的开始输出,然后找出u连接的点v,并把v的度数减一,再次把入度为0的点加入小根堆,这样显然有错,因为只能局部保证最小,后 ...
- 【python】python调用shell方法
在python脚本中,有时候需要调用shell获取一下信息,下面介绍两种常用的调用方法. 第一种,os.system() 这个函数获取的是命令的执行状态,比如 >>> import ...