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]++. 将每个询问按左端点排序,再从左往右扫, ...
随机推荐
- day65--mysql数据库--索引、慢日志、分页
---恢复内容开始--- 一.索引 (一)介绍: 数据库中专门用于帮助用户快速查找数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取. (二)作用: ...
- opencv保存图片路径包含中文乱码解决方案
# coding: utf-8 import numpy as np import cv2 img = cv2.imread('1.jpg',1) cv2.imshow('image', img) k ...
- 20180223-logging模块
Python的logging模块提供了标准的日志接口,可以通过它存储各种格式的日志,logging的日志可以依次分为debug().info().warning().error().cirtical( ...
- linux firewall
一.查看防火墙状态1.首先查看防火墙是否开启,如未开启,需要先开启防火墙并作开机自启 systemctl status firewalld 开启防火墙并设置开机自启 systemctl start f ...
- css 当文字过多时以....省略
<!-- 公告 --> <p class="rst-promotion">公告: {{shopInfo.rst.promotion_info}}</p ...
- bzoj1002 轮状病毒 暴力打标找规律/基尔霍夫矩阵+高斯消元
基本思路: 1.先观察规律,写写画画未果 2.写程序暴力打表找规律,找出规律 1-15的答案:1 5 16 45 121 320 841 2205 5776 151 ...
- 人生苦短_我用Python_Try_Exception异常捕捉_007
# coding=utf-8 ''' request+try__异常处理 ''' import requests class HttpRequests: def __init__(self, url, ...
- Linux复制命令cp进阶
cp -a:连同属性和权限也一起复制 cp -i:如果不带该参数,默认覆盖同名文件而不会提醒. cp -u:只拷贝符合以下条件的文件到目标目录:目标目录中不存在的文件或目标目录中文件版本较旧的文件. ...
- QT中视图(setViewport)和窗口(setWindow) 小总结
参考博客: https://blog.csdn.net/weixin_39583140/article/details/92798127 https://blog.csdn.net/u01244271 ...
- flask02
-web框架 -从浏览器输入一个地址,发送请求,经历了哪些过程 -反向代理,正向代理 -正向代理代理客户 -反向代理代理服务器-你用过的python中的魔法方法:https://www.cnblogs ...