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

Line 1: Two space-separated integers, N and Q

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

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

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 【线段树】+【单点更新】的更多相关文章

  1. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  2. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  3. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  4. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  5. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  6. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  8. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  9. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  10. HDUOJ----1166敌兵布阵(线段树单点更新)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. python模块之copy

    提供浅拷贝和深拷贝两种模式. =>copy(x):返回x的浅拷贝 =>deepcopy(x):返回x的深拷贝 浅拷贝和深拷贝: 浅拷贝复制不变对象,引用可变对象(如列表和字典): 深拷贝复 ...

  2. 【C++】this指针

    来自:黄邦勇帅 this 指针是所有成员函数的隐含指针,每次调用成员函数时,this 指针就指向调用此函数的对象.可以在成员函数类 部使用显使用this 指针. 友元函数不是类的成员函数,所以友元函数 ...

  3. Redis安装-CentOs7

    官方地址 确保gcc已经安装 $ yum list installed | grep gcc $ yum install gcc 下载.提取和编辑Redis: $ wget http://downlo ...

  4. 如何调用docker swarm service的API来创建及更新服务

    平衡的推进,先作一个原型吧. #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json #定义docker s ...

  5. centos6.5 403 Forbidden 设置了777还是不行

    Forbidden You don't have permission to access /liuyanben/install on this server. Apache/2.2.15 (Cent ...

  6. linux基本优化

    修改主机名 hostnamectl set-hostname <hostname> 修改终端命令行颜色 # vim /etc/profile 末尾添加 PS1='\[\e[31;40m\] ...

  7. 在Strust2 使用datatimepicker 标签引发的一系列问题

    问题:出现无法识别的问题 原因:Strust2.1开始,对于ajax类的标签不再使用<%@ taglib prefix="s" uri="/struts-tags& ...

  8. luogu P1126 机器人搬重物

    题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机 ...

  9. JavaBean的详细及引用

    1.JavaBean实际是具有统一接口格式的java类 2.JavaBean的组成:属性(Properties).方法(Method).事件(Events) 3.一个JavaBean的例子(该例子是用 ...

  10. redis命令行清缓存

    任务管理器-服务-redis 右击打开详细信息--右击打开文件位置 在这个位置cmd 输入命令redis-cli 在输入命令:flushall 出现ok即清除缓存成功