线段树(区间数)

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述
这里写图片描述

Language:
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 64465 Accepted: 30048
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

分析,题目要求求指定区间的最大最下值的差值,用到区间树就会更加简单,先构建树再输入数值时候可以对每个区间的最大最小值进行操作。

首先定义每个节点,必须要包括左右边界,向上取整中间值,其余需要存储的数据根据题目要求添加

struct data / /节点
{
int R,L;
int mid()//向上取整的中间值
{
return (R+L)/2;
}
}

题目要求在每个区间应添加每个区间的最大最小值

struct data
{
    int R,L;
    int max,min;
    int mid()
    {
       return (R+L)/2;
    }
}

第二步是构建区间树

Build(int root,int R,int L)
{

        对数值进行操作
        if (r != l)
    {
        build(2*root+1,l,(r+l)/2);
        build(2 * root + 2, ((r + l) / 2) + 1, r);
    }
}

第三步 insert

void insert(int root,int i,int v)
{
    if (p[root].l == p[root].r)
    {
        数值操作
        return;
    }
    数值操作
    if (p[root].mid() >= i)
        insert(2 * root + 1, i, v);
    else
        insert(2 * root + 2, i, v);
}

题目代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 0xffffff0;//负无穷
int maxv = -INF;
int minv = INF;
struct d
{
    int r, l;
    int max, min;
    int mid()
    {
        return (l+r)/2;
    }
}p[800010];
void build(int root, int l, int r)
{
    p[root].l = l;
    p[root].r = r;
    p[root].max = -INF;
    p[root].min = INF;
    if (r != l)
    {
        build(2*root+1,l,(r+l)/2);
        build(2 * root + 2, ((r + l) / 2) + 1, r);
    }
}
void insert(int root,int i,int v)
{
    if (p[root].l == p[root].r)
    {
        p[root].max = p[root].min = v;
        return;
    }
    p[root].max = max(v, p[root].max);
    p[root].min = min(v, p[root].min);
    if (p[root].mid() >= i)
        insert(2 * root + 1, i, v);
    else
        insert(2 * root + 2, i, v);
}
void find(int root,int l,int r)
{
    if (p[root].max <= maxv && p[root].min >= minv)  //剪枝
        return;
    if (p[root].l == l&&p[root].r == r)//如果找到正好重合的区间就停止向下找
    {
        maxv = max(maxv, p[root].max);
        minv = min(minv,p[root].min);
        return;
    }
    if (p[root].mid() >= r)
        find(2 * root + 1, l, r);
    else if (p[root].mid() < l)
        find(2 * root + 2, l, r);
    else
    {
    find(2 * root + 1, l, p[root].mid());
    find(2 * root + 2, p[root].mid() + 1, r);
    }

}
int main()
{
    int n, t, m;
    int R, L;
    scanf("%d%d",&n,&t);
    build(0, 1, n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d",&m);
        insert(0,i,m);
    }
    for (int i = 1; i <= t; i++)
    {
        scanf("%d%d",&L,&R);
        minv = INF;
        maxv = -INF;
        find(0,L,R);
        printf("%d\n",maxv-minv);
    }
    return 0;
}
发布了24 篇原创文章 · 获赞 5 · 访问量 3953

猜你喜欢

转载自blog.csdn.net/qq_41345281/article/details/81947966