poj 3264 Balanced Lineup

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 28851   Accepted: 13593
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<stdio.h>
#define INF 0x3f3f3f3f
int mmax,mmin;
int Max(int a,int b)
{
     return a>b?a:b;
}
int Min(int a,int b)
{
     return a<b?a:b;
}
struct node
{
     int l;
     int r;
     int max;
     int min;
}p[800005];
void build_T(int t,int l,int r)
{
     p[t].l=l;
     p[t].r=r;
     p[t].max=0;
     p[t].min=0;
     if(r==l)
          return;
     int temp=t<<1;
     int mid=(l+r)>>1;
     build_T(temp,l,mid);
     build_T(temp+1,mid+1,r);
}
void insert(int t,int l,int val)
{
     if(p[t].l==p[t].r)
     {
          p[t].max=val;
          p[t].min=val;
          return ;
     }
     int temp=t<<1;
     int mid=(p[t].l+p[t].r)>>1;
     if(l<=mid)
          insert(temp,l,val);
     else
          insert(temp+1,l,val);
     p[t].max=Max(p[temp].max,p[temp+1].max);
     p[t].min=Min(p[temp].min,p[temp+1].min);
}
void query(int t,int l,int r)
{

     if(p[t].r==r&&p[t].l==l)
     {
          mmax=Max(mmax,p[t].max);
          mmin=Min(mmin,p[t].min);
          return;
     }
     int temp=t<<1;
     int mid=(p[t].l+p[t].r)>>1;
     if(r<=mid)
          query(temp,l,r);
     else if(l>mid)
          query(temp+1,l,r);
     else
     {
          query(temp,l,mid);
          query(temp+1,mid+1,r);
     }
}
int main()
{
     int i,m,x,y,n;
     //freopen("in.txt","r",stdin);
     //freopen("out.txt","w",stdout);
     while(scanf("%d %d",&n,&m)!=EOF)
     {
          build_T(1,1,n);
          for(i=1;i<=n;i++)
          {
               scanf("%d",&x);
               insert(1,i,x);
          }
          while(m--)
          {
               scanf("%d %d",&x,&y);
               mmax=-INF;
               mmin=INF;
               query(1,x,y);
               printf("%d\n",mmax-mmin);
          }
     }
     return 0;
}

 

posted @ 2013-08-14 16:31  SprayT  阅读(201)  评论(0编辑  收藏  举报