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标记差不 ...
随机推荐
- mybatis系列-07-输出映射
7.1 resultType 使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功. 如果查询出来的列名和pojo中的属性名全部不一致,没有创建 ...
- Python 学习笔记(四)正则、闭合、生成器
(一)正则表达式 基本规则: ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b 匹配一个单词边界. \d 匹配一个数字. \D 匹配一个任意的非数字字符. x? 匹配可选的x字符.换句话说,就 ...
- BootStrap入门教程 (三) :可重用组件(按钮,导航,标签,徽章,排版,缩略图,提醒,进度条,杂项)
上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前 ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- Cognos 增加全局类
Cognos使用版本10.1.1 由于我服务器装的是linux系统下的,所以window系统下的方法,提一下,但是没有实现过. 1.Linux系统下增加全局类 ●修改GlobalReportStyle ...
- JavaIO流(02)RandomAccessFile类详解
RandomAccessFile类 该类主要是对文件内容进行操作,可以随机的读取一个文件中指定位置的数据: 但是如果想实现这样的功能,则每个数据的长度应该保持一致: 构造方法: 接受File类 ...
- codeforces 630A Again Twenty Five!
A. Again Twenty Five! time limit per test 0.5 seconds memory limit per test 64 megabytes input stand ...
- python challenge第1关--NoteBook上的“乱码”
在 python challenge第0关中已经得到第1关的地址了: http://www.pythonchallenge.com/pc/def/map.html 一.观察地址栏和标签: What a ...
- sqlserver表分区与调优与行列转换
转自: http://www.cnblogs.com/knowledgesea/p/3696912.html http://www.open-open.com/lib/view/open1418462 ...
- JdbcTemplate增删改查
1.使用JdbcTemplate的execute()方法执行SQL语句 jdbcTemplate.execute("CREATE TABLE USER (user_id integer, n ...