poj 3264 Balanced Lineup

来源:互联网 发布:mysql dba是什么意思 编辑:程序博客网 时间:2024/06/05 17:57

题目链接:点这里

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 iLines 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

【题意】

给你一段序列,问你该序列中某个区间的最大值和最小值之差。

【分析】

裸裸的线段树啊,更新都不用,直接在设定的时候维护一下最大最小值就行,然后一个查找函数就ok。什么lazy标记,什么区间更新啥的都不用,so
easy。直接套就中。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>using namespace std;#define rep0(i,l,r) for(int i = (l);i < (r);i++)#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS1(a) memset(a,-1,sizeof(a))#define MSi(a) memset(a,0x3f,sizeof(a))#define sin1(a) scanf("%d",&(a))#define sin2(a,b) scanf("%d%d",&(a),&(b))#define sll(a) scanf("%lld",&(a))#define sll2(a,b) scanf("%lld%lld",&(a),&(b))#define sdo(a) scanf("%lf",&(a))#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))#define inf 0x3f3f3f3f#define lson i<<1,l,mid#define rson ((i<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define MK make_pair#define ll long longtemplate<typename T>void read1(T &m){    T x=0,f=1;    char ch=getchar();    while(ch<'0'||ch>'9')    {        if(ch=='-')f=-1;        ch=getchar();    }    while(ch>='0'&&ch<='9')    {        x=x*10+ch-'0';        ch=getchar();    }    m = x*f;}template<typename T>void read2(T &a,T &b){    read1(a);    read1(b);}template<typename T>void read3(T &a,T &b,T &c){    read1(a);    read1(b);    read1(c);}template<typename T>void out(T a){    if(a>9) out(a/10);    putchar(a%10+'0');}template<typename T>void outn(T a){    if(a>9) out(a/10);    putchar(a%10+'0');    puts("");}using namespace std;const int Max=2e5+200;struct{    int l;    int r;    int m[2];//m[0] min m[1] max} segtree[Max<<2];int num;void settree(int ind,int l,int r){    segtree[ind].l=l;    segtree[ind].r=r;    segtree[ind].m[1] = -1;    segtree[ind].m[0] = Max<<3;    if(l==r) return ;    int mid=(l+r)>>1;    settree(ind<<1,l,mid);    settree(ind<<1|1,mid+1,r);}void update(int ind,int x,int value){    segtree[ind].m[1]=max(segtree[ind].m[1],value);    segtree[ind].m[0]=min(segtree[ind].m[0],value);    if(segtree[ind].l==segtree[ind].r) return;    int mid=(segtree[ind].l+segtree[ind].r)>>1;    if(mid>=x) update(ind<<1,x,value);    else update(ind<<1|1,x,value);}int getm(int ind,int l,int r,int x){    if(segtree[ind].l==l&&segtree[ind].r==r) return segtree[ind].m[x];    int mid=(segtree[ind].l+segtree[ind].r)>>1;    if(r<=mid) return getm(ind<<1,l,r,x);    else if(l>mid) return getm(ind<<1|1,l,r,x);    else    {        if(x) return max(getm(ind<<1,l,mid,x),getm(ind<<1|1,mid+1,r,x));        return min(getm(ind<<1,l,mid,x),getm(ind<<1|1,mid+1,r,x));    }}int main(){//    freopen("in.txt","r",stdin);    int n,q;    read2(n,q);    settree(1,1,n);    rep1(i,1,n)    {        read1(num);        update(1,i,num);    }    int l,r;    rep0(i,0,q)    {        read2(l,r);        outn(getm(1,l,r,1)-getm(1,l,r,0));    }    return 0;}