POJ - 3264 Balanced Lineup(分块||线段树)

Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 62455 Accepted: 29131
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 ≤ 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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

题意:给n个数字,下标1……n,q个询问,区间l到r中最大值与最小值的差

思路:分块或者线段树都可以

首先是分块的代码,分块的思路是,满足一整个块的块间暴力,不满足的块内暴力,复杂度为q*sqrt(n)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 100005
using namespace std;
typedef long long ll;

int main(){
	int n,q;
	scanf("%d%d",&n,&q);
	int a[50004];
	int i,j;
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	int block=sqrt(n);
	int num=n/block;
	if(n%block!=0)num++;
	int belong[50004];
	int ma[50004];
	int mi[50004];
	for(i=1;i<=n;i++){
		belong[i]=(i-1)/block+1;
	}
	int l[50004],r[50004];
	for(i=1;i<=num;i++){
		l[i]=(i-1)*block+1;
		r[i]=i*block;
	}
	r[num]=n;
	for(i=0;i<5004;i++){
		ma[i]=0;
		mi[i]=10000000;
	}
	for(i=1;i<=num;i++){
		for(j=l[i];j<=r[i];j++){
			ma[i]=max(ma[i],a[j]);
			mi[i]=min(mi[i],a[j]);
		}
	}
	while(q--){
		int L,R;
		scanf("%d%d",&L,&R);
		int ans1=0;
		int ans2=10000000;
		if(belong[L]==belong[R]){//当L和R在同一块的情况需要特判
			for(i=L;i<=R;i++){
				ans1=max(ans1,a[i]);
				ans2=min(ans2,a[i]);
			}
			printf("%d\n",ans1-ans2);
			continue;
		}
		for(i=belong[L]+1;i<=belong[R]-1;i++){
			ans1=max(ans1,ma[i]);
			ans2=min(ans2,mi[i]);
		}
		for(i=L;i<=r[belong[L]];i++){
			ans1=max(ans1,a[i]);
			ans2=min(ans2,a[i]);
		}
		for(i=l[belong[R]];i<=R;i++){
			ans1=max(ans1,a[i]);
			ans2=min(ans2,a[i]);
		}
		printf("%d\n",ans1-ans2);
	}
	return 0;
}

下面是线段树的代码,线段树区间更新,模板题,复杂度为q*logn

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 100005
using namespace std;
typedef long long ll;

ll tree[maxn<<2],lson[maxn<<2],rson[maxn<<2],a[maxn];
ll n,q,cnt;

void build(int l,int r){
	int rt=cnt;
	if(l==r){
		tree[rt]=a[l];
	}
	else{
		int mid=(l+r)>>1;
		lson[rt]=++cnt;
		build(l,mid);
		rson[rt]=++cnt;
		build(mid+1,r);
		tree[rt]=max(tree[lson[rt]],tree[rson[rt]]);
	}
}

ll query(int rt,int L,int R,int l,int r){
	if(l==L&&r==R)return tree[rt];
	int mid=(L+R)>>1;
	if(l>mid)return query(rson[rt],mid+1,R,l,r);
	else if(r<=mid)return query(lson[rt],L,mid,l,r);
	else return max(query(lson[rt],L,mid,l,mid),query(rson[rt],mid+1,R,mid+1,r));
}

ll cnt2;
ll tree2[maxn<<2],lson2[maxn<<2],rson2[maxn<<2];

void build2(int l,int r){
	int rt=cnt2;
	if(l==r){
		tree2[rt]=a[l];
	}
	else{
		int mid=(l+r)>>1;
		lson2[rt]=++cnt2;
		build2(l,mid);
		rson2[rt]=++cnt2;
		build2(mid+1,r);
		tree2[rt]=min(tree2[lson2[rt]],tree2[rson2[rt]]);
	}
}

ll query2(int rt,int L,int R,int l,int r){
	if(l==L&&r==R)return tree2[rt];
	int mid=(L+R)>>1;
	if(l>mid)return query2(rson2[rt],mid+1,R,l,r);
	else if(r<=mid)return query2(lson2[rt],L,mid,l,r);
	else return min(query2(lson2[rt],L,mid,l,mid),query2(rson2[rt],mid+1,R,mid+1,r));
}

int main(){
	scanf("%lld%lld",&n,&q);
	int i;
	for(i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	cnt=0;
	cnt2=0;
	build(1,n);
	build2(1,n);
	while(q--){
		ll l,r;
		scanf("%lld%lld",&l,&r);
		printf("%d\n",query(0,1,n,l,r)-query2(0,1,n,l,r));
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值