zoukankan      html  css  js  c++  java
  • BZOJ-1699 Balanced Lineup 线段树区间最大差值

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

    题目大意:农夫JOHN让我们求一个区间上的最大差值(最大值-最小值)

    维护两棵线段树即可,一棵max,一棵min,然后求差值,自己第一次摸索写多棵线段树姿势,感觉写的还可以,以后待改进...
    

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define maxn 50001
    int maxs[maxn<<2]={0},mins[maxn<<2]={0};
    
    void updata1(int now)
    {
        maxs[now]=max(maxs[now<<1],maxs[now<<1|1]); 
    }
    
    void updata2(int now)
    {
        mins[now]=min(mins[now<<1],mins[now<<1|1]);
    }
    
    void point_change(int l,int r,int now,int loc,int num)
    {
        if (l==r)
            {
                maxs[now]=num;
                mins[now]=num;
                return;
            }
        int mid=(l+r)>>1;
        if (loc<=mid)
            point_change(l,mid,now<<1,loc,num);
        else
            point_change(mid+1,r,now<<1|1,loc,num);
        updata1(now);
        updata2(now);
    }
    
    int query(int L,int R,int now,int l,int r,bool f)//f为true为求最大,false为求最小 
    {
        if (L<=l && R>=r)
            if (f==true)
                return maxs[now];
            else
                return mins[now];
        int mid=(l+r)>>1;
        if (f==true)
            {
                int ans=0;
                if (L<=mid)
                    ans=max(ans,query(L,R,now<<1,l,mid,true));
                if (R>mid)
                    ans=max(ans,query(L,R,now<<1|1,mid+1,r,true));
                return ans;
            }
        else
            {
                int ans=100000000;
                if (L<=mid)
                    ans=min(ans,query(L,R,now<<1,l,mid,false));
                if (R>mid)
                    ans=min(ans,query(L,R,now<<1|1,mid+1,r,false));
                return ans;         
            }   
    }
    
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        //build(1,n,1);
        for (int i=1; i<=n; i++)
            {
                int x;
                scanf("%d",&x);
                point_change(1,n,1,i,x);
            }
        for (int i=1; i<=m; i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                int answer=query(x,y,1,1,n,true)-query(x,y,1,1,n,false);
                printf("%d
    ",answer);
            }
        return 0;
    }
  • 相关阅读:
    Kmp 加深理解 之 poj 3461
    Kmp 模板 之 hdu 1711 Number Sequence
    最大连续子序列和(经典DP) 之 hdu 1231 最大连续子序列
    数学 之 hdu 4710 Balls Rearrangement
    01背包变形 之 hdu 2126 Buy the souvenirs
    逆序数 之 hdu 1394 Minimum Inversion Number
    根据进程文件id查看所有进程信息
    N皇后问题
    17. 电话号码的字母组合
    697. 数组的度
  • 原文地址:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5346253.html
Copyright © 2011-2022 走看看