zoukankan      html  css  js  c++  java
  • Balanced Lineup

    裸线段树,没啥好说的,直接上

    Problem 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
     
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    #define INF 0xfffffff
    #define maxn 50000
    struct node
    {
        int L, R, maxv, minv;
        int Mid(){return (L+R)/2;}
    };
    node a[maxn*4+10];
    int maxv, minv;
    void BuildTree(int r, int L, int R);
    void Insert(int r, int k, int e);
    void Query(int r, int L, int R);
    int main()
    {
        int N, Q;
        while(scanf("%d%d", &N, &Q) != EOF)
        {
            int i, e, L, R;
            BuildTree(1, 1, N);
            for(i=1; i<=N; i++)
            {
                scanf("%d", &e);
                Insert(1, i, e);
            }
            for(i=0; i<Q; i++)
            {
                scanf("%d%d", &L, &R);
                maxv = -INF, minv = INF;
                Query(1, L, R);
                printf("%d ", maxv-minv);
            }
        }
        return 0;
    }
    void BuildTree(int r, int L, int R)
    {
        a[r].L = L, a[r].R = R;
        a[r].maxv = -INF, a[r].minv = INF;
        if(L == R)return ;
        BuildTree(r*2, L, a[r].Mid());
        BuildTree(r*2+1, a[r].Mid()+1, R);
    }
    void Insert(int r, int k, int e)
    {
        if(a[r].L == a[r].R)
        {
            a[r].maxv = a[r].minv = e;
            return ;
        }
        a[r].maxv = max(a[r].maxv, e);
        a[r].minv = min(a[r].minv, e);
        if(k <= a[r].Mid())
            Insert(r*2, k, e);
        else
            Insert(r*2+1, k, e);
    }
    void Query(int r, int L, int R)
    {
        if(maxv >= a[r].maxv && minv <= a[r].minv)
            return ;
        if(a[r].L == L && a[r].R == R)
        {
            maxv = max(a[r].maxv, maxv);
            minv = min(a[r].minv, minv);
            return ;
        }
        if(R <= a[r].Mid())
            Query(r*2, L, R);
        else if(L > a[r].Mid())
            Query(r*2+1, L, R);
        else
        {
            Query(r*2, L, a[r].Mid());
            Query(r*2+1, a[r].Mid()+1, R);
        }
    }
  • 相关阅读:
    8.指针小结
    8.指针
    7.数组
    6.结构化程序设计
    python之迭代器
    1.python基础—有这篇文章足够
    python装饰器,细致讲解
    django客户管理系统-使用modelform对HTML标签统一添加样式
    python之md5使用方法
    git干货教程
  • 原文地址:https://www.cnblogs.com/liuxin13/p/3873940.html
Copyright © 2011-2022 走看看