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 ...
随机推荐
- express 4.x+ swig
Express 是一个基于 Node.js 平台的极简.灵活的 web 应用开发框架,它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. express官网:http://www.exp ...
- V-Hyper安装ubuntu-13.10-server-amd64
1.在windws8上的V_Hyper虚拟机上安装Ubuntu虚拟机服务器版.遇到的问题和解决方案 2.正确的在V-Hyper配置方法参考文章:在Hyper-V中安装和配置Ubuntu Server ...
- DRF视图集的使用
# 原创,转载请留言联系 如果要把同一种http请求方法的多个接口放在同一个视图中,比如:查询多条数据和一条数据都是get请求,放在同一个视图里,应该怎么做呢??? 如果直接放在一起,会怎样呢? # ...
- hdu 5190(水题)
Go to movies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 详解nginx、php-fpm和mysql用户权限
通常情况下,我们运行web应用的服务器有CentOS.Ubuntu.Debian等等的Linux发行版本.这时候,构成服务架构所必须的Nginx.php和MySQL等应用的权限控制就显得非常重要,各个 ...
- java 访问 kerberos 认证的 kafka
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 7/25 CSU-ACM2018暑假集训比赛1
题目链接 [A - Tricky Sum ] In this problem you are to calculate the sum of all integers from 1 to n, but ...
- POJ 1486 Sorting Slides(二分图匹配)
[题目链接] http://poj.org/problem?id=1486 [题目大意] 给出每张幻灯片的上下左右坐标,每张幻灯片的页码一定标在这张幻灯片上, 现在问你有没有办法唯一鉴别出一些幻灯片 ...
- 【博弈论】【SG函数】【找规律】Gym - 101147A - The game of Osho
以后这种题还是不能空想,必须打个表看看,规律还是比较好找的……具体是啥看代码.用SG函数暴力的部分就不放了. #include<cstdio> using namespace std; i ...
- 【分块】bzoj2957 楼房重建
http://www.cnblogs.com/wmrv587/p/3843681.html ORZ 分块大爷.思路很神奇也很清晰. 把 块内最值 和 块内有序 两种良好的性质结合起来,非常棒地解决了这 ...