zoukankan      html  css  js  c++  java
  • rmq问题

    对于rmq问题一种常用的经典方法就是使用st表解决

    由于该方法代码量少所以还是学习一下这种思想

    缺点:dp数组需要多开log倍的大小 对于一些题目正常的数据范围容易内存超限

    基本思想:使用dp的方法,dp出i到i+2^j的所有区间 然后查询

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    int maxnum[50010][20];
    int minnum[50010][20];
    int main()
    {
        int num,n,q,i,j,x,y;
        while (~scanf("%d%d",&n,&q))
        {
            for (i = 1; i <= n; i ++)
            {
                scanf("%d",&num);
                maxnum[i][0] = minnum[i][0] = num;   // 先预处理2^0长度的 
            }
            
            for (j = 1; (1<<j) <= n; j ++)
             for (i = 1; i+(1<<j)-1 <= n; i ++)   // 预处理 
             {                                                 
                maxnum[i][j] = max(maxnum[i][j-1],maxnum[i + (1<<(j-1))][j-1]);
                minnum[i][j] = min(minnum[i][j-1],minnum[i + (1<<(j-1))][j-1]);
             }
                
            while (q --)
            {
                scanf("%d%d",&x,&y);
                int z = 0;
                while ((1<<(z+1)) <= y-x+1) z ++;
                int ans = max(maxnum[x][z],maxnum[y-(1<<z)+1][z])
                         - min(minnum[x][z],minnum[y-(1<<z)+1][z]);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    库函数文件操作
    系统文件操作函数
    time函数
    字符(串)输入输出函数
    select&epoll
    epoll
    select
    Apache 配置虚拟主机三种方式
    Apache VirtualHost配置
    Scrapy中用xpath/css爬取豆瓣电影Top250:解决403HTTP status code is not handled or not allowed
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852196.html
Copyright © 2011-2022 走看看