Balanced Lineup(線段樹單點或RMQ)
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
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
RMQ直接套演算法經典入門裡的模板就行了,但是線段樹因為好久不做了,所以今晚自己寫了兩個小時,才搞定……哈哈……不過太開心了,終於自己不用看模板也能寫出來了,那個建樹和插入函式都比較容易,但是在查詢的時候傻B了,左右子樹判斷錯了,然後無限迴圈,改了許久才知道這個錯,……嘿嘿,也學到了許多,加油!!!!
解法一:線段樹
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define f(i,a,n) for(i=a;i<n;i++)
#define F(i,a,n) for(i=a;i<=n;i++)
#define MM 100005
#define MN 505
#define INF 10000007
using namespace std;
typedef long long ll;
struct node
{
int l,r,Max,Min;
}tree[MM*8];
int a;
void build(int i,int l,int r)
{
tree[i].l=l; tree[i].r=r;
tree[i].Max=-INF; tree[i].Min=INF; //記下每段的最大最小值,然後輸出的時候就用大的減小的就行了
if(l==r) return;
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
}
void insert(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
{
tree[i].Max=tree[i].Min=a; //葉子結點的最大最小值都是自己
return ;
}
if(tree[i].Max<a) tree[i].Max=a; //如果非葉子結點就更新最大最小值
if(tree[i].Min>a) tree[i].Min=a;
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid) insert(i<<1,l,r); //轉到左子樹
else insert(i<<1|1,l,r); //轉到右子樹
}
int Max,Min;
void query(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
{
Max=max(Max,tree[i].Max);
Min=min(Min,tree[i].Min);
return ;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(l<=mid&&mid+1<=r)
{
query(i<<1,l,mid); //如果查詢區間在左右子樹兩邊,則進行下去
query(i<<1|1,mid+1,r);
}
if(r<=mid) query(i<<1,l,r); //查詢區間在左子樹的情況
if(l>=mid+1) query(i<<1|1,l,r); //查詢區間在右子樹的情況
}
int main()
{
int n,q,i,l,r;
scanf("%d%d",&n,&q);
build(1,1,n);
for(i=1;i<=n;i++)
{
sca(a);
insert(1,i,i); //插入線段樹,更新最大最小值
}
while(q--)
{
Max=-INF,Min=INF;
scanf("%d%d",&l,&r);
query(1,l,r);
printf("%d\n",Max-Min);
}
return 0;
}
解法二:RMQ
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define f(i,a,n) for(i=a;i<n;i++)
#define F(i,a,n) for(i=a;i<=n;i++)
#define MM 50005
#define MN 1005
#define INF 10000007
using namespace std;
typedef long long ll;
int n,d[MM][30],d1[MM][30];
void RMQ_init()
{
int i,j;
for(i=1; i<=n; i++)
sca(j),d[i][0]=d1[i][0]=j;
for(j=1; (1<<j)<=n; j++)
for(i=1; i+(1<<j)-1<=n; i++) //剛開始從0到n-1就是答案不對,改成1到n就對了,暈……
d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]),
d1[i][j]=min(d1[i][j-1],d1[i+(1<<(j-1))][j-1]);
}
int RMQ(int L,int R)
{
int k=0;
while((1<<(k+1))<=R-L+1) k++;
return max(d[L][k],d[R-(1<<k)+1][k])-min(d1[L][k],d1[R-(1<<k)+1][k]);
}
int main()
{
int q,l,r,val;
scanf("%d%d",&n,&q);
RMQ_init();
while(q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",RMQ(l,r));
}
return 0;
}