kuangbin专题七 POJ3264 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 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
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 线段树维护最大最小,不涉及更改,只用pushup query就可以了
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=;
int minn[maxn<<],maxx[maxn<<],n,q,a[maxn],maxpos,minpos; void pushup(int rt) {
minn[rt]=min(minn[rt<<],minn[rt<<|]);
maxx[rt]=max(maxx[rt<<],maxx[rt<<|]);
} void build(int rt,int L,int R){
minn[rt]=;
maxx[rt]=;
if(L==R) {
scanf("%d",&a[rt]);
minn[rt]=maxx[rt]=a[rt];
return;
}
int mid=(L+R)>>;
build(rt<<,L,mid);
build(rt<<|,mid+,R);
pushup(rt);
} void query(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) {
minpos=min(minpos,minn[rt]);
maxpos=max(maxpos,maxx[rt]);
return;
}
int mid=(L+R)>>;
if(l<=mid) query(rt<<,L,mid,l,r);
if(r>mid) query(rt<<|,mid+,R,l,r);
} int main() {
while(~scanf("%d%d",&n,&q)) {
build(,,n);
int l,r;
while(q--) {
maxpos=-,minpos=inf;
scanf("%d%d",&l,&r);
query(,,n,l,r);
printf("%d\n",l==r?:maxpos-minpos);
}
}
}
kuangbin专题七 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 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
随机推荐
- [0day]jQuery Mobile XSS
漏洞影响范围: 任何一个website使用了 jQuery Mobile 并且开放了重定向都有可能存在XSS,并且目前还没有相关补丁信息. 应用介绍: jQuery Mobile是jQuery 框架的 ...
- leetcode589
树的先序遍历,使用递归实现. class Solution { public: vector<Node> Tree; void preTree(Node node) { Tree.push ...
- python爬虫(3)--异常处理
1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的服务器 服务器不存在 在代码中,我们需要用try-except语句来包围并捕获相应的异常. ...
- JavaScript中常用的函数
javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数 1.常规函数 javascript常规函数包括以下9个函数: (1)alert函数 ...
- 通过测试确定GCC中 INT DOUBLE的最大/最小值和精度(DOUBLE)
INT 确定最大/最小值 由于达到极限之后会变符号,直接循环判断条件即可 DOUBLE确定精度 设置一个DOUBLE变量初始值为1/3.0,每次*10,然后取整数部分,当两次的结果相同时说明已经到最大 ...
- 【总结整理】IFeatureBuffer
IFeatureBuffer pRowBuffer = objTabWYDCQ_Tar.CreateFeatureBuffer(); pRowBuffer.Shape = SourceRow.Shap ...
- css知多少(3)——样式来源与层叠规则(转)
css知多少(3)——样式来源与层叠规则 上一节<css知多少(2)——学习css的思路>有几个人留言表示思路很好.继续期待,而且收到了9个赞,我还是比较欣慰的.没看过的朋友建议先去看 ...
- POJ 3714 分治/求平面最近点对
第一次见这种问题直接懵圈...没想到分治法这么强大,借鉴了lyd的代码: 代码如下 #include<cstdio> #include<algorithm> #include& ...
- Windows系统 安装 Qt 5.7.0
Windows系统 安装 Qt 5.7.0 我们的电脑系统:Windows 10 64位 Qt5 软件:Qt 5. 7. 0 下载 Qt 5.7.0 软件 在这个网站里面,下载:Qt 5.7.0 fo ...
- Luogu 3943 星空
原题是CF79D Password 很妙的题. 首先我们发现区间操作不太好弄,我们想办法把它转化成单点操作,这样子处理的办法会多一点. 方法当然是差分了. 定义差分数组$b_i = a_i \^ a_ ...