Balanced Lineup:线段树:区间最值 / RMQ
不要被线段树这个名字和其长长的代码吓到。
D - Balanced Lineup
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
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
线段树主要的是个树结构,多用于区间修改,查询。修改查询的时间复杂度都为O(long n),是一个很理想的复杂度。
一般输入数据比较多,所以用cin要关闭流同步,或者用scanf,当然最推荐的还是快读。
/*
线段树:区间查询最大最小值
*/ #include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 50020; //总节点数 struct node {
int l, r;
int maxx; //区间最大值
int minx; //区间最小值
}tree[4*maxn]; //总节点数 最坏情况下是4*maxn int n, m, t; inline int read() { //快读
int i = 0, j = 1;
char ch = getchar();
while (ch<'0' || ch>'9') { if (ch == '-')j = -1; ch = getchar(); }
while (ch >= '0'&&ch <= '9') i = i * 10 + ch - '0', ch = getchar();
return i*j;
} void buildtree(int p, int l, int r) { //建树&更新节点
tree[p].l = l; //初始化
tree[p].r = r; //每个节点的左右区间,就是传入的l,r
tree[p].maxx = -1; //把最大值赋值为-1
tree[p].minx = 1e9; //给minx赋值一个在题目中最大的值 if (l == r) { //l==r 代表是叶子节点
tree[p].maxx = tree[p].minx = read();
return;
} int mid = l + r >> 1; //不是叶子节点,就把区间分开 左儿子比右儿子多 (1+5)/2=3 ==> [1~3]--[4~5]
buildtree(p * 2, l, mid); //左区间树
buildtree(p * 2+1, mid + 1, r); //右区间树 tree[p].maxx = max(tree[p * 2].maxx, tree[p * 2 + 1].maxx); //节点的最大值,就是两个儿子节点的最大值
tree[p].minx = min(tree[p * 2].minx, tree[p * 2 + 1].minx); //同理
} int findmax(int p, int x, int y) //查找最大值
{
if (x<=tree[p].l&&tree[p].r <= y) //为什么是<=而不是==,这里是与下边匹配的。
return tree[p].maxx; int Max = -1, mid = (tree[p].l + tree[p].r) / 2;
if (x <= mid)
Max = max(Max, findmax(2 * p, x, y)); //搜索左区间,区间范围仍然是x~y,所以上边是<=
if (y > mid)
Max = max(Max, findmax(2 * p + 1, x, y)); //搜索右区间。
return Max; } int findmin(int p, int x, int y) { //查找最小值 同查找最大值
if (tree[p].l >= x&&tree[p].r <= y)
return tree[p].minx;
int Min = 1e9, mid = (tree[p].l + tree[p].r) / 2;
if (x <= mid)
Min = min(Min, findmin(p * 2, x, y));
if (y > mid)
Min = min(Min, findmin(p * 2 + 1, x, y));
return Min;
} int main() { cin >> n >> m; buildtree(1, 1, n); //可以把输入放在建树里边, int x, y;
for (int i = 0; i < m; i++) {
x = read();
y = read();
cout << findmax(1, x, y)-findmin(1, x, y) << endl;
}
return 0;
}
RMQ,蒟蒻博主还不会。
Balanced Lineup:线段树:区间最值 / RMQ的更多相关文章
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- 【bzoj4695】最假女选手 线段树区间最值操作
题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...
- 【bzoj4355】Play with sequence 线段树区间最值操作
题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...
- 【hdu5306】Gorgeous Sequence 线段树区间最值操作
题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- nyoj 119士兵杀敌(三)(线段树区间最值查询,RMQ算法)
题目119 题目信息 执行结果 本题排行 讨论区 士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军统率着N个士兵,士兵分别编号为1~N,南将军常 ...
随机推荐
- Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践
Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践 背景 很多开发者或者有经验的老手都会建议尽量不要用单例模式,这是有原因的. 单例模式是设计模式中最简单的也是大家通常最先接触的一种设计 ...
- windows安装多个版本的jdk,解决java-version和javac-version版本不一致的问题
系统先装了jdk1.8 ,环境变量里配置的是jdk1.8,java -version 与javac -version 版本一致. 然后安装了jdk1.6 ,环境变量java_home 改成了1.6,但 ...
- iOS | 解决中文乱码
在iOS开发中,多多少少的朋友在开发的过程中,测试数据的时候可能会碰到后台打印的时候不能正确的打印出正常的汉字,打印出一些影响判断的字符,经常需要查看数组中得元素是否是自己想要的,但是苹果并没有对直接 ...
- memcache和redis的区别和联系
一.区别 Memcache : 1,对每个key的数据最大是1M. 2,对各种技术支持比较全面,session可以存储memcache中,各种框架(例如thinkphp)对memcache支持比较好. ...
- systemd的新特性及常见的systemd unit类型分析
systemd概述 )systemd是一种新的linux系统服务管理器,用于替换init系统,能够管理系统启动过程和系统服务,一旦启动起来,就将监管整个系统.在centos7系统中,PID1被syst ...
- 获取DOM
<template> <div> <header-vue :msg="msg" ref="header">heheh< ...
- pom.xml文件报MavenArchiver错误 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
第一种方式 war项目 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...
- mysql的docker化安装
mysql版本有很多,先看下各类版本号说明: 3.X至5.1.X:这是早期MySQL的版本.常见早期的版本有:4.1.7.5.0.56等. 5.4.X到5.7.X:这是为了整合MySQL AB公司社区 ...
- composer切换中国镜像
替换 composer.lock 文件中的 https://files.phpcomposer.com/files/ 为 https://dl.laravel-china.org 命令行 compos ...
- python中for......else......的使用
for x in range(5): if x == 2: print(x) # break else: print("执行else....") 上述代码:当缺少break关键字时 ...