• tyvj:1038 忠诚 线段树


    tyvj:1038 忠诚

    Time Limit: 1 Sec  Memory Limit: 131072KiB
    Submit: 9619  Solved: 3287

    题目连接

    http://www.tyvj.cn/p/1038

    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

    HINT


    题解:

    线段树裸题~

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 400001
    #define mod 10007
    #define eps 1e-9
    //const int inf=0x7fffffff;   //无限大
    const int inf=0x3f3f3f3f;
    /*
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    */
    //**************************************************************************************
    
    int n,q,a[maxn];
    struct data
    {
        int l,r,mn;
    }tr[maxn];
    void build(int k,int s,int t)
    {
        tr[k].l=s;tr[k].r=t;
        if(s==t)
        {
            tr[k].mn=a[s];
            return;
        }
        int mid=(s+t)>>1;
        build(k<<1,s,mid);
        build(k<<1|1,mid+1,t);
        tr[k].mn=min(tr[k<<1].mn,tr[k<<1|1].mn);
    }
    int ask(int k,int s,int t)
    {
        int l=tr[k].l,r=tr[k].r;
        if(s==l&&t==r)
            return tr[k].mn;
        int mid=(l+r)>>1;
        if(t<=mid)
            return ask(k<<1,s,t);
        if(s>mid)
            return ask(k<<1|1,s,t);
        return min(ask(k<<1,s,mid),ask(k<<1|1,mid+1,t));
    }
    int main()
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        build(1,1,n);
        for(int i=1;i<=q;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            printf("%d ",ask(1,x,y));
        }
        return 0;
    }
  • 相关阅读:
    python | if else || where true 流程控制
    python |生成器
    python| 什么是迭代器?
    Python -WordCloud安装、词云制作
    技巧 |Python 使用dict.fromkeys()快速生成一个字典
    python 列表(list)去重方法总结
    区别 |python 的read、readline、readlines和write、writelines
    区别 |Python的 open() 和with open() as
    散点图和气泡图的几种制作方法
    宽表和窄表的建设该如何选择?
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4379496.html
Copyright © 2020-2023  润新知