POJ3264——Balanced Lineup(线段树)
本文出自:http://blog.csdn.net/svitter
题意:在1~200,000个数中。取一段区间。然后在区间中找出最大的数和最小的数字。求这两个数字的差。
分析:按区间取值,非常明显使用的线段树。
区间大小取200000 * 4 = 8 * 10 ^5;
进行查询的时候。注意直接推断l, r 与mid的关系就可以。一開始写的时候直接与tree[root].L推断,多余了,
逻辑不对。
#include <iostream>
#include <stdio.h>
#include <stdlib.h> using namespace std;
const int INF = 0xffffff;
int maxV, minV; struct Node
{
int L, R;
int Mid(){ return (L+R)/2;}
int maxV, minV; //最大数和最小数
//Node *lchild, *rchild; 使用一位数组就能够不使用,能够看做全然二叉树(可能存在空间浪费)
}; Node tree[800010]; //四倍叶子节点 void Insert(int root, int n, int val)
{
//推断叶子节点
if(tree[root].L == tree[root].R)
{
tree[root].maxV = tree[root].minV = val;
return;
} //递归更新
tree[root].minV = min(tree[root].minV, val);
tree[root].maxV = max(tree[root].maxV, val); //当前为区间节点,寻找叶子节点
if(n < tree[root].Mid())
{
Insert(root*2+1, n, val);
}
else
{
Insert(root*2+2, n, val);
}
} void BuildTree(int root, int l, int r)
{
//建立当前节点
tree[root].L = l;
tree[root].R = r;
tree[root].maxV = -INF;
tree[root].minV = INF;
//递归调用建立子树
if(l != r)
{
BuildTree(root*2+1, l, (l+r)/2);
BuildTree(root*2+2, (l+r)/2+1, r);
} } void Query(int root, int l, int r)
{
//递归终止条件
if(l < tree[root].L || r > tree[root].R)
return; //推断条件:全然符合区间
if(l == tree[root].L && r == tree[root].R)
{
maxV = max(maxV, tree[root].maxV);
minV = min(minV, tree[root].minV);
return;
} if(r <= tree[root].Mid())
Query(root*2+1, l, r);
else if(l > tree[root].Mid())
Query(root*2+2, l, r);
else
{
Query(root*2+1, l, tree[root].Mid());
Query(root*2+2, tree[root].Mid()+1, r);
}
} int main()
{
int N, Q;
int val;
int a, b; //查找区间[a,b]
//while(scanf("%d%d", &N, &Q))
scanf("%d%d", &N, &Q);
{ BuildTree(0, 1, N);
for(int i = 0; i < N; i ++)
{
scanf("%d", &val);
Insert(0, i, val);
}
//用于測试线段树生成情况
// for(int i = 0; i < 7; i++)
// {
// printf("No:%d,\nL: %d,\nR: %d,\nMAX: %d,\nMIN: %d,\n\n", i, tree[i].L, tree[i].R, tree[i].maxV, tree[i].minV);
// }
while(Q--)
{
maxV = -INF, minV = INF;
scanf("%d%d", &a, &b);
Query(0, a, b);
// printf("max: %d\nmin: %d\n", maxV, minV);
printf("%d\n", maxV - minV);
}
} return 0;
}
POJ3264——Balanced Lineup(线段树)的更多相关文章
- 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 ...
- 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线 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- 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 ...
随机推荐
- js 去除字符串左右两端的空格
<script type="text/javascript"> function trim(str){ //删除左右两端的空格 return str.repl ...
- 判断两个XML文件结构与内容是否相同
1. 引入 目前公司的这款软件导入导出数据库信息的方法是:组织数据的内容和结构 利用MS com的sax解析 储存数据为XML格式 优点是可以选择部分导出 缺点是速度慢文件导出的文件庞大,若客户出现 ...
- Problem G: Keywords Search
Problem G: Keywords SearchTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 10 Solved: 6[Submit][Status] ...
- BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )
这种题用数据结构怎么写都能AC吧...按1~N弄个链表然后每次插入时就更新答案, 用set维护就可以了... --------------------------------------------- ...
- linux下静态链接库的用法
最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...
- jQuery学习之结构解析
jQuery内核解析 1.jQuery整体的结构是一个匿名函数 (function( window, undefined ) {})(window); 2.jQuery就是一个很普通的函数,也是一个很 ...
- A Byte of Python (1)安装和运行
有两种方式构建软件设计:一种是把软件做得很简单以至于明显找不到缺陷:另一种是把它做得很复杂以至于找不到明显的缺陷. ——C.A.R. Hoare 获得人生中的成功需要的专注与坚持不懈多过天才与机会. ...
- 关于Python的self指向性
Python的self是指向类的实例化对像,而不是类本身,每次调用类的实例化即self指向此实例化对像,如下代码: class Person: def __init__(self,name): sel ...
- URL中#(井号)的作用(转)
2010年9月,twitter改版. 一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为 http://twitter.com/username 改版后,就变 ...
- BZOJ 1005 明明的烦恼 (组合数学)
题解:n为树的节点数,d[ ]为各节点的度数,m为无限制度数的节点数. 则 所以要求在n-2大小的数组中插入tot各序号,共有种插法: 在tot各序号排列中,插第一个节点的 ...