POJ

来源:互联网 发布:死侍蜘蛛侠 知乎 编辑:程序博客网 时间:2024/04/29 22:42

题目链接:http://poj.org/problem?id=3264点击打开链接

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 55875 Accepted: 26190Case 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 ≤ 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 31734251 54 62 2

Sample Output

630

储存最小值最大值
然后区间查询即可

#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;#define maxn 300000+5struct xjy{    int left;    int right;    int mmin;    int mmax;};xjy tree[maxn<<2];int a[maxn];int minans,maxans;void build(int i,int left,int right){    if(left==right)    {        tree[i].left=left;        tree[i].right=right;        tree[i].mmin=a[left];        tree[i].mmax=a[left];        return ;    }    int mid=(left+right)>>1;    build(i<<1,left,mid);    build (i<<1|1,mid+1,right);    tree[i].left=left;    tree[i].right=right;    tree[i].mmax=max(tree[i<<1].mmax,tree[i<<1|1].mmax);    tree[i].mmin=min(tree[i<<1].mmin,tree[i<<1|1].mmin);}void query(int i,int left,int right){    if(tree[i].left==left&&right==tree[i].right)    {        maxans=max(maxans,tree[i].mmax);        minans=min(minans,tree[i].mmin);        return;    }    int mid=(tree[i].left+tree[i].right)>>1;    if(right<=mid)        query(i<<1,left,right);    else if(left>mid)        query(i<<1|1,left,right);    else    {        query(i<<1,left,mid);        query(i<<1|1,mid+1,right);    }}int main(){    int n,m;    int t;    int flag=0;    while(~scanf("%d%d",&n,&m))    {        for(int i=0;i<maxn;i++)        {            tree[i].left=0;            tree[i].right=0;            tree[i].mmin=0;            tree[i].mmax=0;            a[i]=0;        }        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        build(1,1,n);        int l,r;        for(int i=1;i<=m;i++)           {                scanf("%d%d",&l,&r);                minans=INT_MAX;                maxans=0;                query(1,l,r);                printf("%d\n",maxans-minans);           }    }    return 0;}







原创粉丝点击