码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3264 Balanced Lineup

时间:2015-07-30 19:11:34      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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
 
 
简单的线段树,练练手,刚开始用cin超时,改成scanf过了。
#include <iostream>
#include <cstdio>
using namespace std;

struct w
{
       int L;
       int R;
       int vmax;
       int vmin;
}k[800010];
int a=999999999,b=-999999999;
void set(int l,int r,int index)
{
     k[index].L=l;
     k[index].R=r;
     k[index].vmax=-999999999;
     k[index].vmin=999999999;
     if (l!=r)
     {
              set(l,(l+r)/2,index*2);
              set((l+r)/2+1,r,index*2+1);
     }
}

void insert(int index,int i,int v)
{
     if (k[index].L==k[index].R)
     {
          k[index].vmax=k[index].vmin=v;
          return;
     }
     if (k[index].vmax<v)
     k[index].vmax=v;
     if (k[index].vmin>v)
     k[index].vmin=v;
     if (i<=(k[index].R+k[index].L)/2)
     insert(index*2,i,v);
     else
     insert(index*2+1,i,v);
}

void quary(int index,int s,int e)
{
    // if (k[index].vmin>=a&&k[index].vmax<=b)
     
//return;
     if (k[index].L==s&&k[index].R==e)
     {
         if (k[index].vmin<a)
         a=k[index].vmin;
         if (k[index].vmax>b)
         b=k[index].vmax;
         return;
     }
     if (k[index].L==k[index].R)
     return;
     if (e<=(k[index].L+k[index].R)/2)
     quary(index*2,s,e);
     else if (s>(k[index].L+k[index].R)/2)
     quary(index*2+1,s,e);
     else
     {
         quary(index*2,s,(k[index].L+k[index].R)/2);
         quary(index*2+1,(k[index].L+k[index].R)/2+1,e);
     }
}
int main()
{
    int n,m,num,l,r;
    scanf("%d %d",&n,&m);
          set(1,n,1);
          for (int i=1;i<=n;i++)
          {
             // cin>>num;
             scanf("%d",&num);
              insert(1,i,num);
          }
          for (int i=1;i<=m;i++)
          {
             // cin>>l>>r;
             scanf("%d %d",&l,&r);
              a=999999999,b=-999999999;
              quary(1,l,r);
              //cout <<b-a<<endl;
              printf("%d\n",b-a);
          }
    return 0;
}

poj 3264 Balanced Lineup

标签:

原文地址:http://www.cnblogs.com/arno-my-boke/p/4689937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!