POJ3246
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
#include<algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
int a[];
int dp[][];//2^16长度
int DP[][];
int n,q;
void ST()
{
for (int i = ; i <=n ; ++i) {
dp[i][]=a[i];
DP[i][]=a[i];
} for (int j = ; (<<j) <=n ; ++j) {
for (int i = ; i+(<<j)- <= n ; ++i) {
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);//需要注意+的优先级高于<<
DP[i][j]=max(DP[i][j-],DP[i+(<<(j-))][j-]);
}
}
}
int main()
{
scanf("%d%d",&n,&q);
for (int i = ; i <=n ; ++i) {
scanf("%d",&a[i]);
}
ST();
int x,y;
for (int i = ; i <q ; ++i) {
scanf("%d%d",&x,&y);
int m=floor(log((double)(y-x+))/log(2.0));
int MAX=max(DP[x][m],DP[y-(<<m)+][m]);
int MIN=min(dp[x][m],dp[y-(<<m)+][m]);
printf("%d\n",MAX-MIN);
}
return ;
}
POJ3246的更多相关文章
- POJ3368(RMQ)
Frequent values Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreas ...
随机推荐
- thinkphp 创建数据对象之data方法
创建数据对象:data()方法 1.功能:给模型对象$data赋值,将模型对象转化为数据对象 tip:模型对象与数据对象之间就差一个赋过值的$data; 2.方法:data()其源码如下: tip:源 ...
- The tenth day
Why the long face? 你为什么不高兴,你为什么扳着脸,愁眉苦脸. Why the long face? Are you feeling down again? 你脸怎么这么臭,你又心情 ...
- 基于表单布局:分析过时的table结构与当下的div结构
一些话在前面 最近做了百度前端学院一个小任务,其中涉及到表单布局的问题, 它要处理的布局问题:左边的标签要右对齐,右边的输入框.单选按钮等要实现左对齐. 从开始入门就被告知table布局已经过时了,当 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- JSP 里 的 basePath
Eclipse新建JSP页面的时候不会加上 base 这个变量,需要手动添加,经常忘记,MyEclipse 就不用管了会自动添加. 如果忘了下面代码直接copy即可: <% String pat ...
- C# 对XML操作-实例
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- js从入门到精通到深入到就业
本篇博客是我参看人家代码做的总结,个人感觉非常非常好,简单.步步深入,不用花大量时间来学完正本js,只需要把其中的代码理解透彻,上班无压力(上班无压力是指js部分,包括查看框架源代码都有很大帮助) / ...
- react及flux架构范例Todomvc分析
react及flux架构范例Todomvc分析 通过分析flux-todomvc源码,学习如何通过react构建web程序,了解编写react应用程序的一般步骤,同时掌握Flux的单向数据流动架构思想 ...
- hdu-2688 Rotate---树状数组+模拟
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2688 题目大意: 给你n数,(n<=3e6),有两个操作,Q为 当前有多少对数,满足严格递增, ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) G】置置置换(动态规划)
点此看题面 大致题意: 求出有多少个长度为\(n\)的排列满足对于奇数位\(a_{i-1}<a_i\),对于偶数位\(a_{i-1}>a_i\). 考虑打表? 考虑每次只有一个数\(n\) ...