题目连接

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

The Water Problem

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 $a_1, a_2, a_3, . . . , a_n$ 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 $a_l$ and $a_r$.

Input

First you are given an integer $T\ (T \leq 10)$ indicating the number of test cases. For each test case, there is a number $n\ (0 \leq n \leq 1000)$ on a line representing the number of water sources. n integers follow, respectively $a_1, a_2, a_3, . . . , a_n,$ and each integer is in $\{1, . . . , 10^6\}$. On the next line, there is a number $q\ (0 \leq q \leq 1000)$ representing the number of queries. After that, there will be $q$ lines with two integers $l$ and $r\ (1 \leq l \leq r \leq 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

裸的rmq问题,st表水之。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::max;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
int n, arr[N], st[N + 5][11];
struct SparseTable {
inline void init() {
rep(i, n) st[i][0] = arr[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}
inline int rmq(int a, int b) {
int k = __builtin_clz((int)1) - __builtin_clz(b - a + 1);
return max(st[a][k], st[b - (1 << k) + 1][k]);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, a, b, q;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
rep(i, n) scanf("%d", &arr[i]);
go.init();
scanf("%d", &q);
while(q--) {
scanf("%d %d", &a, &b);
printf("%d\n", go.rmq(a - 1, b - 1));
}
}
return 0;
}

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 线段树

    The Water Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

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

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

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

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

  5. 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 ...

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

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

  7. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. HDU 5832 A water problem (带坑水题)

    A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  9. HDU 5832 A water problem 水题

    A water problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

随机推荐

  1. com学习(四)——用 ATL 写第一个组件

    建立 ATL 工程 步骤2.1:建立一个工作区(WorkSpace). 步骤2.2:在工作区中,建立一个 ATL 工程(Project).示例程序叫 Simple1,并选择DLL方式,见图一. 图一. ...

  2. CODEVS 1036 商务旅行

    题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任 ...

  3. php json中文处理方法,请json更懂中文

    1.php5.3版本及以下.的处理方式 /** *php5.3版本以前,json中文问题的解决解决方案 */ function encode_json($str) { return urldecode ...

  4. 二模10day2解题报告

    T1.最多因子数(divisors) 给出范围l,r求其中约数和最大的最小整数. 非常深井冰的题目:如果特判加暴力的话分数低的可怜 AC做法要用到分解质因数和线性筛(这俩好写),然而,一个一个枚举还是 ...

  5. iOS百度地图探索

    新建工程后,几项准备: 1.工程中一个文件设为.mm后缀 2.在Xcode的Project -> Edit Active Target -> Build -> Linking -&g ...

  6. [前端 1] 使用frameset框架构建网页基本布局

    导读:在做项目的过程中,发现网页的一些地方是不变的,比如说顶部.底部.而变幻的内容就只是一部分.这个时候在想,这是每次都刷新一个界面呢,还是有别的快捷方法呢.然后就找到了frameset 这个东西.本 ...

  7. 线程中CreateEvent和SetEvent及WaitForSingleObject的用法

    首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面. CreateEvent 函功能描述:创建或打开一个命名的或无名的事件对象. EVENT有两种状态 ...

  8. hql 关联查询

    HQL 带的连接语句只能是实体与 该实体的属性 进行连接 其意义就是为了优化(通过延迟加载查询关联的属性)需要进行配置 from A left join A.B where (b.flag is nu ...

  9. 【Linux】基于Bind_DLZ和MySQL数据的DNS搭建

    参考:http://www.linuxfromscratch.org/blfs/view/cvs/server/bind.html 参考:http://bind-dlz.sourceforge.net ...

  10. MallBuilder 多用户商城管理系统 v5.8.1.1

    MallBuilder是一款基于PHP+MYSQL的多用户网上商城解决方案.利用MallBuilder可以快速建立一个功能强大的类似京东商城.天猫商城.1号店商城的网上商城,或企业.行业化.本地化和垂 ...