In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,an representing the size of the water source. Given a set of queries each containing 2 integers l and r, please find out the biggest water source between al and ar.

Input

First you are given an integer T(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an, and each integer is in {1,...,106}. On the next line, there is a number q(0≤q≤1000) representing the number of queries. After that, there will be q lines with two integers l and r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source.

Output

For each query, output an integer representing the size of the biggest water source.

Sample Input

3

1

100

1

1 1

5

1 2 3 4 5

5

1 2

1 3

2 4

3 4

3 5

3

1 999999 1

4

1 1

1 2

2 3

3 3

Sample Output

100

2

3

4

4

5

1

999999

999999

1

求区间内最大的水源,之前区间dp没法做,会T,用rmq

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e3+10;
int a[N],MAX[N][20];
void first(int n)
{
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
MAX[i][j]=max(MAX[i][j-1],MAX[i+(1<<(j-1))][j-1]);
//MIN[i][j]=min(MIN[i][j-1],MIN[i+(1<<(j-1))][j-1];
}
}
}
int solve(int l,int r)
{
int x=0;
while(l-1+(1<<x)<=r) x++;
x--;
return max(MAX[l][x],MAX[r-(1<<x)+1][x]);
}
int main()
{
int re,n,tot,l,r;scf(re);
while(re--)
{
mm(a,0);
mm(MAX,0);
scf(n);
rep(i,1,n+1)
{
scf(a[i]);
MAX[i][0]=a[i];
}
first(n);
scf(tot);
while(tot--)
{
scff(l,r);
prf(solve(l,r));
}
}
return 0;
}

最新文章

  1. Django URL的命令空间
  2. 到底AR初创公司Magic Leap是不是骗子?我看未必
  3. Flask Restful Small Demo
  4. delphi 取得汉字的第一个字母
  5. SharePoint Srver 2010 资源汇总
  6. SpringMVC源码解析 - HandlerMethod
  7. OpengGL ES2.0 Using NDK
  8. Linux系统编程(11)——进程间通信之有名管道
  9. Knockout应用开发指南 第三章:绑定语法(1)
  10. 姿势体系结构的详细解释 -- C
  11. 百度地图 iOS SDK - 坐标转换方法
  12. VMWare安装Mac系统后无法全屏显示的问题
  13. EF实体实现链接字符串加密
  14. Inno setup 操作注册表操作参数详解
  15. python测试开发django-57.xadmin选项二级联动
  16. 剪格子---(dfs回溯)
  17. Hadoop学习笔记(一)——编译安装和配置
  18. 解决 IllegalArgumentException: Could not resolve placeholder in string value &quot;${XXXXXX}&quot;
  19. ABOUT ME/OI回忆录
  20. Next Permutation &amp; Previous Permutation

热门文章

  1. C#实战技能之WebApi+Task+WebSocket
  2. 使用 vscode + chrome debuger断点调试 Vue 程序
  3. itchat
  4. too much recursion(太多递归)Uncaught RangeError: Maximum call stack size exceeded BootstrapValidator报错
  5. netty中的ChannelHandler和ChannelPipeline
  6. Android编码学习之Adapter
  7. CoreGraphics之CGContextSaveGState与UIGraphicsPushContext
  8. [HDFS Manual] CH1 HDFS体系结构
  9. hdoj:2055
  10. Linux解压缩命令tar