zoukankan      html  css  js  c++  java
  • 线段树比大小

     
    Language:
    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 68059   Accepted: 31607
    Case Time Limit: 2000MS

    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 ≤ A ≤ B ≤ N), 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

    USACO 2007 January Silver

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int a[50000+5];
    int n,q,w,e;
    int maxx,minn;
    
    struct seg
    {
        int l,r,ma,mi;
    };
    seg tree[200000+5];

     利用递归形成树;

    void build(int s,int l,int r)
    {
        tree[s].l=l;
        tree[s].r=r;
        if(l==r)
        {
            tree[s].ma=a[l];
            tree[s].mi=a[l];
            return;
        }
        int m=(l+r)/2;
        build(s*2,l,m);
        build(s*2+1,m+1,r);
        tree[s].ma=max(tree[s*2].ma,tree[s*2+1].ma);
        tree[s].mi=min(tree[s*2].mi,tree[s*2+1].mi);
    }
    void query(int s,int x,int y)
    {
        if(tree[s].l==x&&tree[s].r==y)
        {
            minn=min(minn,tree[s].mi);
            maxx=max(maxx,tree[s].ma);
            return;
        }
        int m=(tree[s].l+tree[s].r)/2;
        if(y<=m)
        {
            query(s*2,x,y);
        }
        else if(x>m)
        {
            query(s*2+1,x,y);
        }
        else
        {
            query(s*2,x,m);
            query(s*2+1,m+1,y);
        }
    }

     

     

     

     

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int a[50000+5];
    int n,q,w,e;
    int maxx,minn;
    
    struct seg
    {
        int l,r,ma,mi;
    };
    seg tree[200000+5];
    
    void build(int s,int l,int r)
    {
        tree[s].l=l;
        tree[s].r=r;
        if(l==r)
        {
            tree[s].ma=a[l];
            tree[s].mi=a[l];
            return;
        }
        int m=(l+r)/2;
        build(s*2,l,m);
        build(s*2+1,m+1,r);
        tree[s].ma=max(tree[s*2].ma,tree[s*2+1].ma);
        tree[s].mi=min(tree[s*2].mi,tree[s*2+1].mi);
    }
    
    void query(int s,int x,int y)
    {
        if(tree[s].l==x&&tree[s].r==y)
        {
            minn=min(minn,tree[s].mi);
            maxx=max(maxx,tree[s].ma);
            return;
        }
        int m=(tree[s].l+tree[s].r)/2;
        if(y<=m)
        {
            query(s*2,x,y);
        }
        else if(x>m)
        {
            query(s*2+1,x,y);
        }
        else
        {
            query(s*2,x,m);
            query(s*2+1,m+1,y);
        }
    }
    
    int main()
    {
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        build(1,1,n);
        for(int i=1;i<=q;i++)
        {
            scanf("%d%d",&w,&e);
            minn=1000000+10;
            maxx=0;
            query(1,w,e);
            printf("%d
    ",maxx-minn);
        }
        return 0;
    }
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=50000+5;
    struct seg{
        int l,r,ma,mi;
    }tree[maxn*3+1];
    int a[maxn];
    int maxx,minn;
    int n,q,a1,b;
    void build(int s,int l,int r){
        tree[s].l=l;
        tree[s].r=r;
        if(l==r){
            tree[s].ma=a[l];
            tree[s].mi=a[l];
            return;
        }else{
            int mid=(tree[s].l+tree[s].r)/2;
            build(s*2,l,mid);
            build(s*2+1,mid+1,r);
            tree[s].ma=max(tree[s*2].ma,tree[s*2+1].ma);
            tree[s].mi=min(tree[s*2].mi,tree[s*2+1].mi);
        }
    }
    void query(int s,int l,int r){
        if(tree[s].l==l&&tree[s].r==r){
            maxx=max(maxx,tree[s].ma);
            minn=min(minn,tree[s].mi);
            return;
        }else{
            int mid=(tree[s].l+tree[s].r)/2;
            if(l>mid){
                query(s*2+1,l,r);
            }else if(r<=mid){
                query(s*2,l,r);
            }else{
                query(s*2,l,mid);
                query(s*2+1,mid+1,r);
            }
        }
    }
    int main(){
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        build(1,1,n);
        while(q--){
            scanf("%d%d",&a1,&b);
            minn=1000000+1000;
            maxx=-1;
            query(1,a1,b);
            printf("%d
    ",maxx-minn);
        }
        return 0;
    }
  • 相关阅读:
    避免因为Arcgis Server服务设置不当导致Oracle Process溢出的方法
    ArcSOC进程数不断增长导致oracle processes溢出原因分析
    PostgreSQL中的Toast Pointer
    SQLite中字段顺序和PAGE_SIZE对性能的影响
    PG数据库CPU和内存满负荷运转优化案
    ArcGIS Server浏览地图服务无响应原因分析说明
    PythonWeb全栈开发-虚拟环境搭建
    C 语言学习——错误处理
    C 语言学习——Valgrind 内存问题简述
    HTML学习--拖放API实现拖放排序
  • 原文地址:https://www.cnblogs.com/qqshiacm/p/10498427.html
Copyright © 2011-2022 走看看