tyvj 1038 忠诚 区间最小值 线段树或者rmq

P1038 忠诚
时间: 1000ms / 空间: 131072KiB / Java类名: Main

描述

    老管家是一个聪明能干的人。他为财主工作了整整10年,财主为了让自已账目更加清楚。要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意。但是由于一些人的挑拨,财主还是对管家产生了怀疑。于是他决定用一种特别的方法来判断管家的忠诚,他把每次的账目按1,2,3…编号,然后不定时的问管家问题,问题是这样的:在a到b号账中最少的一笔是多少?为了让管家没时间作假他总是一次问多个问题。

输入格式

输入中第一行有两个数m,n表示有m(m<=100000)笔账,n表示有n个问题,n<=100000。
第二行为m个数,分别是账目的钱数
后面n行分别是n个问题,每行有2个数字说明开始结束的账目编号。

输出格式

输出文件中为每个问题的答案。具体查看样例。

测试样例1

输入

10 3 
1 2 3 4 5 6 7 8 9 10 
2 7 
3 9 
1 10

输出

2 3 1
思路就是线段树,或者rmq st;
rmq:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
int a[100100];
int dp[100100][30];//存位置
int minn(int x,int y)
{
    return a[x]<=a[y]?x:y;
}
void rmq(int len)
{
    for(int i=0; i<len; i++)
    dp[i][0]=i;
    for(int j=1; (1<<j)<len; j++)
    for(int i=0; i+(1<<j)-1<len; i++)
    dp[i][j]=minn(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int query(int l,int r)
{
    int x=(int)(log((double)(r-l+1))/log(2.0));
    return minn(dp[l][x],dp[r-(1<<x)+1][x]);
}
int main()
{
    int x,y,q,i,t;
    while(~scanf("%d%d",&x,&q))
    {
        for(i=0;i<x;i++)
        scanf("%d",&a[i]);
        rmq(x);
        while(q--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            if(l>r)
            swap(l,r);
            printf("%d ",a[query(l-1,r-1)]);
        }
    }
}

线段树:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
struct is
{
    int l,r;
    int num;
}tree[200010*3];
void build_tree(int l,int r,int pos)
{
    tree[pos].l=l;
    tree[pos].r=r;
    if(l==r)
    {
        //tree[pos].num=1;
        scanf("%d",&tree[pos].num);
        return;
    }
    int mid=(l+r)/2;
    build_tree(l,mid,pos*2);
    build_tree(mid+1,r,pos*2+1);
    tree[pos].num=min(tree[pos*2].num,tree[pos*2+1].num);
}
int query(int l,int r,int pos)
{
    //cout<<l<<" "<<r<<" "<<pos<<endl;
    if(tree[pos].l==l&&tree[pos].r==r)
    return tree[pos].num;
    int mid=(tree[pos].l+tree[pos].r)/2;
    if(l>mid)
    return query(l,r,pos*2+1);
    else if(r<=mid)
    return query(l,r,pos*2);
    else
    return min(query(l,mid,pos*2),query(mid+1,r,pos*2+1));
}
int main()
{
    int x,y,i,t;
    while(~scanf("%d%d",&x,&y))
    {
        build_tree(1,x,1);
        for(i=0;i<y;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            printf("%d ",query(u,v,1));
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jhz033/p/5397742.html