zoukankan      html  css  js  c++  java
  • POJ3264 Balanced Lineup

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 22573   Accepted: 10499
    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 ≤ 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 <stdio.h>
    using namespace std;
    structnode
    {
       int l,r;
       int Min,Max;
    };
    node st[200000];
    int N,Q,r_max,r_min;
    void built(int k,int x,int y)
    {
       st[k].l=x;
       st[k].r=y;
       if(x==y)
       {
    	   scanf("%d",&st[k].Max);
    	   st[k].Min=st[k].Max;
    	   return;
       }
       int m=(x+y)>>1;
       int t=k<<1;
       built(t,x,m);
       built(t|1,m+1,y);
       st[k].Max=st[t].Max>st[t|1].Max?st[t].Max:st[t|1].Max;
       st[k].Min=st[t].Min<st[t|1].Min?st[t].Min:st[t|1].Min;
    }
    void Qu(int k,int x,int y)
    {
       if(st[k].l==x&&st[k].r==y)
       {
          if(st[k].Max>r_max) r_max=st[k].Max;
    	  if(st[k].Min<r_min) r_min=st[k].Min;
    	  return ;
       }
       int m=(st[k].l+st[k].r)>>1;
       int t=k<<1;
       if(x>m)
       {
          Qu(t|1,x,y);
       }
       else if(y<=m)
       {
           Qu(t,x,y);
       }
       else
       {
             Qu(t,x,m);
    		 Qu(t|1,m+1,y);
       }
    }
    int main()
    {
    	int x,y;
        while(scanf("%d%d",&N,&Q)!=EOF)
    	{
    	    built(1,1,N);
           while(Q--)
    	   {
    	       r_max=0;r_min=1000000;
    		   scanf("%d%d",&x,&y);
    		   Qu(1,x,y);
    		   printf("%d\n",r_max-r_min);
    	   }
    
    	}
        return 0;
    }
  • 相关阅读:
    弹窗
    [转]JNI字段描述符“([Ljava/lang/String;)V”
    [转]JNIEnv解析
    [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
    [转]Linux下如何查看版本信息
    [转]apt-get 与 yum的区别 (转)
    我的tesseract学习记录(二)
    [转]pkg-config的用法
    [转]linux 创建连接命令 ln -s 软链接
    如何写makefile
  • 原文地址:https://www.cnblogs.com/372465774y/p/2579928.html
Copyright © 2011-2022 走看看