Balanced Lineup(线段树的简单了解)
个人心得:线段树就是将一段序列拆分为一个个单独的节点,不过每俩个节点又可以联系在一起,所以就能很好的结合,比如这一题,
每次插入的时候都将这一段区间的最大最小值更新,就能大大减少时间。
这个线段树建立是以数组的,根节点为0,后面每次都是父节点*2+1/2。
这题简单的教会了我如何创建线段树,以及一些简单的线段树操作,还要继续加深。
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 <stdio.h>
#include <string.h>
#include<iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=0xffffff0;
int maxa=-inf;
int mina=inf;
struct tree
{
int l,r;
int maxt,mint;
int mid()
{
return (l+r)/;
} };
tree Tree[];
void builttree(int root,int x,int y){
Tree[root].l=x;
Tree[root].r=y;
Tree[root].maxt=-inf;
Tree[root].mint=inf;
if(x!=y){
builttree(root*+,x,(x+y)/);
builttree(root*+,(x+y)/+,y);
}
}
void inserttree(int root,int i,int v){
if(Tree[root].l==i&Tree[root].r==i)
{
Tree[root].maxt=Tree[root].mint=v;
return;
}
Tree[root].maxt=max(Tree[root].maxt,v);
Tree[root].mint=min(Tree[root].mint,v);
if(i<=Tree[root].mid())
inserttree(root*+,i,v);
else
inserttree(root*+,i,v); }
void checktree(int root,int x,int y){
if(Tree[root].maxt<=maxa&&Tree[root].mint>=mina)
return;
if(Tree[root].l==x&&Tree[root].r==y)
{
maxa=max(maxa,Tree[root].maxt);
mina=min(mina,Tree[root].mint);
return ;
}
if(y<=Tree[root].mid())
checktree(root*+,x,y);
else if(x>Tree[root].mid())
checktree(root*+,x,y);
else {
checktree(root*+,x,Tree[root].mid());
checktree(root*+,Tree[root].mid()+,y);
} }
int main()
{
int n,m;
scanf("%d%d",&n,&m);
builttree(,,n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
inserttree(,i,x);
}
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mina=inf,maxa=-inf;
checktree(,x,y);
printf("%d\n",maxa-mina);
} return ; }
Balanced Lineup(线段树的简单了解)的更多相关文章
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
随机推荐
- Centos----本地yum源制作
本地YUM源制作 1. YUM相关概念 1.1. 什么是YUM YUM(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的S ...
- Loadrunder常见问题汇总(持续更新)
1.LR 脚本为空的解决方法: 1)如果安装了IE以外的浏览器,并且IE不是默认浏览器,则无法生成录制脚本 2)如果录制脚本时IE不能打开,则需要将浏览器的IE工具高级选项中,将“启用第三方浏览器扩展 ...
- android 加固防止反编译-重新打包
http://blog.csdn.net/u010921385/article/details/52505094 1.需要加密的Apk(源Apk) 2.壳程序Apk(负责解密Apk工作) 3.加密工具 ...
- Linux网络检测手段汇总
1.iftop iftop可测量通过每一个套接字连接传输的数据:它采用的工作方式有别于nload.iftop使用pcap库来捕获进出网络适配器的数据包,然后汇总数据包大小和数量,搞清楚总的带宽使用情况 ...
- 线程同步synchronized和ReentrantLock
一.线程同步问题的产生及解决方案 问题的产生: Java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),将会导致数据不准确,相互之间产生冲突. 如下例:假设有一个卖票 ...
- 逐行读取txt文件并存入到数组中
get_file_contents_on_line.php $file = fopen("log.txt", "r"); $user=array(); $i=0 ...
- class_alias--为一个类创建别名
class_alias--为一个类创建别名 bool class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ...
- CentOS 7防火墙设置开放80端口
在CentOS 6.x版本中,默认使用的是iptables防火墙.到了CentOS 7.x版本,默认防火墙变成了firewalld.本篇通过使用firewalld开启.关闭 HTTP(80)端口,来讲 ...
- 使用Shell脚本查找程序对应的进程ID,并杀死进程
#!/bin/sh NAME='shell.php' echo $NAME ID=`ps -ef | grep "$NAME" | grep -v "$0" | ...
- 文件系统中跳转【TLCL】
pwd - Print name of current working directory cd - Change directory ls - List directory contents Lin ...