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 ...
随机推荐
- HTML5-坦克大战一画出敌人坦克并让自己的坦克可以发子弹的功能(二)
上一篇博客只画出了,一个坦克,并让其可以上下左右移动,这篇博客将画出敌人的坦克,并让自己的坦克可以发子弹,但是还不是尽善尽美,还有一些问题,将会在下篇博客说明: html代码: <!DOCTYP ...
- 1.flask视图和URL
1.第一个flask程序 from flask import Flask ''' Flask这个类是项目的核心,以后很多操作都是基于这个类的对象 注册URL等等,都是基于这个类 ''' app = F ...
- [ 手记 ] 联想rd650服务器整列及系统安装
联想 RD650服务器 磁盘阵列:http://wenku.baidu.com/view/b364c2db5f0e7cd185253644.html?from=search 该服务器安装系统需要BIO ...
- Javascript基于对象三大特征 -- 冒充对象
Javascript基于对象三大特征 基本概述 JavaScript基于对象的三大特征和C++,Java面向对象的三大特征一样,都是封装(encapsulation).继承(inheritance ) ...
- Spring Boot 项目部署到本地Tomcat,出现访问路径问题
首先确定war 包没问题,把war包放在webapps目录下,访问http://localhost:8080/ + 项目名称 发现首页可以访问但css,js请求都是404,跳转页面也是404 解决方法 ...
- JSONObject常用的API
http://www.cnblogs.com/java-pan/archive/2012/04/07/jsonobject.html 1.介绍基于JSONObject 1.1的API 2.只介绍常用的 ...
- 实现类似微信聊天功能的mysql表设计
前言: 最近设计了一套聊天功能,此功能支持人对人聊天.发送图片.查看聊天记录.按时间展示聊天列表.最后一条聊天数据及未读消息数 下面分享一下表结构及实现逻辑: 表结构: 1.聊天主表 id(主键id) ...
- 【转载】Linux kill, killall, kill -9
1) 查看进程的方法: ps -ef 或者 ps aux root 15087 0.0 0.0 0 0 ? S 23:31 0:00 [kwo ...
- django + dropzone.js 上传文件
1.dropzone.js http://www.dropzonejs.com/ dropzone.js是一个可预览\可定制化的文件拖拽上传,实现AJAX异步上传文件的工具 2.dropzone.js ...
- luogu P1938找工就业
一头牛在一个城市最多只能赚D元,然后它必须到另一个城市工作.当然它可以在别处工作一阵子后,又回到原来的城市再最多赚D美元.而且这样的往返次数没有限制城市间有P条单向路径,共有C座城市,编号1~C,奶牛 ...