POJ3264 Balanced Lineup 【线段树】+【单点更新】
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 32778 | Accepted: 15425 | |
| 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
2014-9-4 12:07:18更新:
#include <stdio.h>
#include <algorithm>
#define inf 0x7fffffff
#define maxn 50002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; struct Node{
int maxv, minv;
} tree[maxn << 2];
int arr[maxn], minv, maxv; void pushUp(int rt){
tree[rt].maxv = max(tree[rt << 1].maxv, tree[rt << 1 | 1].maxv);
tree[rt].minv = min(tree[rt << 1].minv, tree[rt << 1 | 1].minv);
} void build(int l, int r, int rt)
{
if(l == r){
tree[rt].maxv = tree[rt].minv = arr[l];
return;
}
int mid = (l + r) >> 1;
build(lson); build(rson);
pushUp(rt);
} void query(int left, int right, int l, int r, int rt)
{
if(left == l && right == r){
maxv = max(maxv, tree[rt].maxv);
minv = min(minv, tree[rt].minv);
return;
}
int mid = (l + r) >> 1;
if(right <= mid) return query(left, right, lson);
else if(left > mid) return query(left, right, rson);
query(left, mid, lson); query(mid + 1, right, rson);
} int main()
{
int n, m, i, a, b;
while(scanf("%d%d", &n, &m) == 2){
for(i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
build(1, n, 1);
while(m--){
scanf("%d%d", &a, &b);
minv = inf; maxv = 0;
query(a, b, 1, n, 1);
printf("%d\n", maxv - minv);
}
}
return 0;
}
#include <stdio.h>
#define maxn 200002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1 struct Node{
int min, max;
} tree[maxn << 2];
int maxAns, minAns; int maxVal(int a, int b)
{
return a > b ? a : b;
} int minVal(int a, int b)
{
return a < b ? a : b;
} void build(int l, int r, int rt)
{
if(l == r){
scanf("%d", &tree[rt].min);
tree[rt].max = tree[rt].min;
return;
} int mid = (l + r) >> 1;
build(lson);
build(rson); tree[rt].max = maxVal(tree[rt << 1].max, tree[rt << 1 | 1].max);
tree[rt].min = minVal(tree[rt << 1].min, tree[rt << 1 | 1].min);
} void query(int left, int right, int l, int r, int rt)
{
if(left == l && right == r){
if(tree[rt].max > maxAns) maxAns = tree[rt].max;
if(minAns > tree[rt].min) minAns = tree[rt].min;
return;
} int mid = (l + r) >> 1;
if(right <= mid) query(left, right, lson);
else if(left > mid) query(left, right, rson);
else{
query(left, mid, lson);
query(mid + 1, right, rson);
}
} int main()
{
int n, q, i, a, b;
scanf("%d%d", &n, &q); build(1, n, 1); while(q--){
scanf("%d%d", &a, &b);
maxAns = 1; minAns = 1000000;
query(a, b, 1, n, 1);
printf("%d\n", maxAns - minAns);
} 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 ...
- HDU 1754 I Hate It 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- HDU 1166 敌兵布阵(线段树单点更新)
敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10575 Accepted: 5489 Descrip ...
- HDU 1166 敌兵布阵(线段树单点更新,区间查询)
描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- HDUOJ----1166敌兵布阵(线段树单点更新)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- String类的常见方法的使用案例
String类的常见方法的使用案例 //使用指定的字符串替换当前字符串中指定的内容 //将helloworld中的o替换为a String s="HelloWorld"; Stri ...
- 多个springboot项目部署在同一tomcat上,出现jmx错误
多个springboot项目部署在同一tomcat上,出现jmx错误 原因:因为jmx某些东西重复,禁用jmx就可以了 endpoints.jmx.unique-names=true
- JQ面向对象
静态方法:某种类型才有的方法,这个方法干的事情只有类型本身有关,不受具体实例对象的影响,在C#语言中,它用static表示,VB中用share表示,而在jq中我们一般用$或者JQuery表示JQ类型, ...
- NumPy、SciPy 等Python包在Windows下的whl安装包下载
http://www.lfd.uci.edu/~gohlke/pythonlibs/ 感谢加利福尼亚大学尔湾分校(University of California, Irvine)荧光动力实验室(瞎翻 ...
- Vue 不使用Vuex的情况下进行状态管理
在封装自己的Vue ui库的时候,往往要封装一些比较复杂的组件,比如说table,form之类.这些组件由于功能繁杂,还涉及到子组件嵌套及通信,如果没有一套状态管理方案的话很容易导致代码难以阅读.难以 ...
- (13)python 正则表达式
匹配单个字符 f. o f和o之间是任意字符 例如:fbo123 .. 任意两个字符 \.用来匹配. 边界匹配 the 表示包含the的任何字符串 ^from 表示以from开头的所 ...
- 洛谷 P1316 丢瓶盖【二分答案】
题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...
- CF981C Useful Decomposition【树/思维】
[链接]:CF [题意]:给定一棵树,要求拆成若干条简单路径,并且这些路径都经过一个公共节点.给出任意一个解决方案,如不存在输出No. [分析]: 因为是一棵树, 所以如果要求任意两条路线至少有一个公 ...
- HDU 3045 Picnic Cows
$dp$,斜率优化. 设$dp[i]$表示$1$至$i$位置的最小费用,则$dp[i]=min(dp[j]+s[i]-s[j]-(i-j)*x[j+1])$,$dp[n]$为答案. 然后斜率优化就可以 ...
- 简单DP【p1934】封印
Description 很久以前,魔界大旱,水井全部干涸,温度也越来越高.为了拯救居民,夜叉族国王龙溟希望能打破神魔之井,进入人界"窃取"水灵珠,以修复大地水脉.可是六界之间皆有封 ...