• 九度oj 1544 数字序列区间最小值


    原题链接:http://ac.jobdu.com/problem.php?pid=1544

    RMQ线段树。。。

    如下:

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 using std::min;
     7 const int Max_N = 101000;
     8 const int INF = 0x7fffffff;
     9 struct SegTree{
    10     struct Node{
    11         int v;
    12     }seg[Max_N << 2];
    13     void init(int n){
    14         built(1, 1, n);
    15     }
    16     inline void built(int root, int l, int r){
    17         if (l == r){
    18             scanf("%d", &seg[root].v);
    19             return;
    20         }
    21         int mid = (l + r) >> 1;
    22         built(root << 1, l, mid);
    23         built(root << 1 | 1, mid + 1, r);
    24         seg[root].v = min(seg[root << 1].v, seg[root << 1 | 1].v);
    25     }
    26     inline int query(int root, int l, int r, int x, int y){
    27         if (x > r || y < l) return INF;
    28         if (x <= l && y >= r){
    29             return seg[root].v;
    30         }
    31         int mid = (l + r) >> 1;
    32         int v1 = query(root << 1, l, mid, x, y);
    33         int v2 = query(root << 1 | 1, mid + 1, r, x, y);
    34         return min(v1, v2);
    35     }
    36     inline void gogo(int n){
    37         int m, a, b;
    38         scanf("%d", &m);
    39         while (m--){
    40             scanf("%d %d", &a, &b);
    41             printf("%d
    ", query(1, 1, n, a, b));
    42         }
    43     }
    44 }sg;
    45 int main(){
    46 #ifdef LOCAL
    47     freopen("in.txt", "r", stdin);
    48     freopen("out.txt", "w+", stdout);
    49 #endif
    50     int n;
    51     while (~scanf("%d", &n)){
    52         sg.init(n), sg.gogo(n);
    53     }
    54     return 0;
    55 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    CopyOnWriteArrayList
    Gradle version 2.2 is required. Current version is 2.10
    install mysql on ubuntu
    A<T extends B> and A <? extends B>
    java event listeners and dispatcher
    git
    linux patch usage
    Codeforces Round #404 (Div. 2) C 二分查找
    dijkstra算法模板及其用法
    Sublime Text 3 快捷键精华版
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4477634.html
Copyright © 2020-2023  润新知