poj3264Balanced Lineup(倍增ST表)
2024-08-30 06:31:30
Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 52328 | Accepted: 24551 | |
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.
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
//先敲个板子
#include<iostream>
#include<cstdio>
#include<cstring> #define maxn 1000000 using namespace std;
int n,m,ans,x,y,a[maxn],p[maxn];
int f1[maxn][],f2[maxn][]; inline int init()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;} void ST()
{
for(int i=;i<=n;i++)
f1[i][]=f2[i][]=a[i];
for(int j=;j<=;j++)
for(int i=;i+(<<j)-<=n;i++)
{
f1[i][j]=min(f1[i][j-],f1[i+(<<j-)][j-]);
f2[i][j]=max(f2[i][j-],f2[i+(<<j-)][j-]);
}
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
if((<<j)>i)
{
p[i]=j-;
break;
}
} int query(int l,int r)
{
int k=p[r-l+];
int ans1=min(f1[l][k],f1[r-(<<k)+][k]);
int ans2=max(f2[l][k],f2[r-(<<k)+][k]);
return ans2-ans1;
} int main()
{
n=init();m=init();
for(int i=;i<=n;i++)
a[i]=init();
ST();
for(int i=;i<=m;i++)
{
x=init();y=init();
printf("%d\n",query(x,y));
}
return ;
}
最新文章
- Block 代码快
- Leetcode-190 Reverse Bits
- 初识JavaScript 变量, 操作符, 数组
- Vigen&#232;re密码
- php操作mongodb中的ISODate格式日期
- Face++ – 提供给你实时的脸部识别 API
- web项目的日志打印位置设置
- Android动画效果生动有趣的通知NiftyNotification(Android Toast替代品)
- hdu 3790 最短路径问题(最短路,Dijsktra)
- 迷途指针 new delete
- Quartz Scheduler 开发指南(1)
- (转) Spring读书笔记-----Spring的Bean之配置依赖
- 双向链表JAVA代码
- sql优化-总结
- Linux通过使用Sambaserver示例
- ESFramework 4.0 快速上手(01) -- Rapid引擎
- 【BZOJ 1002】: [FJOI2007]轮状病毒
- python3操作MySQL数据库,一次插入多条记录的方法
- Python学习计划
- &#39;Tensorboard.util&#39; has no attribute &#39;Retrier&#39; - &#39;Tensorboard.util&#39;没有属性&#39;Retrier&#39;