您的位置:首页 > 理论基础 > 数据结构算法

CUGB专题训练之数据结构:A - Balanced Lineup(线段树单点或RMQ)

2014-03-05 21:22 429 查看
A - Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status

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


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: