zoukankan      html  css  js  c++  java
  • poj 3264 线段树 求区间最大最小值

    Description

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

    Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

    Input

    Line 1: Two space-separated integers, N and Q. Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

    Output

    Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

    Sample Input

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    Sample Output

    6
    3
    0

    Source

     
     
    线段树 处理该问题   存储区间 最大值与最小值
     
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #define INF 0xfffffff
    using namespace std;
    struct tree
    {
        int L;
        int R;
        int minv;
        int maxv;
        int mid()
        {
            return (L+R)/2;
        }
    }tree[800010];
    int maxv=-INF;
    int minv=INF;
    void buildtree(int root,int L,int R)
    {
        tree[root].L=L;
        tree[root].R=R;
        tree[root].minv=INF;
        tree[root].maxv=-INF;
        if(L!=R)
        {
            buildtree(2*root+1,L,(L+R)/2);
            buildtree(2*root+2,(L+R)/2+1,R);
        }
    }
    void inse(int root,int i,int v)
    {
        if(tree[root].L==tree[root].R)
        {
            tree[root].minv=v;
            tree[root].maxv=v;
            return ;
        }
        tree[root].minv=min(tree[root].minv,v);
        tree[root].maxv=max(tree[root].maxv,v);
        if(i<=tree[root].mid())
            inse(2*root+1,i,v);
        else
            inse(2*root+2,i,v);
    }
    void query(int root ,int s,int e)
    {
        if(tree[root].minv>minv&&tree[root].maxv<maxv)
            return ;
        if(tree[root].L==s&&tree[root].R==e)
        {
            minv=min(minv,tree[root].minv);
            maxv=max(maxv,tree[root].maxv);
            return ;
        }
        if(e<=tree[root].mid())
            query(2*root+1,s,e);
        else if(s>tree[root].mid())
            query(2*root+2,s,e);
        else
        {
            query(2*root+1,s,tree[root].mid());
            query(2*root+2,tree[root].mid()+1,e);
        }
    }
    int main()
    {
        int n,q;
        int re;
        int ss,ee;
            scanf("%d%d",&n,&q);
            buildtree(0,1,n);
            for(int i=1; i<=n; i++)
            {
                //cout<<"**********"<<endl;
                scanf("%d",&re);
                inse(0,i,re);
            }
            for(int j=1; j<=q; j++)
            {
                scanf("%d%d",&ss,&ee);
                minv=INF;
                maxv=-INF;
                query(0,ss,ee);
                printf("%d
    ",maxv-minv);
            }
    
    return 0;
    }
    View Code
     
  • 相关阅读:
    数字重排
    环游世界
    Hibernate笔记(一)增删改查CRUD
    Sliding Window Algorithm 滑动窗口算法
    纯HTML/CSS/JS实现淘宝、京东两种轮播图
    COA计算机组织与结构笔记
    数据结构与算法笔记:最小生成树Kruskal、Prim算法与JAVA实现
    数据结构与算法笔记:图的基础与JAVA实现
    数据结构与算法:并查集
    JDBC学习笔记
  • 原文地址:https://www.cnblogs.com/hsd-/p/4671725.html
Copyright © 2011-2022 走看看