zoukankan      html  css  js  c++  java
  • 51Nod 1174 区间中最大的数(RMQ)

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 const int maxn = 10000 + 5;
     7 int a[maxn];
     8 int f[maxn][15];
     9 
    10 void rmq(int cnt){
    11     memset(f, 0, sizeof(f));
    12     for (int i = 1; i <= cnt; i++){
    13         f[i][0] = a[i];
    14     }
    15 
    16     //O(nlogn)
    17     for (int j = 1; j < 15; j++){
    18         for (int i = 1; i <= cnt; i++){
    19             if (i + (1 << j) - 1 <= cnt){
    20                 f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
    21             }
    22         }
    23     }
    24 }
    25 
    26 
    27 int Query(int x, int y){
    28     int k = 0;
    29     while ((1 << (k + 1)) <= y - x + 1)
    30         k++;      
    31     return max(f[x][k], f[y - (1 << k) + 1][k]);
    32 }
    33 
    34 int main(){
    35     ios::sync_with_stdio(false);
    36 
    37     int n;
    38     cin >> n;
    39     for (int i = 1; i <= n; i++){
    40         cin >> a[i];
    41     }
    42     rmq(n);
    43     int m;
    44     cin >> m;
    45     while (m--){
    46         int x, y;
    47         cin >> x >> y;
    48         x++, y++;
    49         int ans = Query(x, y);
    50         cout << ans << endl;
    51     }
    52 
    53     //system("pause");
    54     return 0;
    55 }
  • 相关阅读:
    P1052 过河
    P1004 方格取数
    自定义事件
    自定义单选,多选按钮
    构造函数+原型的js混合模式
    图标
    格式化
    时间 ---- 时间简史
    居中
    插入DOM元素
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8666599.html
Copyright © 2011-2022 走看看