ACM: 线段树 poj 3264

来源:互联网 发布:java回退流 编辑:程序博客网 时间:2024/05/17 04:10
Balanced Lineup
Description

For the daily milking, Farmer John's N cows (1 ≤ N≤ 50,000) always line up in the same order. One day Farmer Johndecides to organize a game of Ultimate Frisbee with some of thecows. To keep things simple, he will take a contiguous range ofcows from the milking lineup to play the game. However, for all thecows 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 thedifference in height between the shortest and the tallest cow inthe group.

Input

Line 1: Twospace-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer thatis the height of cow i
Lines N+2..N+Q+1: Two integers A andB (1 ≤ ABN), representing therange of cows from A to B inclusive.

Output

Lines 1..Q:Each line contains a single integer that is a response to a replyand indicates the difference in height between the tallest andshortest 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

 

题意: 求区间内最大最小值得差.

 

解题思路:

    1. 线段树简单应用.

 

代码:

#include<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 200005

struct node
{
 int l, r;
 int min, max;
}pt[MAX*2];

int n, m;
int a;
int A, B;

inline int min(int a, int b)
{
 return a < b ? a : b;
}

inline int max(int a, int b)
{
 return a > b ? a : b;
}

void buildTree(int l, int r, int pos)
{
 pt[pos].l = l, pt[pos].r = r;
 pt[pos].max =-(1<<30);
 pt[pos].min =(1<<30);
 if(l == r) return ;
 int mid = (l+r)/2;
 buildTree(l, mid, pos*2);
 buildTree(mid+1, r, pos*2+1);
}

void insert(int l, int r, int val, int pos)
{
 if(pt[pos].l == l&& pt[pos].r == r)
 {
  pt[pos].max = pt[pos].min =val;
  return ;
 }
 int mid = (pt[pos].l+pt[pos].r)/2;
 if(r <= mid)
  insert(l, r, val, pos*2);
 else if(l > mid)
  insert(l, r, val,pos*2+1);
 else
 {
  insert(l, mid, val,pos*2);
  insert(mid+1, r, val,pos*2+1);
 }
 pt[pos].min = min(pt[pos*2].min,pt[pos*2+1].min);
 pt[pos].max = max(pt[pos*2].max,pt[pos*2+1].max);
}

int findMax(int l, int r, int pos)
{
 if(pt[pos].l == l&& pt[pos].r == r)
 {
  return pt[pos].max;
 }
 int mid = (pt[pos].l+pt[pos].r)/2;
 if(r <= mid)
  return findMax(l, r,pos*2);
 else if(l > mid)
  return findMax(l, r,pos*2+1);
 else
 {
  return max(findMax(l, mid,pos*2), findMax(mid+1, r, pos*2+1));
 }
}

int findMin(int l, int r, int pos)
{
 if(pt[pos].l == l&& pt[pos].r == r)
 {
  return pt[pos].min;
 }
 int mid = (pt[pos].l+pt[pos].r)/2;
 if(r <= mid)
  return findMin(l, r,pos*2);
 else if(l > mid)
  return findMin(l, r,pos*2+1);
 else
 {
  return min(findMin(l, mid,pos*2), findMin(mid+1, r, pos*2+1));
 }
}

int main()
{
 int i;
// freopen("input.txt", "r", stdin);
 while(scanf("%d %d",&n,&m) != EOF)
 {
  buildTree(1, n, 1);
  for(i = 1; i <=n; ++i)
  {
   scanf("%d",&a);
   insert(i, i,a, 1);
  }

  for(i = 0; i< m; ++i)
  {
   scanf("%d%d",&A, &B);
   int result =findMax(A, B, 1)-findMin(A, B, 1);
   printf("%d\n",result);
  }
 }
 return 0;
}

0 0
原创粉丝点击