Tyvj1038 忠诚 (线段树)

【Tyvj1038】忠诚 线段树

 

题目描述

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

输入

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

输出

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

样例输入

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

样例输出

2 3 1

思路:

水一波线段树。。结果发现这道题没法提交了,因为比较简单,过了样例就姑且算对了吧。。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int M = 1e5 + 10;
int sum[M<<2];
void pushup(int rt){
    sum[rt] = min(sum[rt<<1|1],sum[rt<<1]);
}

void build(int l,int r,int rt){
    if(l == r){
        cin>>sum[rt];
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

int query(int L,int R,int l,int r,int rt){
    if(L <= l && R >= r){
        return sum[rt];
    }
    int m = (l + r) >> 1;
    int ret  = 99999999;
    if(L <= m) ret = min(ret,query(L,R,lson));
    if(R > m) ret = min(ret,query(L,R,rson));
    return ret;
}

int main()
{
     int n,m,x,y;
     cin>>n>>m;
     build(1,n,1);
     while(m--){
        cin>>x>>y;
        cout<<query(x,y,1,n,1)<<endl;
     }
     return 0;
}

 

posted @ 2018-03-13 21:11  冥想选手  阅读(131)  评论(0编辑  收藏  举报