Balanced Lineup(最简单的线段树题目)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 33389 | Accepted: 15665 | |
| Case Time Limit: 2000MS | ||
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
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=; int minv,maxv;
struct node
{
int l,r,maxv,minv;
int mid()
{
return (l+r)/;
}
}nodes[*MS]; void creat(int root,int l,int r)
{
nodes[root].l=l;
nodes[root].r=r;
nodes[root].minv=INF;
nodes[root].maxv=-INF;
if(l==r)
return ;
creat(root<<,l,(l+r)/);
creat(root<<|,(l+r)/+,r);
} void updata(int root,int pos,int value)
{
if(nodes[root].l==nodes[root].r)
{
nodes[root].minv=nodes[root].maxv=value;
return ;
}
nodes[root].minv=min(nodes[root].minv,value);
nodes[root].maxv=max(nodes[root].maxv,value);
if(pos<=nodes[root].mid())
updata(root<<,pos,value);
else
updata(root<<|,pos,value);
} void query(int root,int l,int r) //注意这里的minv,maxv使用的全局变量,
{ //使用引用也可以
if(nodes[root].minv>=minv&&nodes[root].maxv<=maxv)
return ; // 剪枝
if(nodes[root].l>=l&&nodes[root].r<=r)
{
minv=min(minv,nodes[root].minv);
maxv=max(maxv,nodes[root].maxv);
return ;
}
if(l<=nodes[root].mid())
query(root<<,l,r);
if(r>nodes[root].mid())
query(root<<|,l,r);
} int main()
{
int N,Q,x,y;
scanf("%d%d",&N,&Q);
creat(,,N);
for(int i=;i<=N;i++)
{
scanf("%d",&x);
updata(,i,x);
}
while(Q--)
{
scanf("%d%d",&x,&y);
minv=INF;
maxv=-INF;
query(,x,y);
printf("%d\n",maxv-minv);
}
return ;
}
Balanced Lineup(最简单的线段树题目)的更多相关文章
- 【ACM/ICPC2013】线段树题目集合(一)
前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...
- 线段树 & 题目
首先说下我写的线段树吧. 我是按照线段树[完全版]那个人的写法来写的,因为网上大多数题解都是按照他的写法来写. 确实比较飘逸,所以就借用了. 节点大小是maxn是4倍,准确来说是大于maxn的2^x次 ...
- 嗯 第二道线段树题目 对左右节点和下标有了更深的理解 hdu1556
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- 「CQOI2006」简单题 线段树
「CQOI2006」简单题 线段树 水.区间修改,单点查询.用线段树维护区间\([L,R]\)内的所有\(1\)的个数,懒标记表示为当前区间是否需要反转(相对于区间当前状态),下方标记时懒标记取反即可 ...
- 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40687 Accepted: 19137 ...
- POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53721 Accepted: 25244 ...
- 洛谷P5057 [CQOI2006]简单题(线段树)
题意 题目链接 Sol 紫色的线段树板子题??... #include<iostream> #include<cstdio> #include<cmath> usi ...
- poj 3468 A Simple Problem with Integers(原来是一道简单的线段树区间修改用来练练splay)
题目链接:http://poj.org/problem?id=3468 题解:splay功能比线段树强大当然代价就是有些操作比线段树慢,这题用splay实现的比线段树慢上一倍.线段树用lazy标记差不 ...
随机推荐
- CSS定位(CSS定位概述、相对定位、绝对定位、浮动)
CSS 定位属性 CSS 定位属性允许你对元素进行定位. 属性 描述 position 把元素放置到一个静态的.相对的.绝对的.或固定的位置中. top 定义了一个定位元素的上外边距边界与其包含块上边 ...
- vi--文本编辑常用快捷键之光标移动
再来一发! 上一篇关于vi/vim的文章中,主要介绍了文本的复制粘贴删除替换等操作,在慢慢的适应vim的过程中,我发现有很多时间实际上是浪费在移动光标上的,特别是行内移动光标.这篇文章就主要是介绍vi ...
- java 复习003
今天主要复习下数据结构的东西 树 自平衡二叉查找树 AVL树(高平衡树)(wiki) 特性:任何节点的两个子树的高度最大差别为一 时间复杂度:查找.插入和删除在平均和最坏情况下都是O(log n) 红 ...
- [Hive - LanguageManual] Create/Drop/Alter -View、 Index 、 Function
Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version info ...
- warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
最近在编译库文件后,使用它做APP,遇到如下问题: 1>LIBCMT.lib(invarg.obj) : error LNK2005: __pInvalidArgHandler 已经在 LIBC ...
- libvirt虚拟系统如何增加usb设备
之前干这些事情都是通过virt-manager来搞定的.不过由于这个图形界面不太方便,而且现在没法打开(具体原因不详,每次打开提示一些方法未实现什么的),所以试下用libvirt的命令virsh来搞定 ...
- Xcode 4 插件制作入门
转自:http://www.onevcat.com/2013/02/xcode-plugin/ 2014.5.4更新 对于 Xcode 5,本文有些地方显得过时了.Xcode 5 现在已经全面转向了 ...
- 【转】iPhone 6 屏幕揭秘
http://www.cocoachina.com/design/20141218/10680.html 一根线的渲染 为了说明多种设备的不同像素渲染情况,我们比较了一个一像素宽的线是怎样渲染的: 最 ...
- 问题-FireDAC连接Sqlite3提示“unable to open database file”
相关资料:http://www.dfwlt.com/forum.php?mod=viewthread&tid=1497&extra= 问题现象:FireDAC连接Sqlite3在开发电 ...
- My97DatePicker的calendar.js的反混淆
eval(string)函数 <script> eval(function(p, a, c, k, e, d) { p = 'function p(){console.log(" ...