The Water Problem

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5443

Description

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

HINT

题意

查询区间最大值,不带修改

题解:

随便怎么写,反正数据范围只有1000

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 10505
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct node
{
int l,r;
int ma;
};
node a[maxn*];
int num[maxn];
void build(int x,int l,int r)
{
a[x].l=l,a[x].r=r;
if(l==r)
{
a[x].ma=num[l];
return;
}
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].ma=max(a[x<<].ma,a[x<<|].ma);
}
int query(int x,int l,int r)
{
int L=a[x].l,R=a[x].r;
if(l<=L&&R<=r)
return a[x].ma;
int mid=(a[x].l+a[x].r)>>;
if(r<=mid)
return query(x<<,l,r);
if(l>mid)
return query(x<<|,l,r);
return max(query(x<<,l,mid),query(x<<|,mid+,r));
}
int main()
{
int t=read();
while(t--)
{
memset(a,,sizeof(a));
int n=read();
for(int i=;i<=n;i++)
num[i]=read();
build(,,n);
int q=read();
for(int i=;i<q;i++)
{
int l=read(),r=read();
printf("%d\n",query(,l,r));
}
}
}

hdu 5443 The Water Problem 线段树的更多相关文章

  1. hdu 5443 The Water Problem(长春网络赛——暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java ...

  2. hdu 5443 The Water Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Description In Land waterless, ...

  3. 【线段树】HDU 5443 The Water Problem

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 题目大意: T组数据.n个值,m个询问,求区间l到r里的最大值.(n,m<=1000) ...

  4. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  5. HDU 5443 The Water Problem (ST算法)

    题目链接:HDU 5443 Problem Description In Land waterless, water is a very limited resource. People always ...

  6. 2015上海网络赛 HDU 5475 An easy problem 线段树

    题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. #include<iostream> #include<cstdio> #include<cstr ...

  7. ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

    Problem Description In Land waterless, water is a very limited resource. People always fight for the ...

  8. HDU 5443 The Water Problem (水题,暴力)

    题意:给定 n 个数,然后有 q 个询问,问你每个区间的最大值. 析:数据很小,直接暴力即可,不会超时,也可以用RMQ算法. 代码如下: #include <cstdio> #includ ...

  9. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. HDU 1503 Advanced Fruits (LCS,变形)

    题意: 给两个水果名,要求他们的LCS部分只输出1次,其他照常输出,但是必须保持原来的顺序! 思路: 求LCS是常规的,但是输出麻烦了,要先求LCS,再标记两串中的所有LCS字符,在遇到LCS字符时, ...

  2. ↗☻【PHP与MySQL程序设计 #BOOK#】第3章 PHP基础

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  3. Maven配置文件Pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 "      xmlns:xsi="http://www.w3. ...

  4. Js动态传递不定数目的参数

    回调程序中,经常有这样的需求:用户传递一个回调方法,该方法可以有不定的参数. 如果参数数目固定则很容易实现,看代码: //回调函数1 function callback1(a,b,c) { alert ...

  5. NET下RabbitMQ实践[实战篇]

    之前的文章中,介绍了如何将RabbitMQ以WCF方式进行发布.今天就介绍一下我们产品中如何使用RabbitMQ的!          在Discuz!NT企业版中,提供了对HTTP错误日志的记录功能 ...

  6. 从linux启动到rootfs的挂载分析

    简单的来说,根文件系统包括虚拟根文件系统和真实根文件系统.在Kernel启动的初始阶段,首先去创建虚拟的根文件系统,接下来再去调用do_mount来加载真正的文件系统,并将根文件系统切换到真正的文件系 ...

  7. 利用文件实现Free Pascal中的简单排序功能

    此程序主要是验证文件功能的读写功能,总结到的东西有:①文件无论是读还是写,都要先建立链接关系才可以进行;②读与写不能同时进行,必须分开操作,这也可以理解,在实际鼠标操作时也是如此的!③读写后必须用cl ...

  8. 转载--PHP json_encode() 和json_decode()函数介绍

    转自:http://www.nowamagic.net/php/php_FunctionJsonEncode.php 转自:http://www.jb51.net/article/30489.htm ...

  9. class0513(html基础加强)

    内容:HTML.CSS 目标:掌握手写HTML实现一般难度的Web页面的能力(如网站注册表单),为ASP.Net学习打基础.坚持手写HTML,可视化设计只是一种自学的手段. 参考书:张孝祥<Ja ...

  10. 在iptables中添加vnc访问规则

    iptables -I INPUT -p tcp --dport 5800|5900|5801|5901 -j ACCEPT service iptales save