zoukankan      html  css  js  c++  java
  • 【Tyvj1038】忠诚

    Description

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

    Input

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

    Output

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

    Sample Input

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

    Sample Output

    2 3 1
    /*
        线段树,维护区间的最小值 
    */
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=1000010;
    int cnt=0;
    struct treetype{
        int lptr,rptr;//左右孩子指针
        int Left,Right; //区间[Left,Right)
        int mn;//区间最小值 
    }t[maxn*2];
    int a[maxn];
    void buildtree(int ll,int rr){
        int cur=++cnt;
        t[cnt].Left=ll; t[cnt].Right=rr;
        if (rr!=ll+1){
            t[cur].lptr=cnt+1; buildtree(ll,(ll+rr)/2);
            t[cur].rptr=cnt+1; buildtree((ll+rr)/2,rr);
            t[cur].mn=min(t[t[cur].lptr].mn,t[t[cur].rptr].mn);
        }else t[cur].mn=a[ll];
    }
    int query(int k,int ll,int rr){
        if (ll<=t[k].Left && rr>=t[k].Right) return t[k].mn;
        else {
            int ans;
            if (ll<(t[k].Left+t[k].Right)/2) ans=query(t[k].lptr,ll,rr);
            if (rr>(t[k].Left+t[k].Right)/2) ans=min(ans,query(t[k].rptr,ll,rr));
            return ans; //返回区间最小值 
        }
    }
    
    int main(){
        int m,n,x,y;
        scanf("%d%d",&m,&n);
        for (int i=1;i<=m;i++) scanf("%d",&a[i]);
        buildtree(1,m+1);
        for (int i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            printf("%d ",query(1,x,y+1));
        } 
        return 0;
    }
  • 相关阅读:
    Linux shell read命令
    mysql 详解 01
    centos 6/7 mysql 5.7 修改root密码 mysql root密码找回
    iptables基本原理讲解与规则的增删改查
    nginx反向代理https访问502, nginx反向代理, 支持SNI的https回源,SNI源点,nginx反向代理报错
    nginx 配置 强制访问https
    有名管道FIFO进程间数据传输实例
    命名管道FIFO及其读写规则
    224、Basic Calculator
    (匿名)管道的读写规则
  • 原文地址:https://www.cnblogs.com/liumengyue/p/5127915.html
Copyright © 2011-2022 走看看