poj3264 Balanced Lineup(树状数组)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 64655 | Accepted: 30135 | |
| 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
Source
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
#define MAX 50005
int Max[MAX],Min[MAX];
int a[MAX];
int n,q;
inline void read(int &x){
char ch;
bool flag=false;
for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
x=flag?-x:x;
}
inline void write(int x){
static const int maxlen=;
static char s[maxlen];
if (x<) { putchar('-'); x=-x;}
if(!x){ putchar(''); return; }
int len=; for(;x;x/=) s[len++]=x % +'';
for(int i=len-;i>=;--i) putchar(s[i]);
}
int lowbit(int x)
{
return x&-x;
}
void updata(int i,int val)
{
while(i<=n)
{
Min[i]=min(Min[i],val);
Max[i]=max(Max[i],val);
i+=lowbit(i);
}
}
int query(int l,int r)
{
int maxn=a[l],minn=a[r];
while()
{
maxn=max(maxn,a[r]),minn=min(minn,a[r]);
if(l==r) break;
for(r-=;r-l>=lowbit(r);r-=lowbit(r))
maxn=max(Max[r],maxn),minn=min(minn,Min[r]);
}
return maxn-minn;
}
int main()
{
memset(Min,INF,sizeof(Min));
read(n);read(q);
for(int i=;i<=n;i++){
read(a[i]);
updata(i,a[i]);
}
while(q--)
{
int a,b;
read(a),read(b);
write(query(a,b));
putchar('\n');
}
return ;
}
时间复杂度近似为log2(n)。
poj3264 Balanced Lineup(树状数组)的更多相关文章
- Balanced Lineup(树状数组 POJ3264)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...
- 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...
- 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)
维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...
- [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)
传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...
- [USACO17JAN]Balanced Photo平衡的照片 (树状数组)
题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include ...
- st表树状数组入门题单
预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...
- 第十二届湖南省赛G - Parenthesis (树状数组维护)
Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
随机推荐
- 前端对base64编码的理解,原生js实现字符base64编码
目录 常见对base64的认知(不完全正确) 多问一个为什么,base64到底是个啥? 按照我们的思路实现一下 到这里基本就实现了,结果跟原生的方法打印的是一样的 下一次 @( 对于前端工程师来说ba ...
- 微信小程序(9)--音频及视频弹幕
记录微信小程序音频及视频弹幕播放效果. 1.audio <!-- audio.wxml --> <audio poster="{{poster}}" name=& ...
- 一、RequireHttps
一.RequireHttps 强制使用Https重新发送请求:如: public class HomeController : Controller { [RequireHttps] public A ...
- 洛谷P3830 随机树(SHOI2012)概率期望DP
题意:中文题,按照题目要求的二叉树生成方式,问(1)叶平均深度 (2)树平均深度 解法:这道题看完题之后完全没头绪,无奈看题解果然不是我能想到的qwq.题解参考https://blog.csdn.ne ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【串线篇】sql注入问题
一.什么是SQL注入?? 二.mybatis是如何避免这种情况的呐? 这就得益于mybatis在底层实现时使用预编译语句.数据库在执行该语句时,直接使用预编译的语句,然后用传入的userId替换占位符 ...
- [Luogu1821][USACO07FEB]银牛派对Silver Cow Party
由题意可知,我们需要求的是很多个点到同一个店的最短距离,然后再求同一个点到很多个点的最短距离. 对于后者我们很好解决,就是很经典的单源最短路径,跑一边dijkstra或者SPFA即可. 然而对于前者, ...
- Servlet接口和web.xml配置文件初识
一.Java Web核心Servlet 1.什么是Servlet? Servlet是运行在服务器端的Java小程序,是sun公司提供的一套规范,用来处理客户端请求.响应给浏览器的动态资源.但Servl ...
- nyoj 471:好多的树(容斥原理)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=471 还是直接上代码.. #include<bits/stdc++.h> u ...
- Jenkins之配置GitHub-Webhook2
什么是持续集成(Continuous integration) 提出者Martin Fowler本人对持续集成是这样定义的:持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每 ...