hdu5443 【线段树】

题意:

思路:
暴力是可以的O(1e7),这里采用线段树,线段树区间查找O(logn)

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

const int INF=-0x3f3f3f3f;
const int MAXN=200000;

struct st{
    int left,right;
    int maxx;
};
st q[MAXN*4];
int n,m;


void built(int num,int L,int R)
{
    q[num].left=L;
    q[num].right=R;
    if(q[num].left==q[num].right)
    {
        scanf("%d",&q[num].maxx);
        return;
    }
    built(2*num,L,(L+R)/2);
    built(2*num+1,(L+R)/2+1,R);
    q[num].maxx=max(q[2*num].maxx,q[2*num+1].maxx);
}

int get_maxa(int s,int t,int num)
{
    if(s<=q[num].left&&t>=q[num].right)
        return q[num].maxx;
    if(s>q[num].right||t<q[num].left)
        return INF;
    int a,b;
    a=get_maxa(s,t,2*num);
    b=get_maxa(s,t,2*num+1);
    return max(a,b);
}

int main()
{
    int n,qq;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int s,t;
        scanf("%d",&n);
        built(1,1,n);
        scanf("%d",&qq);
        while(qq--)
        {
            scanf("%d%d",&s,&t);
            printf("%d\n",get_maxa(s,t,1));
        }
    }
    return 0;
}
posted @ 2016-09-02 18:00  see_you_later  阅读(100)  评论(0编辑  收藏  举报