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;
    }
  • 相关阅读:
    windows7 端口查看以及杀死进程释放端口
    字符设备驱动模块与测试代码编写。
    c++项目范例
    较复杂makefile跟lds脚本程序的编写
    S5PV210时钟,看门狗定时器
    S5PV210中断处理
    arm 异常处理结构
    arm指令系统
    arm体系结构
    s5pv210 的启动
  • 原文地址:https://www.cnblogs.com/372465774y/p/2579928.html
Copyright © 2011-2022 走看看