题目大意就是有很多牛。告诉你每只牛的高度。然后有很多个询问。输出该区间内的最大身高差。也就是用RMQ求最大值最小值。貌似还可以用线段树。然而,我还不会线段树。。。。。T_T

可能是太多组数据了。cin和cout会TLE。换成scanf和printf就顺当的AC了。。。。啦啦啦、

RMQ还是只会用模板。。T_T

附代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#define maxn 51000
#define maxm 20
using namespace std;

int dpmax[maxn][maxm];
int dpmin[maxn][maxm];
int n, q;

int main()
{
    while(cin >> n >> q)
    {
        for (int i=1; i<=n; ++i)   // 下标从开始。初始化dpmax和dpmin数组。
        {
           // cin >> dpmax[i][0];
            scanf("%d", &dpmax[i][0]);
            dpmin[i][0] = dpmax[i][0];
        }

int end_j = log(n + 0.0) / log(2.0); // 预处理。计算每个区间的最大值和最小值。所以询问非常快。
        for (int j=1; j<=end_j; ++j)
        {
            int end_i = n + 1 - (1 << j);  // dpmax[i][j] 表示从第 i 个数开始。长度为1<<j的区间。包括第i个数。
            for (int i=1; i<=end_i; ++i)
            {
                dpmax[i][j] = max(dpmax[i][j-1], dpmax[i+(1<<(j-1))][j-1]);
                dpmin[i][j] = min(dpmin[i][j-1], dpmin[i+(1<<(j-1))][j-1]);
            }
        }

for (int i=0; i<q; ++i)
        {
            int st, ed;
            cin >> st >> ed;
            int k = log(ed - st + 1.0) / log(2.0);   //需要讨论的最大区间长度。
            int ansmax = max(dpmax[st][k], dpmax[ed-(1<<k)+1][k]);
            int ansmin = min(dpmin[st][k], dpmin[ed-(1<<k)+1][k]);
            //cout << ansmax - ansmin << endl;
            printf("%d\n", ansmax - ansmin);
        }
    }
    return 0;
}

RMQ复习链接:http://blog.csdn.net/liang5630/article/details/7917702

POJ 3264 RMQ水题的更多相关文章

  1. poj 3264 RMQ 水题

    题意:找到一段数字里最大值和最小值的差 水题 #include<cstdio> #include<iostream> #include<algorithm> #in ...

  2. POJ 3264 RMQ裸题

    POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...

  3. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  4. Poj 1552 Doubles(水题)

    一.Description As part of an arithmetic competency program, your students will be given randomly gene ...

  5. poj 3264 RMQ

    直接写个RMQ就能过. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  6. 最小费用最大流模板 poj 2159 模板水题

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15944   Accepted: 8167 Descr ...

  7. POJ 1837 Balance 水题, DP 难度:0

    题目 http://poj.org/problem?id=1837 题意 单组数据,有一根杠杆,有R个钩子,其位置hi为整数且属于[-15,15],有C个重物,其质量wi为整数且属于[1,25],重物 ...

  8. poj 3264(RMQ或者线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42929   Accepted: 20184 ...

  9. POJ - 3090 gcd水题

    大概题意就是求\(1 \le i,j \le n\)的\(gcd(i,j) = 1\)的个数+2(对于0的特判) 正解应该是欧拉函数或者高逼格的莫比乌斯反演 但数据实在太水直接打表算了 /*H E A ...

随机推荐

  1. 20145330 《网络对抗》 Web基础

    20145330 <网络对抗> Web基础 1.实践内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建库.创 ...

  2. 字符串分割(C++)(转载)

    转载出自:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html 经常碰到字符串分割的问题,这里总结下,也方便我以 ...

  3. 高级版本VS打开低版本VS工程,无法调试的问题

    选中Debugging选项,在Command命令行里面输入生成exe文件的相对路径. 转载:http://blog.csdn.net/x931100537/article/details/405052 ...

  4. P3066 [USACO12DEC]逃跑的BarnRunning Away From

    目录 题目 思路 错误&&注意 代码 题目 luoguP3066 思路 虽说这个题目有多种做法,但 左偏树算法: 我们发现这个合并的时候并不好合并,因为存的值不是固定的 那我们是不是可 ...

  5. 【建表】ElasticSearch建表命令

    使用google插件Postman 发送方式为PUT,url地址: 192.168.2.11:9200/IndexName 文本raw,数据为json格式 { "settings" ...

  6. C# 用Linq查询DataGridView行中的数据是否包含(各种操作)

    http://blog.csdn.net/xht555/article/details/38685845 https://www.cnblogs.com/wuchao/archive/2012/12/ ...

  7. 3D CNN for Video Processing

    3D CNN for Video Processing Updated on 2018-08-06 19:53:57 本文主要是总结下当前流行的处理 Video 信息的深度神经网络的处理方法. 参考文 ...

  8. 《EMCAScript6入门》读书笔记——16.Generator函数的语法

    鼠标指针移到图片上,右键,选择在“在新标签页中打开”,放大即可看到清晰文字.

  9. BZOJ 1001: [BeiJing2006]狼抓兔子(s-t平面图+最短路求最小割)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 题意: 思路:这道题目是最小割题目,但是吧你直接套用Dinic是会超时的. 这里有种很奇妙的做 ...

  10. WiscKey: Separating Keys from Values in SSD-Conscious Storage [读后整理]

    WiscKey: Separating Keys from Values in SSD-Conscious Storage WiscKey是一个基于LSM的KV存储引擎,特点是:针对SSD的顺序和随机 ...