zoukankan      html  css  js  c++  java
  • poj 3264(简单线段树)

    思路很明确,直接用线段树.

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 26161   Accepted: 12250
    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

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define N 50050
    #define INF 0x3fffffff
    int n,q;
    
    struct node
    {
        int b,d;
        int mx,mi; 
        node* Tl;
        node* Tr;
    }edge[10*N];
    
    int cnt;
    
    node *Tbuild(int s,int t)
    {
        node* tmp=&edge[cnt++];
        tmp->mi=INF;
        tmp->mx=-1;
        tmp->Tl=tmp->Tr=NULL;
        tmp->b=s; tmp->d=t;
        int mid=(s+t)/2;
        if(s!=t)
        {
            tmp->Tl=Tbuild(s,mid);
            tmp->Tr=Tbuild(mid+1,t);
        }
        return tmp;
    }
    
    node* head;
    
    void insert(node* tmp,int s,int k)
    {
        if(s>tmp->mx) tmp->mx=s;
        if(s<tmp->mi) tmp->mi=s;
        if(tmp->b==tmp->d&&tmp->d==k)  return ;
        int mid = (tmp->b+tmp->d)/2;
        if(k<=mid)
        {
            insert(tmp->Tl,s,k);
        }
        else insert(tmp->Tr,s,k);
    }
    
    int findmx(node * tmp,int s,int t)
    {
        if(tmp->b==s&&tmp->d==t)
        {
            return tmp->mx;
        }
        int mid=(tmp->b+tmp->d)/2;
        if(s>mid) return findmx(tmp->Tr,max(mid+1,s),t);
        if(t<=mid) return findmx(tmp->Tl,s,min(mid,t));
        return max(findmx(tmp->Tl,s,mid),findmx(tmp->Tr,mid+1,t));
    }
    
    int findmi(node * tmp,int s,int t)
    {
        if(tmp->b==s&&tmp->d==t)
        {
            return tmp->mi;
        }
        int mid=(tmp->b+tmp->d)/2;
        if(s>mid) return findmi(tmp->Tr,max(mid+1,s),t);
        if(t<=mid) return findmi(tmp->Tl,s,min(mid,t));
        return min(findmi(tmp->Tl,s,mid),findmi(tmp->Tr,mid+1,t));
    }
    
    int main()
    {
        cnt=0;
        scanf("%d%d",&n,&q);
        head=Tbuild(1,n);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            insert(head,x,i);
        }
        for(int i=0;i<q;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            int tx,ti;
            tx=findmx(head,x,y);
            ti=findmi(head,x,y);
            printf("%d\n",tx-ti);
        }
        return 0;
    }
  • 相关阅读:
    cookie
    手写Promise/Promise.all/promise.race
    Hbuilder如何真机调试?
    什么是深拷贝?什么是浅拷贝?如何实现深拷贝?
    Vue.set()?怎么用?
    vueRouter怎么用?
    Vue如何实现组件间通信?
    reduce()累加器
    filter()数组遍历
    map()数组遍历
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2934860.html
Copyright © 2011-2022 走看看