poj3264 Balanced Lineup(求区间的最大值与最小值之差)

【学习笔记】高级数据结构--线状树 poj 3264 #include#include #includeusing namespace std;const int INF=0xffffff0;int minV= INF;int maxV= -INF;struct Node{//定义了树中每个节点的信息 int L,R; //树节点的左孩子L,右孩子R int minV,maxV; //在树节点中记录在其范围内的max和min int Mid(){ / 阅读详情

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 37869 Accepted: 17751
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

解题思路:先求最大值,再求最小值

/*******************************************************************************
 * Author :          jinbao
 * Email :           dongjinbao913106840144@gmail.com
 * Last modified :   2015-05-11 20:30
 * Filename :        poj3264.cpp
 * Description :     
 * *****************************************************************************/
#include <iostream>
#include <stdio.h>
using namespace std;
#define min(x,y) x<y?x:y
#define max(x,y) x>y?x:y
const int MAX = 50000+1;
struct cow{
	int Min,Max;
}p[4*MAX];
int a[MAX];
void build(int node,int begin,int end){
	if (begin==end){
		scanf("%d",&a[begin]);
		p[node].Min=p[node].Max=a[begin];
	}
	else{
		build(node*2,begin,(begin+end)/2);
		build(node*2+1,(begin+end)/2+1,end);
		p[node].Min=min(p[node*2].Min,p[node*2+1].Min);
		p[node].Max=max(p[node*2].Max,p[node*2+1].Max);
	}
}
int query(int node,int begin,int end,int left,int right,int flag){
	if (end<left || begin>right){
		if (flag==0)
			return 0xffffff;
		return -1;
	}
	if (begin>=left && end<=right){
		if (flag==0)
			return p[node].Min;
		return p[node].Max;
	}
	int m=query(2*node,begin,(begin+end)/2,left,right,flag);
	int n=query(2*node+1,(begin+end)/2+1,end,left,right,flag);
	if (flag==0)
		return min(m,n);
	return max(m,n);
}
int main(){
	int n,q,l,r,Min,Max;
	while (~scanf("%d%d",&n,&q)){
		build(1,1,n);
		while (q--){
			scanf("%d%d",&l,&r);
			int ans = query(1,1,n,l,r,1) - query(1,1,n,l,r,0);
			printf("%d\n",ans);
		}
	}

	return 0;
}



Xilinx IP库在Modelsim/Questasim中的高效仿真实践 本文详细介绍了在Modelsim/Questasim中高效仿真Xilinx FPGA设计的核心方法。针对仿真时常见的IP核缺失错误,重点解析了两种主流解决方案:使用Vivado的compile_simlib命令自动编译完整仿真库,以及利用export_simulation命令为特定工程生成仿真脚本。文章还提供了手动配置库的深入理解、实战排错步骤及高级避坑指南,帮助用户构建稳定、高效的独立仿真环境。 阅读详情

相关推荐

避坑指南:PCIe设备兼容性那些坑——聊聊MPS/MRRS设置不当引发的血泪史

本文深入探讨了PCIe设备兼容性问题,特别是MPS/MRRS参数设置不当引发的常见故障。通过详细解析MPS(Max Payload Size)和MRRS(Max Read Request Size)的作用机制,提供Linux内核五大配置策略的对比分析,并分享FPGA加速卡等实战案例的排查经验。文章还介绍了诊断工具链、动态调试技巧以及性能优化方法,帮助工程师有效解决PCIe兼容性难题。

weixin_30872499的博客 419

poj 3264(线段树)

http://poj.org/problem?id=3264 初学线段可以做的水题,也是线段树的基础运用。也是我的第一个线段树的题。 题意:在区间范围内的最大值减去最小值 思路:线段树记录下每个区间内的最大值以及最小值,然后查询。 我也是第一次做,然后耗时比较多。 1 #include <stdio.h> 2 #include <string.h&g...

A1823085974的博客 254

【实战分享】低成本K型热电偶测温方案:单运放放大与STM32 ADC采集优化

本文详细介绍了低成本K型热电偶测温方案,通过单运放放大与STM32 ADC采集优化,解决了信号微弱、负电压处理和冷端补偿等核心挑战。方案采用LMV358运放和T型网络降低噪声,结合STM32的ADC校准和软件滤波,实现±0.5℃精度,物料成本控制在15元以内,适用于工业测温场景。

weixin_30839881的博客 167

poj3264(简单线段树)

题目链接:https://vjudge.net/problem/POJ-3264 题意:线段树简单应用题,区间查询最大值和最小值的差。 思路:用线段树维护区间的最大值和最小值即可。 AC代码: #include<cstdio> #include<algorithm> using namespace std; const int maxn=50005; ...

weixin_33804582的博客 266

Rust 与 Golang 深度对决:从语法到应用场景的全方位解析

Rust 定义函数以fn关键字开头,参数需明确指定类型,返回值类型紧跟在箭头->之后。// 定义函数add,接收两个i32类型参数a和b,返回i32类型结果,功能为两数相加a + bRust 和 Golang 各有千秋,在不同领域展现独特优势。Rust 凭借强大的内存安全与高性能,在系统编程、网络编程以及对内存安全和性能要求高的场景中表现突出;Golang 则以简洁语法和出色并发性能,在云计算、分布式系统、Web 开发等领域占据一席之地。

muyouking 1769

[Dify实战] Bug 自动归因与复现指南生成器-带绑定资源

摘要: 本文介绍如何利用Dify构建"Bug自动归因与复现指南生成器",解决研发测试中的常见痛点。该系统可自动解析Bug信息,结构化输出复现步骤、可能原因、修复建议和回归测试点,显著提升问题处理效率。文章详细阐述了方案流程、Prompt设计要点、输出模板及落地清单,并提供了RCA分析模板和标准复现步骤示例。通过统一Bug描述模板和自动化分析,该系统能有效降低人力成本,加速问题闭环,适用于持续迭代的中大型项目。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 293

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.youkuaiyun.com/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差。 分析:按区间取值,很明显使用的线段树。区间大小取200000 * 4 = 8 * 10 ^5;           进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root]

svitter的专栏 1591

图神经网络在金融风险传播分析中的应用

《图神经网络在金融风险传播分析中的应用》 摘要 本文旨在探讨图神经网络(Graph Neural Networks, GNN)在金融风险传播分析中的应用。首先,本文简要介绍了图神经网络的基本概念、发展历史以及其在各类应用场景中的重要性。接着,详细介绍了图神经网络的核心算法,包括图卷积神经网

AI天才研究院 1279

POJ 3264

POJ 3264

transbit的博客 276

CVE-2021-3156本地提权漏洞复现

受影响版本 sudo: 1.8.2 - 1.8.31p2 sudo: 1.9.0 - 1.9.5p1 测试 我这里是在本地centos7上进行测试。 需要目标机器上存在python编译器、目标主机出网或者能上传文件到目标主机。 以非root用户登录系统,并运行如下命令: sudoedit -s / 通常响应一个以 sudoedit: / 开头的报错,多半存在漏洞。 利用 提权脚本: https://www.o2oxy.cn/wp-content/uploa

The current road is different, love needs to distinguish between right and wrong 3776

[RK-Linux] 移植Linux-5.10到RK3399(十)| 配置AP6256模组使能WIFI、BT功能

[RK-Linux] 移植Linux-5.10到RK3399(十)| 配置AP6256模组使能WIFI、BT功能

Systemcall名屋 2978

POJ 3264:Balanced Lineup(线段树应用:区间最大值与最小值之差)

POJ 3264:Balanced Lineup(线段树应用:区间最大值与最小值之差) 题目链接:POJ 3264 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 or...

serendipity的博客 885

软件设计师---计算机系统

软件设计师---计算机系统

我从不打没有准备的仗! 3万+

Balanced Lineup POJ - 3264(线段-求某区间的最大值与最小值的差)

一片绿地的N(1≤N≤50000)颗树排成一排,在一个美好的下午,园艺师傅接到任务对这N棵树修剪,园艺师傅已经知道这N颗树的高度H(1≤H≤1000000)。园艺师傅街道的任务有Q(1≤Q≤200000)个小任务,对于每一个小任务,园艺师傅都要知道特定区间最高的树和最矮的树的高度差。园艺师傅不会算法,这个任务对他有些难,你能帮助他吗? Input 第一行为N(1≤N≤50000)和Q(1≤Q≤200000);. 从第2行到第N+1行,每行一个数字, 表示第i棵树的高度(1≤height≤1000000);

Are_you_ready的博客 480

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

poj 3264 Balanced Lineup(查询区间最大值与最小值的差)

1.代码: #include #include #include #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define N 100000 int a[N]; int ST1[N][20]; int ST2[N][20]; int n,q; void make_ST() { for(i

24小时的程序员 915

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

poj3264Balanced Lineup(线段树)

poj3264Balanced Lineup Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 48911   Accepted: 22908 Case Time Limit: 2000MS Description

luckcircle的博客 349

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

POJ 3264 Balanced Lineup(线段树—求区间最大值与最小值差)

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41077 Accepted: 19305 Case Time Limit: 2000MS Description

Chunzhen的博客 607

Balanced Lineup(POJ 3264 线段树 )多次求任意区间最大值与最小值的差

Balanced Lineup(点击转到) Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64368   Accepted: 30003 Case Time Limit: 2000MS Description For the daily milking, Farmer John's ...

SYITwin的博客 503

[POJ 3264]Balanced Lineup[树状数组查询区间最大最小值]

题目链接:[POJ 3264]Balanced Lineup[树状数组查询区间最大最小值] 题意分析: 查询每个区间的最大值与最小值只差。 解题思路: 用树状数组查询。具体见博文:[HDU 1754]I Hate It[树状数组查询+更新区间最大值] 只是多了个查询最小值。没啥区别。 个人感受: 对最大值最小值的查询还是没掌握牢固,多瞅瞅。 具体代码如下: #include #i

闲停 2539

Balanced Lineup poj3264 线段树

Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最小值,然后做差就好了,基本上就是线段树模板题 代码实现 #include<cstdio> #include<cstring> #include<algorit...

weixin_30367169的博客 152

POJ 3264 Balanced Lineup【简单线段树,区间最值,无更新】

简单线段树,区间最值之差,没有更新。 注意细节呀。。。

AC_Dreameng 1196

poj 3264 Balanced Lineup

http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39375   Accepted: 18463 Case Time Limit: 2000MS Descr

大太阳 580

【RMQ】poj 3264 Balanced Lineup

RMQ 查询区间最值。 自然可以用线段树来做。 但对于序列值不改变的问题,查询可以用dp+位运算 其算法可以看作由区间dp演化而来,一般的数位dp需要用O(n^2)的时间复杂度来处理,预处理得到区间可以是任意的。 此时用功能的弱化来换时间。我们预处理只处理出区间 i->i+(2^j)-1 最值, 我们想得到 i->j 的最值可以由两个交叉的区间取最值来求(正因为求的是最值,所以区间

学无纸巾 2030

poj 3264 Balanced Lineup线段树区间最值差

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 50919   Accepted: 23849 Case Time Limit: 2000MS Description For the daily milking, Farme

The_star_is_at的博客 410

POJ-3264 Balanced Lineup(线段树代码讲解很详细)

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

weixin_44313771的博客 2208

POJ - 3264 Balanced Lineup [线段树] [最大值最小值]

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 ...

Prince_NYing的博客 273
上一篇: poj2299 Ultra-QuickSort(线段树计数问题)
下一篇: hdu4565 So Easy!(矩阵快速幂)
JinbaoSite0144
博客等级 码龄12年 231粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值