ACM the water problem详解[C语言][线段树]


本题可以用分块、树状数组、线段树求解
分块算法需要构建索引表,时间复杂度也不低,但由于本题数据规模不大n<=1000,因此,分块也能解决问题
树状数组效率高,但数据规模的原因,时间上没有太大优势,而且其思路较复杂
综上原因,本题选择使用线段树求解,思路简单,时间复杂度满足条件,也节省空间

构建本题线段树如下图,假设所求区间为[1,n]

/************************************
Problem Description
In Land waterless, water is a very
limited resource. People always fight
for the biggest source of water.
Given a sequence of water sources
with a1,a2,a3,...,an representing
the size of the water source.
Given a set of queries each
containing 2 integers l and r,
please find out the biggest
water source between al and ar.

在干旱的地域,水是非常珍贵的资源
人们总是为了水源而争斗
现在给出一段水源,a1,a2...an表示水源规模的大小
并且给出两个数 l和r,求规模最大的al...ar之间的水源,


Input
First you are given an integer T(T≤10)
indicating the number of test cases.
For each test case, there is a number n(0≤n≤1000)
on a line representing the number of water sources.
n integers follow, respectively a1,a2,a3,...,an,
and each integer is in {1,...,10^6}. On the next line,
there is a number q(0≤q≤1000) representing the number of queries.
After that, there will be q lines with two integers l and r(1≤l≤r≤n)
indicating the range of which you should find out the biggest water source.

首先输入一个T(T<=10),表示有T组测试数据
下一行会有一个数字n(0<=n<=1000)表示水源的数量
在下一行依次输入各个水源的大小
再下一行有数字q(0<=q<=100)
下一行会有q组l和r(1≤l≤r≤n),分别求每组范围内的最大水源

Output
For each query,
output an integer representing the size of the biggest water source.

Sample Input

3
1
100
1
1 1
5
1 2 3 4 5
5
1 2
1 3
2 4
3 4
3 5
3
1 999999 1
4
1 1
1 2
2 3
3 3

Sample Output
100
2
3
4
4
5
1
999999
999999
1
**************************************/
//本题未涉及大数运算,测试数据较少,直接按定义求解即可
//按定义,即为求l~r之间的极大值
#include<stdio.h>
#include<string.h>

struct
{
    int l,r;
    int max;
}tree[4001];
int a[1001]={0};

int  ergtree(int i,int l,int r)
//由根遍历二叉树,如果当前节点的最大值与所求区间重合,返回该值
//否则,判断进入左右孩子,比较左右孩子返回的最大值,返回较大者
//i为当前节点序号
{
    int lm=0,rm=0,t=0;
    if(l==tree[i].l&&r==tree[i].r)
        return tree[i].max;
    if(l<=tree[i*2].r)
    {
        if(tree[i*2].r>r)
            lm=ergtree(i*2,l,r);
        else
            lm=ergtree(i*2,l,tree[i*2].r);
    }
    if(r>=tree[i*2+1].l)
    {
        if(tree[i*2+1].l<l)
            lm=ergtree(i*2+1,l,r);
        else
            rm=ergtree(i*2+1,tree[i*2+1].l,r);
    }
    return lm>rm?lm:rm;
}

/***************0
构建线段树
max存储[l,r]的max
函数返回当前最大值
构建后为完全二叉树
由根至叶,由左至右
依次编号为tree[i]
****************/
int build(int i,int l,int r)
{
    int lm,rm;
    tree[i].l=l;
    tree[i].r=r;
    if(l==r)        //相等无需继续递归,最大值即为a[l] or a[r]
    {
        tree[i].max=a[l];
        //printf("tree No:%d  l:%d r:%d max:%d\n",i,tree[i].l,tree[i].r,tree[i].max);
        return a[l];    //finish
    }

        //左孩子序号
    lm=build(i*2,l,(l+r)/2);
          //右孩子序号
    rm=build(i*2+1,(l+r)/2+1,r);

    tree[i].max = lm>rm ? lm : rm;
    //printf("Tree No:%d  l:%d r:%d max:%d\n",i,tree[i].l,tree[i].r,tree[i].max);
    return tree[i].max;
}

void main()
{
    //freopen("in.txt","r",stdin);
    int n,T,q;
    int i,j,k,max;
    int l,r;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(tree,0,sizeof(tree));
        //get data
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }

        //build segment tree
        build(1,1,n);
        scanf("%d",&q);
        //获取q,循环计算[l,r]间最大值,没得一次结果直接输出
        while(q--)
        {
            scanf("%d%d",&l,&r);
            //printf("root max:%d\n",tree[1].max);
            max=ergtree(1,l,r);     //从根开始搜索
            printf("%d\n",max);
        }

    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值