问题描述:输入N个数,M组区间,例如 6 3;对应得6个数为1 7 3 4 2 5,对应的三个区间为:1 5;4 6;2 2 计算每个区间的最大值与最小值的差值
样本输出:6 3 0
#include <iostream>
#include <stdio.h>
using namespace std;
const int INF=0xffffff0;
int max(int a,int b)
{
return (a>=b)?a:b;
}
int min(int a,int b)
{
return (a<b)?a:b;
}
struct line
{
int minn;
int maxx;
}a[200010];
int s[50010],maxnum,minnum;
void buildt(int l,int r,int step)
{
if(l==r)
{
a[step].maxx=a[step].minn=s[l];
return;
}
int mid=(l+r)/2;
buildt(l,mid,2*step);
buildt(mid+1,r,2*step+1);
a[step].maxx=max(a[2*step].maxx,a[2*step+1].maxx);
a[step].minn=min(a[2*step].minn,a[2*step+1].minn);
return ;
}
void findd(int l,int r,int step,int x,int y)
{
if(l==x&&r==y)
{
maxnum=max(maxnum,a[step].maxx);
minnum=min(minnum,a[step].minn);
return ;
}
int mid=(l+r)/2;
if(x>mid)
findd(mid+1,r,step*2+1,x,y);
else if(y<=mid)
findd(l,mid,step*2,x,y);
else
{
findd(mid+1,r,step*2+1,mid+1,y);
findd(l,mid,step*2,x,mid);
}
return;
}
int main()
{
int n,m,x,y,t;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&s[i]);
buildt(1,n,1);
while(m--)
{
scanf("%d%d",&x,&y);
maxnum=-INF;minnum=INF;
findd(1,n,1,x,y);
printf("%d\n",maxnum-minnum);
}
return 0;
}